Created
March 6, 2020 20:06
-
-
Save FFKL/8614cdc679e05bf082392320fe30f07f to your computer and use it in GitHub Desktop.
Example of custom types for fastest-validator. Advanced auto completion.
This file contains 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
interface ValidationTest { | |
arr: { haha: string } | { hoho: number }[]; | |
obj: { oops: boolean }; | |
str: string; | |
} | |
const schema: Validation<ValidationTest> = { | |
arr: { type: 'array', items: { type: 'object', props: { haha: { type: 'string' } } } }, | |
obj: { type: 'object', props: { oops: { type: 'boolean' } } }, | |
str: { type: 'string' } | |
}; | |
type Validation<T> = { | |
[key in keyof T]?: PatchedRule<T[key]>; | |
}; | |
type PatchedRule<T> = ValidationRuleObject | { type: 'object', props: Validation<T> } | PatchedArray<T>; | |
type PatchedArray<T> = { type: 'array', items: PatchedRule<T extends (infer R)[] ? R : T> }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment