feat(d.ts): allow alternatives schema at strict object schema#2835
Closed
yarkoyarok wants to merge 1 commit intohapijs:masterfrom
Closed
feat(d.ts): allow alternatives schema at strict object schema#2835yarkoyarok wants to merge 1 commit intohapijs:masterfrom
yarkoyarok wants to merge 1 commit intohapijs:masterfrom
Conversation
Marsup
reviewed
Sep 21, 2022
| type StrictSchemaMap<TSchema = any> = { | ||
| [key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]> | ||
| type StrictSchemaMap<TSchema = any> = { | ||
| [key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]> | AlternativesSchema |
Collaborator
There was a problem hiding this comment.
To me it's the wrong place to do it. I've tried it by modifying ObjectPropertiesSchema alone and it seems to work, let me know what you think.
type IsUnion<T, U extends T = T> =
T extends unknown ? [U] extends [T] ? false : true : false;
type ObjectPropertiesSchema<T = any> =
true extends IsUnion<Exclude<T, undefined | null>>
? Joi.AlternativesSchema
: T extends NullableType<string>
? Joi.StringSchema
: T extends NullableType<number>
? Joi.NumberSchema
: T extends NullableType<bigint>
? Joi.NumberSchema
: T extends NullableType<boolean>
? Joi.BooleanSchema
: T extends NullableType<Date>
? Joi.DateSchema
: T extends NullableType<Array<any>>
? Joi.ArraySchema
: T extends NullableType<object>
? (StrictSchemaMap<T> | ObjectSchema<T>)
: never
Contributor
Author
There was a problem hiding this comment.
your solution seems to be better! I am fine with it.
Collaborator
|
Fixed in #2844, I picked your test though. Thanks for the PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #2622
Initially at #2622 topic starter was mentioning
AlternativesSchemaat types. But in implementation it's lost.While we can have union type inside interfaces, eg:
field: string | number, the only way to describe them by Joi is to useAlternativesSchema. So as I see strict object schema map should allowAlternativesSchemafor any field, while we cannot detect in TS, where do we have union type and where not.Currently we cannot use strict mode for schemas of interfaces which have union types.