Skip to content

feat(d.ts): allow alternatives schema at strict object schema#2835

Closed
yarkoyarok wants to merge 1 commit intohapijs:masterfrom
yarkoyarok:feat-2622-allow-alternatives-schema-at-strict-object-schema
Closed

feat(d.ts): allow alternatives schema at strict object schema#2835
yarkoyarok wants to merge 1 commit intohapijs:masterfrom
yarkoyarok:feat-2622-allow-alternatives-schema-at-strict-object-schema

Conversation

@yarkoyarok
Copy link
Contributor

Part of #2622

Initially at #2622 topic starter was mentioning AlternativesSchema at 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 use AlternativesSchema. So as I see strict object schema map should allow AlternativesSchema for 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.

@yarkoyarok yarkoyarok changed the title feat: allow alternatives schema at strict object schema feat(d.ts): allow alternatives schema at strict object schema Sep 12, 2022
type StrictSchemaMap<TSchema = any> = {
[key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]>
type StrictSchemaMap<TSchema = any> = {
[key in keyof TSchema]-?: ObjectPropertiesSchema<TSchema[key]> | AlternativesSchema
Copy link
Collaborator

@Marsup Marsup Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your solution seems to be better! I am fine with it.

@Marsup
Copy link
Collaborator

Marsup commented Sep 22, 2022

Fixed in #2844, I picked your test though. Thanks for the PR!

@Marsup Marsup closed this Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants