-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.d.ts
149 lines (123 loc) · 3.36 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// abstract interfaces for extending
interface basicRule<T> {
type: string,
optional?: boolean,
oneOf?: Array<T>,
custom?: (arg: any, object: this) => boolean,
}
interface basicNumberRule extends basicRule<number> {
min?: number,
max?: number,
equal?: number,
}
interface basicStringRule extends basicRule<string> {
minLength?: number,
maxLength?: number,
match?: RegExp,
notEmpty?: boolean,
}
interface simpleStringRule extends basicStringRule {
equal?: string,
}
interface complexStringRule<T> extends basicStringRule {
equal?: T,
}
// interfaces for implementation
interface intRule extends basicNumberRule {
type: 'int',
}
interface booleanRule extends basicRule<boolean> {
type: 'boolean',
toBe?: boolean,
}
interface stringRule extends simpleStringRule {
type: 'string',
}
interface emailRule extends simpleStringRule {
type: 'email',
user?: (user: string) => boolean,
domain?: (domain: string) => boolean,
}
interface passwordRule extends simpleStringRule {
type: 'password',
uppercase?: number,
specialChars?: number,
numbers?: number,
matchesOneOf?: Array<string|number>,
matchesAllOf?: Array<string|number>,
}
interface urlRule extends simpleStringRule {
type: 'url',
protocol?: (protocol: string) => boolean,
domain?: (domain: string) => boolean,
}
interface dateRule extends basicRule<Date> {
type: 'date',
after?: Date,
before?: Date,
between?: [Date, Date],
equal?: Date,
}
interface floatRule extends basicNumberRule {
type: 'float',
}
interface arrayRule extends basicRule<any> {
type: 'array',
of?: Rule,
notEmpty?: boolean,
length?: number,
}
interface functionRule extends basicRule<Function> {
type: 'function',
result?: {
of: any,
toBe: Rule,
},
}
interface stringIntRule extends complexStringRule<number> {
type: 'string-int',
min?: number,
max?: number,
}
interface stringFloatRule extends complexStringRule<number> {
type: 'string-float',
min?: number,
max?: number,
}
interface stringDateRule extends complexStringRule<Date> {
type: 'string-date',
after?: Date,
before?: Date,
between?: [Date, Date],
}
interface stringBooleanRule extends complexStringRule<boolean> {
type: 'string-boolean',
toBe?: boolean,
}
// There is no regex-validated string type, so make multipleRule is not easy
// interface multipleRule {}
type ruleType = intRule
|booleanRule
|stringRule
|emailRule
|passwordRule
|urlRule
|dateRule
|floatRule
|arrayRule
|functionRule
|stringIntRule
|stringFloatRule
|stringDateRule
|stringBooleanRule;
export declare class Rule {
public constructor (rule: ruleType|string, error?: string|null|undefined);
public static addCustom(name: string, rules: {[key: string]: (val: any, arg?: any) => boolean}): void;
public test(value: any): boolean;
}
export declare class Validator {
public constructor (fields: {[key: string]: Rule});
public test(object: {[key: string]: any}): boolean;
public testAll(objects: Array< {[key: string]: any} >): number;
public getErrors(object: {[key: string]: any}): Array<string>;
}