/
rules.ts
194 lines (181 loc) · 4.68 KB
/
rules.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import * as Validator from "validator";
interface IRuleConfig {
value: string;
validate: (param: any) => (value: any) => boolean;
message: string;
}
interface IRules {
[key: string]: IRuleConfig;
}
function isNullOrUndefined(v: any) {
return v === undefined || v === null;
}
const Rules: IRules = {
required: {
value: "boolean",
validate: (param: any) => {
return (value: any) => {
const isNull = isNullOrUndefined(value) || value === "";
return !(param && isNull);
};
},
message: "[NAME] is required."
},
requiredAllowEmptyString: {
value: "boolean",
validate: (param: any) => {
return (value: any) => {
return !(param && isNullOrUndefined(value));
};
},
message: "[NAME] is required."
},
min: {
value: "number",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value)) {
return true;
}
return value >= param;
};
},
message: "[NAME] must be equal or greater than [PARAM]."
},
max: {
value: "number",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value)) {
return true;
}
return value <= param;
};
},
message: "[NAME] must be equal or less than [PARAM]."
},
minlength: {
value: "number",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value)) {
return true;
}
return String(value).length >= param;
};
},
message: "The length of [NAME] must be equal or greater than [PARAM]."
},
maxlength: {
value: "number",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value)) {
return true;
}
return String(value).length <= param;
};
},
message: "The length of [NAME] must be equal or less than [PARAM]."
},
pattern: {
value: "string",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value)) {
return true;
}
const regex = new RegExp(param);
return regex.test(value);
};
},
message: "[NAME] must be match with pattern /[PARAM]/."
},
email: {
value: "boolean",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value) || !param) {
return true;
}
return Validator.isEmail(String(value));
};
},
message: "[NAME] must be valid email format."
},
url: {
value: "boolean",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value) || !param) {
return true;
}
return Validator.isURL(String(value), {
require_tld: false
});
};
},
message: "[NAME] must be valid url format."
},
list: {
value: "array",
validate: (param: any) => {
return (value: any) => {
if (isNullOrUndefined(value)) {
return true;
}
const array = param as Array<any>;
return array.indexOf(value) !== -1;
};
},
message: "[NAME] must be one of [[PARAM]]."
}
};
export class Rule {
constructor(private _name: string, private _param: any, private config: IRuleConfig) {
}
public message(key: string, value: any): string {
let ret = this.config.message;
ret = ret.replace("[NAME]", key);
ret = ret.replace("[PARAM]", this.param);
ret = ret.replace("[VALUE]", value);
return ret;
}
public get name() { return this._name; }
public get param() { return this._param; }
public validate(value: any, data: any, reqData?: any): boolean {
let ruleParam = this.param;
if (typeof(ruleParam) === "function") {
ruleParam = ruleParam(data, reqData);
}
const func = this.config.validate(ruleParam);
return func(value);
}
public isRequired(): boolean {
return this.name === "required" || this.name === "requiredAllowEmptyString";
}
}
export class RuleFactory {
public test(name: string, param: any): void {
let type: string = typeof(param);
const config: IRuleConfig = Rules[name];
if (type === "object" && Array.isArray(param)) {
type = "array";
}
if (!config) {
throw new Error("Unknown rule: " + name);
}
if (type !== config.value && type !== "function") {
throw new Error(`rules.${name} must be function or ${config.value}`);
}
}
public hasRule(name: string): boolean {
return !!Rules[name];
}
public getRuleConfig(name: string): IRuleConfig {
return Rules[name];
}
public newInstance(name: string, param: any) {
return new Rule(name, param, Rules[name]);
}
}