-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.d.ts
90 lines (79 loc) · 2.73 KB
/
index.d.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
type Type = Function | string
interface PropertyDefinition {
type?: Type;
required?: boolean;
length?: number | { min?: number; max?: number };
size?: number | { min?: number; max?: number };
enum?: string[];
each?: Rule;
elements?: Rule[];
match?: RegExp;
use?: { [key: string]: ValidationFunction };
message?: string | MessageFunction | { [key: string]: string | MessageFunction };
schema?: Schema;
properties?: SchemaDefinition;
}
type Rule = Type
| PropertyDefinition
| SchemaDefinition
| Schema
| RuleArray
// Remove when ts 3.7 is released
interface RuleArray extends Array<Rule> {}
interface SchemaDefinition {
[key: string]: Rule
}
interface ValidationFunction {
(value: any, ctx: object, ...args: any): boolean;
}
interface TypecastFunction {
(value: any): unknown;
}
interface MessageFunction {
(path: string, ctx: object, ...args: any): string;
}
interface ValidationOptions {
typecast?: boolean;
strip?: boolean;
strict?: boolean;
}
export class ValidationError extends Error {
constructor(message: string, path: string);
path: string;
status: number;
expose: boolean;
}
export default class Schema {
constructor(definition?: SchemaDefinition, options?: ValidationOptions);
path(path: string, rules?: Rule): Property;
assert(target: { [key: string]: any }, options?: ValidationOptions): void;
validate(target: { [key: string]: any }, options?: ValidationOptions): ValidationError[];
message(validator: string, message: string | MessageFunction): Schema;
message(messages: { [validator: string]: string | MessageFunction }): Schema;
validator(name: string, fn: ValidationFunction): Schema;
validator(validators: { [name: string]: ValidationFunction }): Schema;
typecaster(name: string, fn: TypecastFunction): Schema;
typecaster(typecasters: { [name: string]: TypecastFunction }): Schema;
}
declare class Property {
constructor(name: string, schema: Schema);
message(messages: (string | MessageFunction) | { [validator: string]: string | MessageFunction }): Property;
schema(schema: Schema): Property;
use(fns: { [name: string]: ValidationFunction }): Property;
required(bool?: boolean): Property;
type(type: Type): Property;
string(): Property;
number(): Property;
array(): Property;
date(): Property;
length(rule: number | { min?: number; max?: number }): Property;
size(rule: number | { min?: number; max?: number }): Property;
enum(enums: string[] | number[]): Property;
match(regexp: RegExp): Property;
each(rules: Rule): Property;
elements(arr: Rule[]): Property;
properties(props: SchemaDefinition): Property;
path(path: string, rules?: Rule): Schema;
typecast<T>(value: any): T;
validate(value: any, ctx: object, path: string): ValidationError | null;
}