Skip to content

Instantly share code, notes, and snippets.

@FFKL
Created March 6, 2020 20:06
Show Gist options
  • Save FFKL/8614cdc679e05bf082392320fe30f07f to your computer and use it in GitHub Desktop.
Save FFKL/8614cdc679e05bf082392320fe30f07f to your computer and use it in GitHub Desktop.
Example of custom types for fastest-validator. Advanced auto completion.
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