generated from ericvera/ts-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
65 lines (54 loc) · 1.38 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
export enum ValidatorType {
STRING = 'string',
NUMBER = 'number',
BOOLEAN = 'boolean',
OBJECT = 'object',
ARRAY = 'array',
}
export type ValidationFunction = (
value: unknown,
path?: string[],
key?: string,
) => void
export type GetPropValidatorFunction<T> = (
key: keyof T,
) => Schema<T>[typeof key]
export type TestFunction<TData = unknown> = (
value: Partial<TData>,
path?: string[],
key?: string,
) => void
export interface ValidatorBase {
validate: ValidationFunction
type: ValidatorType
}
export interface StringValidator extends ValidatorBase {
type: ValidatorType.STRING
}
export interface NumberValidator extends ValidatorBase {
type: ValidatorType.NUMBER
}
export interface BooleanValidator extends ValidatorBase {
type: ValidatorType.BOOLEAN
}
export interface ObjectValidator<TData> extends ValidatorBase {
type: ValidatorType.OBJECT
getProp: GetPropValidatorFunction<TData>
}
export interface ArrayValidator extends ValidatorBase {
type: ValidatorType.ARRAY
}
export type Value<TData> = TData extends unknown[]
? ArrayValidator
: TData extends object
? ObjectValidator<TData>
: TData extends string
? StringValidator
: TData extends number
? NumberValidator
: TData extends boolean
? BooleanValidator
: ValidatorBase
export type Schema<TData> = {
[P in keyof TData]-?: Value<TData[P]>
}