Skip to content

Commit

Permalink
fix(validator): GetSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 16, 2023
1 parent e2de55c commit e30c7ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/validator/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,20 @@ describe('yup', () => {
}),
).toMatchSnapshot()
})

test('mixed', () => {
type Message = {
id: number
image: File
}

const _ = yup
const messageSchemaIgnored: yup.GetSchema<Message> = _.object($ =>
$.shape({
id: _.number().required(),
image: _.mixed().required(),
}),
)
expect(1).toBe(1)
})
})
31 changes: 16 additions & 15 deletions src/validator/yupTypes/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,22 @@ export interface MixedSchema<T = any> {
}

export type GetSchema<T> =
// 为何要加 []
// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
[T] extends [string]
? StringSchema<T>
: [T] extends [number]
? NumberSchema<T>
: [T] extends [boolean]
? BooleanSchema<T>
: T extends Date
? DateSchema<T>
: T extends Array<infer X>
? ArraySchema<X>
: T extends {}
? ObjectSchema<T>
: MixedSchema<T>
| (// 为何要加 []
// https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
[T] extends [string]
? StringSchema<T>
: [T] extends [number]
? NumberSchema<T>
: [T] extends [boolean]
? BooleanSchema<T>
: [T] extends [Date]
? DateSchema<T>
: T extends Array<infer X>
? ArraySchema<X>
: T extends {}
? ObjectSchema<T>
: MixedSchema<T>)
| MixedSchema<T>

export type GetObjectSchema<T extends {}> = {
[K in keyof T]: GetSchema<T[K]>
Expand Down

0 comments on commit e30c7ae

Please sign in to comment.