@aomex/common-validator 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/dist/index.d.ts +858 -0
- package/dist/index.js +1561 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1561 @@
|
|
|
1
|
+
// src/i18n/index.ts
|
|
2
|
+
import { I18n as I18n3 } from "@aomex/internal-tools";
|
|
3
|
+
|
|
4
|
+
// src/i18n/locales/zh-cn.ts
|
|
5
|
+
import { I18n } from "@aomex/internal-tools";
|
|
6
|
+
var zh = I18n.define({
|
|
7
|
+
validator: {
|
|
8
|
+
required: "{{label}}\uFF1A\u5FC5\u586B",
|
|
9
|
+
validation_failed: "\u9A8C\u8BC1\u5931\u8D25\uFF1A",
|
|
10
|
+
array: {
|
|
11
|
+
must_be_array: "{{label}}\uFF1A\u5FC5\u987B\u662F\u6570\u7EC4\u7C7B\u578B",
|
|
12
|
+
length_not_in_range: "{{label}}\uFF1A\u6570\u7EC4\u957F\u5EA6\u4E0D\u5728\u6307\u5B9A\u8303\u56F4\u5185"
|
|
13
|
+
},
|
|
14
|
+
boolean: {
|
|
15
|
+
must_be_boolean: "{{label}}\uFF1A\u5FC5\u987B\u662F\u5E03\u5C14\u7C7B\u578B"
|
|
16
|
+
},
|
|
17
|
+
buffer: {
|
|
18
|
+
must_be_buffer: "{{label}}\uFF1A\u5FC5\u987B\u662Fbuffer\u7C7B\u578B"
|
|
19
|
+
},
|
|
20
|
+
stream: {
|
|
21
|
+
must_be_stream: "{{label}}\uFF1A\u5FC5\u987B\u662Fstream\u7C7B\u578B"
|
|
22
|
+
},
|
|
23
|
+
dateTime: {
|
|
24
|
+
must_be_date: "{{label}}\uFF1A\u5FC5\u987B\u662F\u65F6\u95F4\u7C7B\u578B",
|
|
25
|
+
not_in_range: "{{label}}\uFF1A\u4E0D\u5728\u6307\u5B9A\u65F6\u95F4\u5185"
|
|
26
|
+
},
|
|
27
|
+
number: {
|
|
28
|
+
must_be_number: "{{label}}\uFF1A\u5FC5\u987B\u662F\u6570\u5B57\u7C7B\u578B",
|
|
29
|
+
must_be_integer: "{{label}}\uFF1A\u5FC5\u987B\u662F\u6574\u6570",
|
|
30
|
+
must_be_bigint: "{{label}}\uFF1A\u5FC5\u987B\u662F\u5927\u6574\u6570\u7C7B\u578B",
|
|
31
|
+
not_in_range: "{{label}}\uFF1A\u4E0D\u5728\u6307\u5B9A\u7684\u6570\u5B57\u8303\u56F4"
|
|
32
|
+
},
|
|
33
|
+
string: {
|
|
34
|
+
must_be_string: "{{label}}\uFF1A\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u7C7B\u578B",
|
|
35
|
+
must_be_email: "{{label}}\uFF1A\u5FC5\u987B\u662F\u7535\u5B50\u90AE\u4EF6\u683C\u5F0F",
|
|
36
|
+
must_be_hash: "{{label}}\uFF1A\u5FC5\u987B\u662F\u54C8\u5E0C\u683C\u5F0F",
|
|
37
|
+
must_be_ip: "{{label}}\uFF1A\u5FC5\u987B\u662FIP{{versions}}\u5730\u5740",
|
|
38
|
+
must_be_ulid: "{{label}}\uFF1A\u5FC5\u987B\u662FULID\u683C\u5F0F",
|
|
39
|
+
must_be_uuid: "{{label}}\uFF1A\u5FC5\u987B\u662FUUID{{versions}}\u683C\u5F0F",
|
|
40
|
+
length_not_in_range: "{{label}}\uFF1A\u5B57\u7B26\u4E32\u957F\u5EA6\u4E0D\u5408\u6CD5",
|
|
41
|
+
not_match_pattern: "{{label}}\uFF1A\u5B57\u7B26\u4E32\u672A\u5339\u914D\u5230\u89C4\u5219"
|
|
42
|
+
},
|
|
43
|
+
object: {
|
|
44
|
+
must_be_object: "{{label}}\uFF1A\u5FC5\u987B\u662F\u5BF9\u8C61\u7C7B\u578B"
|
|
45
|
+
},
|
|
46
|
+
enum: {
|
|
47
|
+
not_in_range: "{{label}}\uFF1A\u4E0D\u5728\u679A\u4E3E\u8303\u56F4",
|
|
48
|
+
only_support_string_number: "\u679A\u4E3E\u503C\u53EA\u80FD\u662F\u6570\u7EC4\u6216\u8005\u5B57\u7B26\u4E32",
|
|
49
|
+
can_not_be_empty: '\u679A\u4E3E\u503C\u4E0D\u80FD\u662F\u7A7A\u503C\uFF1A"{{item}}"'
|
|
50
|
+
},
|
|
51
|
+
one_of: {
|
|
52
|
+
not_match_rule: "{{label}}\uFF1A\u672A\u5339\u914D\u89C4\u5219",
|
|
53
|
+
match_multiple_rule: "{{label}}\uFF1A\u5339\u914D\u8D85\u8FC71\u4E2A\u89C4\u5219"
|
|
54
|
+
},
|
|
55
|
+
any_of: {
|
|
56
|
+
not_match_rule: "{{label}}\uFF1A\u672A\u5339\u914D\u89C4\u5219"
|
|
57
|
+
},
|
|
58
|
+
all_of: {
|
|
59
|
+
not_match_all: "{{label}}\uFF1A\u672A\u5339\u914D\u6240\u6709\u89C4\u5219"
|
|
60
|
+
},
|
|
61
|
+
url: {
|
|
62
|
+
must_be_url: "{{label}}\uFF1A\u5FC5\u987B\u662FURL\u683C\u5F0F",
|
|
63
|
+
unsupported_scheme: "{{label}}\uFF1AURL\u5305\u542B\u4E0D\u652F\u6301\u7684\u534F\u8BAE\uFF1A{{scheme}}"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// src/i18n/locales/en-us.ts
|
|
69
|
+
import { I18n as I18n2 } from "@aomex/internal-tools";
|
|
70
|
+
var en = I18n2.satisfies(zh).define({
|
|
71
|
+
validator: {
|
|
72
|
+
required: "{{label}}: required",
|
|
73
|
+
validation_failed: "Validate failed: ",
|
|
74
|
+
array: {
|
|
75
|
+
must_be_array: "{{label}}: must be array",
|
|
76
|
+
length_not_in_range: "{{label}}: array length not in range"
|
|
77
|
+
},
|
|
78
|
+
boolean: {
|
|
79
|
+
must_be_boolean: "{{label}}: must be boolean"
|
|
80
|
+
},
|
|
81
|
+
buffer: {
|
|
82
|
+
must_be_buffer: "{{label}}: must be buffer"
|
|
83
|
+
},
|
|
84
|
+
stream: {
|
|
85
|
+
must_be_stream: "{{label}}: must be stream"
|
|
86
|
+
},
|
|
87
|
+
dateTime: {
|
|
88
|
+
must_be_date: "{{label}}: must be date-time",
|
|
89
|
+
not_in_range: "{{label}}: time not in range"
|
|
90
|
+
},
|
|
91
|
+
number: {
|
|
92
|
+
must_be_number: "{{label}}: must be number",
|
|
93
|
+
must_be_integer: "{{label}}: must be integer",
|
|
94
|
+
must_be_bigint: "{{label}}: must be bigint",
|
|
95
|
+
not_in_range: "{{label}}: number not in range"
|
|
96
|
+
},
|
|
97
|
+
string: {
|
|
98
|
+
must_be_string: "{{label}}: must be string",
|
|
99
|
+
must_be_email: "{{label}}: must be email",
|
|
100
|
+
must_be_hash: "{{label}}: must be hash format",
|
|
101
|
+
must_be_ip: "{{label}}: must be IP{{versions}} address",
|
|
102
|
+
must_be_ulid: "{{label}}: must be ULID format",
|
|
103
|
+
must_be_uuid: "{{label}}: must be UUID{{versions}} format",
|
|
104
|
+
length_not_in_range: "{{label}}: string length not in range",
|
|
105
|
+
not_match_pattern: "{{label}}: string not matched pattern"
|
|
106
|
+
},
|
|
107
|
+
object: {
|
|
108
|
+
must_be_object: "{{label}}: must be object"
|
|
109
|
+
},
|
|
110
|
+
enum: {
|
|
111
|
+
not_in_range: "{{label}}: not in enum",
|
|
112
|
+
only_support_string_number: "enum values can be only string or number",
|
|
113
|
+
can_not_be_empty: 'enum values can not be empty: "{{item}}"'
|
|
114
|
+
},
|
|
115
|
+
one_of: {
|
|
116
|
+
not_match_rule: "{{label}}: does not match rule",
|
|
117
|
+
match_multiple_rule: "{{label}}: match more than one rule"
|
|
118
|
+
},
|
|
119
|
+
any_of: {
|
|
120
|
+
not_match_rule: "{{label}}: does not match rule"
|
|
121
|
+
},
|
|
122
|
+
all_of: {
|
|
123
|
+
not_match_all: "{{label}}: does not match all rules"
|
|
124
|
+
},
|
|
125
|
+
url: {
|
|
126
|
+
must_be_url: "{{label}}: must be URL format",
|
|
127
|
+
unsupported_scheme: "{{label}}: URL contains unsupported scheme: {{scheme}}"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// src/i18n/locales/index.ts
|
|
133
|
+
var locales = {
|
|
134
|
+
zh_CN: zh,
|
|
135
|
+
en_US: en
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/i18n/index.ts
|
|
139
|
+
var i18n = new I18n3({
|
|
140
|
+
resources: locales,
|
|
141
|
+
defaultLanguage: "zh_CN"
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// src/base/validate-result.ts
|
|
145
|
+
var ValidateResult = {
|
|
146
|
+
accept: (data) => {
|
|
147
|
+
return { data };
|
|
148
|
+
},
|
|
149
|
+
deny: (message) => {
|
|
150
|
+
return {
|
|
151
|
+
errors: [message]
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
noError: (result) => {
|
|
155
|
+
return "data" in result;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// src/base/validator.ts
|
|
160
|
+
var Validator = class {
|
|
161
|
+
SubClass;
|
|
162
|
+
constructor() {
|
|
163
|
+
this.SubClass = new.target;
|
|
164
|
+
}
|
|
165
|
+
config = {
|
|
166
|
+
required: true,
|
|
167
|
+
nullable: false,
|
|
168
|
+
strict: false
|
|
169
|
+
};
|
|
170
|
+
static toDocument(validator) {
|
|
171
|
+
const {
|
|
172
|
+
required,
|
|
173
|
+
nullable,
|
|
174
|
+
docs: { description, deprecated, example, ...schemaProperties } = {},
|
|
175
|
+
defaultValue
|
|
176
|
+
} = validator.config;
|
|
177
|
+
const bothProperties = { description, deprecated, example };
|
|
178
|
+
const schema = {
|
|
179
|
+
...schemaProperties,
|
|
180
|
+
...bothProperties,
|
|
181
|
+
default: validator.getDefaultValue(defaultValue),
|
|
182
|
+
nullable: nullable || void 0,
|
|
183
|
+
...validator.toDocument()
|
|
184
|
+
};
|
|
185
|
+
const result = {
|
|
186
|
+
...bothProperties,
|
|
187
|
+
required: required || void 0,
|
|
188
|
+
schema
|
|
189
|
+
};
|
|
190
|
+
return JSON.parse(JSON.stringify(result));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* @param is 是否开启,默认值:`true`
|
|
194
|
+
*/
|
|
195
|
+
strict(is = true) {
|
|
196
|
+
const validator = this.copy();
|
|
197
|
+
validator.config.strict = is;
|
|
198
|
+
return validator;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 扩展openapi的配置
|
|
202
|
+
* - **replace:** 本次设置直接覆盖原来的扩展配置
|
|
203
|
+
* - **merge:** 本次设置通过`Object.assign`的形式合并到原来的扩展配置
|
|
204
|
+
* @param mode 默认值:`merge`
|
|
205
|
+
*/
|
|
206
|
+
docs(docs, mode = "merge") {
|
|
207
|
+
const validator = this.copy();
|
|
208
|
+
validator.config.docs = mode === "merge" ? { ...validator.config.docs, ...docs } : docs;
|
|
209
|
+
return validator;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* 值如果是 `undefined, null, ''`或者没传入,以上情况会直接转换成`undefined`
|
|
213
|
+
*
|
|
214
|
+
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
215
|
+
*
|
|
216
|
+
* 注意:如果设置了`nullable()`,则`null`不做处理
|
|
217
|
+
*/
|
|
218
|
+
optional() {
|
|
219
|
+
const validator = this.copy();
|
|
220
|
+
validator.config.required = false;
|
|
221
|
+
return validator;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 把`null`识别成合法的值
|
|
225
|
+
* @see optional
|
|
226
|
+
*/
|
|
227
|
+
nullable() {
|
|
228
|
+
const validator = this.copy();
|
|
229
|
+
validator.config.nullable = true;
|
|
230
|
+
return validator;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 数据验证成功最后会触发该方法,你可以把数据转换成任何格式
|
|
234
|
+
* ```typescript
|
|
235
|
+
* // 最终返回类型:string
|
|
236
|
+
* rule.string()
|
|
237
|
+
* // 最终返回类型:Array<string>
|
|
238
|
+
* rule.string().transform((value) => [value])
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
transform(fn) {
|
|
242
|
+
const validator = this.copy();
|
|
243
|
+
validator.config.transform = fn;
|
|
244
|
+
return validator;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* 如果值为空或者没传入,则使用该默认值,而且无需验证
|
|
248
|
+
*
|
|
249
|
+
* 注意:如果执行了`default(...)`,则无需再执行`optional()`
|
|
250
|
+
*/
|
|
251
|
+
default(value) {
|
|
252
|
+
const validator = this.copy();
|
|
253
|
+
validator.config.required = false;
|
|
254
|
+
validator.config.defaultValue = value;
|
|
255
|
+
return validator;
|
|
256
|
+
}
|
|
257
|
+
async validate(value, key = "", label = "") {
|
|
258
|
+
const { defaultValue, required } = this.config;
|
|
259
|
+
if (this.isEmpty(value)) {
|
|
260
|
+
value = this.getDefaultValue(defaultValue);
|
|
261
|
+
if (this.isEmpty(value)) {
|
|
262
|
+
if (!required)
|
|
263
|
+
return ValidateResult.accept(
|
|
264
|
+
this.config.transform ? await this.config.transform(value) : value
|
|
265
|
+
);
|
|
266
|
+
return ValidateResult.deny(i18n.t("validator.required", { label }));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (this.isValidNull(value)) {
|
|
270
|
+
return ValidateResult.accept(
|
|
271
|
+
this.config.transform ? await this.config.transform(value) : value
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
const result = await this.validateValue(value, key, label);
|
|
275
|
+
if (ValidateResult.noError(result) && this.config.transform) {
|
|
276
|
+
result.data = await this.config.transform(result.data);
|
|
277
|
+
}
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
isEmpty(value) {
|
|
281
|
+
return value === void 0 || !this.config.nullable && value === null || value === "";
|
|
282
|
+
}
|
|
283
|
+
isValidNull(value) {
|
|
284
|
+
return value === null && this.config.nullable;
|
|
285
|
+
}
|
|
286
|
+
getDefaultValue(value) {
|
|
287
|
+
return typeof value === "function" ? value() : value;
|
|
288
|
+
}
|
|
289
|
+
copy() {
|
|
290
|
+
return new this.SubClass().copyConfig(this);
|
|
291
|
+
}
|
|
292
|
+
copyConfig(prev) {
|
|
293
|
+
this.config = { ...prev.config };
|
|
294
|
+
return this;
|
|
295
|
+
}
|
|
296
|
+
toDocument() {
|
|
297
|
+
return {};
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// src/base/base-string.ts
|
|
302
|
+
var BaseStringValidator = class extends Validator {
|
|
303
|
+
/**
|
|
304
|
+
* 删除两边空格后再进行验证
|
|
305
|
+
*/
|
|
306
|
+
trim(is = true) {
|
|
307
|
+
const validator = this.copy();
|
|
308
|
+
validator.config.trim = is;
|
|
309
|
+
return validator;
|
|
310
|
+
}
|
|
311
|
+
length(data) {
|
|
312
|
+
const validator = this.copy();
|
|
313
|
+
validator.config.lengthRange = typeof data === "number" ? { min: data, max: data } : data;
|
|
314
|
+
return validator;
|
|
315
|
+
}
|
|
316
|
+
match(pattern2) {
|
|
317
|
+
const validator = this.copy();
|
|
318
|
+
validator.config.pattern = pattern2;
|
|
319
|
+
return validator;
|
|
320
|
+
}
|
|
321
|
+
isEmpty(value) {
|
|
322
|
+
if (value !== "" && super.isEmpty(value)) return true;
|
|
323
|
+
if (typeof value !== "string") return false;
|
|
324
|
+
if (value === "" || this.getTrimValue(value) === "") return true;
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
getTrimValue(value) {
|
|
328
|
+
return this.config.trim ? value.trim() : value;
|
|
329
|
+
}
|
|
330
|
+
validateValue(value, key, label) {
|
|
331
|
+
const { pattern: pattern2, lengthRange } = this.config;
|
|
332
|
+
if (typeof value !== "string") {
|
|
333
|
+
return ValidateResult.deny(i18n.t("validator.string.must_be_string", { label }));
|
|
334
|
+
}
|
|
335
|
+
value = this.getTrimValue(value);
|
|
336
|
+
if (lengthRange) {
|
|
337
|
+
const { min = 0, max = Infinity } = lengthRange;
|
|
338
|
+
const length = value.length;
|
|
339
|
+
if (length < min || length > max) {
|
|
340
|
+
return ValidateResult.deny(
|
|
341
|
+
i18n.t("validator.string.length_not_in_range", { label })
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (pattern2 && !pattern2.test(value)) {
|
|
346
|
+
return ValidateResult.deny(i18n.t("validator.string.not_match_pattern", { label }));
|
|
347
|
+
}
|
|
348
|
+
return this.validateString(value, key, label);
|
|
349
|
+
}
|
|
350
|
+
toDocument() {
|
|
351
|
+
const { lengthRange = {}, pattern: pattern2 } = this.config;
|
|
352
|
+
return {
|
|
353
|
+
type: "string",
|
|
354
|
+
// FIXME: openapi没地方放`pattern.flags`
|
|
355
|
+
pattern: pattern2 ? pattern2.source : void 0,
|
|
356
|
+
minLength: lengthRange.min,
|
|
357
|
+
maxLength: lengthRange.max
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
// src/base/base-number.ts
|
|
363
|
+
var BaseNumberValidator = class extends Validator {
|
|
364
|
+
/**
|
|
365
|
+
* 最小范围限制
|
|
366
|
+
* @param min 最小值
|
|
367
|
+
* @param inclusive 是否包含最小值,默认值:`true`
|
|
368
|
+
*/
|
|
369
|
+
min(min, inclusive = true) {
|
|
370
|
+
const validator = this.copy();
|
|
371
|
+
validator.config.min = min;
|
|
372
|
+
validator.config.minInclusive = inclusive;
|
|
373
|
+
return validator;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* 最大范围限制
|
|
377
|
+
* @param min 最大值
|
|
378
|
+
* @param inclusive 是否包含最大值,默认值:`true`
|
|
379
|
+
*/
|
|
380
|
+
max(max, inclusive = true) {
|
|
381
|
+
const validator = this.copy();
|
|
382
|
+
validator.config.max = max;
|
|
383
|
+
validator.config.maxInclusive = inclusive;
|
|
384
|
+
return validator;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* 小数点保留位数,使用`.toFixed()`剔除多余的小数
|
|
388
|
+
*/
|
|
389
|
+
precision(decimals) {
|
|
390
|
+
const validator = this.copy();
|
|
391
|
+
validator.config.precision = decimals;
|
|
392
|
+
return validator;
|
|
393
|
+
}
|
|
394
|
+
validateValue(num, key, label) {
|
|
395
|
+
const {
|
|
396
|
+
min = -Infinity,
|
|
397
|
+
max = Infinity,
|
|
398
|
+
minInclusive,
|
|
399
|
+
maxInclusive,
|
|
400
|
+
strict,
|
|
401
|
+
precision
|
|
402
|
+
} = this.config;
|
|
403
|
+
if (!strict && typeof num === "string") {
|
|
404
|
+
num = Number(num);
|
|
405
|
+
}
|
|
406
|
+
if (!Number.isFinite(num)) {
|
|
407
|
+
return ValidateResult.deny(i18n.t("validator.number.must_be_number", { label }));
|
|
408
|
+
}
|
|
409
|
+
if (precision !== void 0) {
|
|
410
|
+
num = Number(num.toFixed(precision));
|
|
411
|
+
}
|
|
412
|
+
if ((minInclusive ? num < min : num <= min) || (maxInclusive ? num > max : num >= max)) {
|
|
413
|
+
return ValidateResult.deny(i18n.t("validator.number.not_in_range", { label }));
|
|
414
|
+
}
|
|
415
|
+
return this.validateNumber(num, key, label);
|
|
416
|
+
}
|
|
417
|
+
toDocument() {
|
|
418
|
+
const { max, maxInclusive, min, minInclusive } = this.config;
|
|
419
|
+
return {
|
|
420
|
+
type: "number",
|
|
421
|
+
maximum: max,
|
|
422
|
+
minimum: min,
|
|
423
|
+
exclusiveMaximum: maxInclusive === void 0 ? void 0 : !maxInclusive,
|
|
424
|
+
exclusiveMinimum: minInclusive === void 0 ? void 0 : !minInclusive
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// src/base/validate-denied-error.ts
|
|
430
|
+
var ValidateDeniedError = class extends Error {
|
|
431
|
+
constructor(message, errors = []) {
|
|
432
|
+
super(message);
|
|
433
|
+
this.errors = errors;
|
|
434
|
+
}
|
|
435
|
+
getValidateErrors() {
|
|
436
|
+
return this.errors;
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
// src/validators/all-of.ts
|
|
441
|
+
var AllOfValidator = class extends Validator {
|
|
442
|
+
constructor(validators) {
|
|
443
|
+
super();
|
|
444
|
+
this.config.validators = validators;
|
|
445
|
+
}
|
|
446
|
+
isEmpty(_) {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
async validateValue(value, key, label) {
|
|
450
|
+
const { validators } = this.config;
|
|
451
|
+
for (let i = 0; i < validators.length; ++i) {
|
|
452
|
+
const validator = validators[i];
|
|
453
|
+
const result = await validator.validate(value, key, label);
|
|
454
|
+
if (ValidateResult.noError(result)) {
|
|
455
|
+
value = result.data;
|
|
456
|
+
} else {
|
|
457
|
+
return ValidateResult.deny(i18n.t("validator.all_of.not_match_all", { label }));
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return ValidateResult.accept(value);
|
|
461
|
+
}
|
|
462
|
+
copyConfig(prev) {
|
|
463
|
+
super.copyConfig(prev);
|
|
464
|
+
this.config.validators = [...prev.config.validators];
|
|
465
|
+
return this;
|
|
466
|
+
}
|
|
467
|
+
toDocument() {
|
|
468
|
+
const { validators } = this.config;
|
|
469
|
+
return {
|
|
470
|
+
allOf: validators.map((validator) => Validator.toDocument(validator).schema)
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
// src/validators/any.ts
|
|
476
|
+
var AnyValidator = class extends Validator {
|
|
477
|
+
validateValue(value) {
|
|
478
|
+
return ValidateResult.accept(value);
|
|
479
|
+
}
|
|
480
|
+
toDocument() {
|
|
481
|
+
return {
|
|
482
|
+
anyOf: [
|
|
483
|
+
{ type: "array", items: {} },
|
|
484
|
+
{ type: "boolean" },
|
|
485
|
+
{ type: "integer" },
|
|
486
|
+
{ type: "number" },
|
|
487
|
+
{ type: "object" },
|
|
488
|
+
{ type: "string" }
|
|
489
|
+
]
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// src/validators/any-of.ts
|
|
495
|
+
var AnyOfValidator = class extends Validator {
|
|
496
|
+
constructor(validators) {
|
|
497
|
+
super();
|
|
498
|
+
this.config.validators = validators;
|
|
499
|
+
}
|
|
500
|
+
isEmpty(_) {
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
async validateValue(value, key, label) {
|
|
504
|
+
const { validators } = this.config;
|
|
505
|
+
let matched = false;
|
|
506
|
+
for (let i = 0; i < validators.length; ++i) {
|
|
507
|
+
const validator = validators[i];
|
|
508
|
+
const result = await validator.validate(value, key, label);
|
|
509
|
+
if (ValidateResult.noError(result)) {
|
|
510
|
+
value = result.data;
|
|
511
|
+
matched = true;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return matched ? ValidateResult.accept(value) : ValidateResult.deny(i18n.t("validator.any_of.not_match_rule", { label }));
|
|
515
|
+
}
|
|
516
|
+
copyConfig(prev) {
|
|
517
|
+
super.copyConfig(prev);
|
|
518
|
+
this.config.validators = [...prev.config.validators];
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
toDocument() {
|
|
522
|
+
const { validators } = this.config;
|
|
523
|
+
return {
|
|
524
|
+
anyOf: validators.map((validator) => Validator.toDocument(validator).schema)
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
// src/validators/array.ts
|
|
530
|
+
var ArrayValidator = class extends Validator {
|
|
531
|
+
constructor(validator) {
|
|
532
|
+
super();
|
|
533
|
+
this.config.itemValidator = validator;
|
|
534
|
+
this.config.lengthRange = {};
|
|
535
|
+
}
|
|
536
|
+
forceToArray(mode, separator) {
|
|
537
|
+
const validator = this.copy();
|
|
538
|
+
validator.config.force = { mode, separator };
|
|
539
|
+
return validator;
|
|
540
|
+
}
|
|
541
|
+
length(data) {
|
|
542
|
+
const validator = this.copy();
|
|
543
|
+
validator.config.lengthRange = typeof data === "number" ? { min: data, max: data } : data;
|
|
544
|
+
return validator;
|
|
545
|
+
}
|
|
546
|
+
async validateValue(value, key, label) {
|
|
547
|
+
const {
|
|
548
|
+
force,
|
|
549
|
+
lengthRange: { min = 0, max = Infinity },
|
|
550
|
+
itemValidator
|
|
551
|
+
} = this.config;
|
|
552
|
+
let items;
|
|
553
|
+
if (Array.isArray(value)) {
|
|
554
|
+
items = value.slice();
|
|
555
|
+
} else if (force) {
|
|
556
|
+
const { mode, separator = /\s*,\s*/ } = force;
|
|
557
|
+
switch (mode) {
|
|
558
|
+
case "block":
|
|
559
|
+
items = [value];
|
|
560
|
+
break;
|
|
561
|
+
case "separator":
|
|
562
|
+
if (typeof value === "string") {
|
|
563
|
+
items = value.split(separator);
|
|
564
|
+
}
|
|
565
|
+
break;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
if (!Array.isArray(items)) {
|
|
569
|
+
return ValidateResult.deny(i18n.t("validator.array.must_be_array", { label }));
|
|
570
|
+
}
|
|
571
|
+
const length = items.length;
|
|
572
|
+
if (length < min || length > max) {
|
|
573
|
+
return ValidateResult.deny(
|
|
574
|
+
i18n.t("validator.array.length_not_in_range", { label })
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
if (itemValidator) {
|
|
578
|
+
let error = {
|
|
579
|
+
errors: []
|
|
580
|
+
};
|
|
581
|
+
await Promise.all(
|
|
582
|
+
items.map(async (_, index, arr) => {
|
|
583
|
+
const result = await itemValidator["validate"](
|
|
584
|
+
arr[index],
|
|
585
|
+
index.toString(),
|
|
586
|
+
label && key ? `${label}.${index}` : index.toString()
|
|
587
|
+
);
|
|
588
|
+
if (ValidateResult.noError(result)) {
|
|
589
|
+
arr[index] = result.data;
|
|
590
|
+
} else {
|
|
591
|
+
error.errors = error.errors.concat(result.errors);
|
|
592
|
+
}
|
|
593
|
+
})
|
|
594
|
+
);
|
|
595
|
+
if (error.errors.length) return error;
|
|
596
|
+
}
|
|
597
|
+
return ValidateResult.accept(items);
|
|
598
|
+
}
|
|
599
|
+
toDocument() {
|
|
600
|
+
const { itemValidator, lengthRange } = this.config;
|
|
601
|
+
return {
|
|
602
|
+
type: "array",
|
|
603
|
+
items: itemValidator && Validator.toDocument(itemValidator).schema || {},
|
|
604
|
+
minItems: lengthRange.min,
|
|
605
|
+
maxItems: lengthRange.max
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
// src/validators/bigInt.ts
|
|
611
|
+
var BigIntValidator = class extends Validator {
|
|
612
|
+
min(min, inclusive = true) {
|
|
613
|
+
const validator = this.copy();
|
|
614
|
+
validator.config.min = min;
|
|
615
|
+
validator.config.minInclusive = inclusive;
|
|
616
|
+
return validator;
|
|
617
|
+
}
|
|
618
|
+
max(max, inclusive = true) {
|
|
619
|
+
const validator = this.copy();
|
|
620
|
+
validator.config.max = max;
|
|
621
|
+
validator.config.maxInclusive = inclusive;
|
|
622
|
+
return validator;
|
|
623
|
+
}
|
|
624
|
+
validateValue(value, _key, label) {
|
|
625
|
+
const {
|
|
626
|
+
strict,
|
|
627
|
+
min = -Infinity,
|
|
628
|
+
max = Infinity,
|
|
629
|
+
minInclusive,
|
|
630
|
+
maxInclusive
|
|
631
|
+
} = this.config;
|
|
632
|
+
const type = typeof value;
|
|
633
|
+
let num;
|
|
634
|
+
if (type !== "bigint") {
|
|
635
|
+
if (!strict && (type === "number" || type === "string")) {
|
|
636
|
+
try {
|
|
637
|
+
num = BigInt(value);
|
|
638
|
+
} catch {
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
if (num === void 0) {
|
|
642
|
+
return ValidateResult.deny(i18n.t("validator.number.must_be_bigint", { label }));
|
|
643
|
+
}
|
|
644
|
+
} else {
|
|
645
|
+
num = value;
|
|
646
|
+
}
|
|
647
|
+
if ((minInclusive ? num < min : num <= min) || (maxInclusive ? num > max : num >= max)) {
|
|
648
|
+
return ValidateResult.deny(i18n.t("validator.number.not_in_range", { label }));
|
|
649
|
+
}
|
|
650
|
+
return ValidateResult.accept(num);
|
|
651
|
+
}
|
|
652
|
+
toDocument() {
|
|
653
|
+
const { min, max, minInclusive, maxInclusive } = this.config;
|
|
654
|
+
const defaultValue = this.getDefaultValue(this.config.defaultValue);
|
|
655
|
+
return {
|
|
656
|
+
type: "integer",
|
|
657
|
+
format: "int64",
|
|
658
|
+
example: "922337203685475807",
|
|
659
|
+
// JSON.stringify 无法处理bigint类型
|
|
660
|
+
default: defaultValue === void 0 ? void 0 : Number(defaultValue),
|
|
661
|
+
minimum: min === void 0 ? void 0 : Number(min),
|
|
662
|
+
maximum: max === void 0 ? void 0 : Number(max),
|
|
663
|
+
exclusiveMinimum: minInclusive === void 0 ? void 0 : !minInclusive,
|
|
664
|
+
exclusiveMaximum: maxInclusive === void 0 ? void 0 : !maxInclusive
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
// src/validators/boolean.ts
|
|
670
|
+
var BooleanValidator = class _BooleanValidator extends Validator {
|
|
671
|
+
static defaultTruthyValues = [1, "1", true, "true"];
|
|
672
|
+
static defaultFalsyValues = [0, "0", false, "false"];
|
|
673
|
+
/**
|
|
674
|
+
* 重新设置真值。默认值:`[1, '1', true, 'true']`
|
|
675
|
+
* ```typescript
|
|
676
|
+
* boolean.setTruthyValues([true, 'yes']);
|
|
677
|
+
* boolean.setTruthyValues(['on', 'good']);
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
setTruthyValues(values) {
|
|
681
|
+
const validator = this.copy();
|
|
682
|
+
validator.config.truthyValues = values;
|
|
683
|
+
return validator;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* 重新设置假值。默认值:`[0, '0', false, 'false']`
|
|
687
|
+
* ```typescript
|
|
688
|
+
* boolean.setFalsyValues([false, 'no']);
|
|
689
|
+
* boolean.setFalsyValues(['off', 'bad']);
|
|
690
|
+
* ```
|
|
691
|
+
*/
|
|
692
|
+
setFalsyValues(values) {
|
|
693
|
+
const validator = this.copy();
|
|
694
|
+
validator.config.falsyValues = values;
|
|
695
|
+
return validator;
|
|
696
|
+
}
|
|
697
|
+
validateValue(value, _key, label) {
|
|
698
|
+
const {
|
|
699
|
+
truthyValues: trueValues = _BooleanValidator.defaultTruthyValues,
|
|
700
|
+
falsyValues: falseValues = _BooleanValidator.defaultFalsyValues
|
|
701
|
+
} = this.config;
|
|
702
|
+
if (trueValues.includes(value)) {
|
|
703
|
+
return ValidateResult.accept(true);
|
|
704
|
+
}
|
|
705
|
+
if (falseValues.includes(value)) {
|
|
706
|
+
return ValidateResult.accept(false);
|
|
707
|
+
}
|
|
708
|
+
return ValidateResult.deny(i18n.t("validator.boolean.must_be_boolean", { label }));
|
|
709
|
+
}
|
|
710
|
+
toDocument() {
|
|
711
|
+
return {
|
|
712
|
+
type: "boolean"
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
// src/validators/buffer.ts
|
|
718
|
+
var hexReg = /^(?:0x)?[0-9a-f]+$/i;
|
|
719
|
+
var base64Reg = /^([a-z0-9+/]{4})*([a-z0-9+/]{3}=|[a-z0-9+/]{2}==)?$/i;
|
|
720
|
+
var BufferValidator = class extends Validator {
|
|
721
|
+
/**
|
|
722
|
+
* 尝试从其他类型恢复为buffer类型:
|
|
723
|
+
*
|
|
724
|
+
* - `hex` 从十六进制字符串恢复
|
|
725
|
+
* - `base64` 从base64字符串恢复
|
|
726
|
+
*/
|
|
727
|
+
parseFrom(...encodings) {
|
|
728
|
+
const validator = this.copy();
|
|
729
|
+
validator.config.fromEncodings = [...new Set(encodings)];
|
|
730
|
+
return validator;
|
|
731
|
+
}
|
|
732
|
+
validateValue(value, _key, label) {
|
|
733
|
+
const { fromEncodings = [] } = this.config;
|
|
734
|
+
let buffer;
|
|
735
|
+
if (!Buffer.isBuffer(value)) {
|
|
736
|
+
loop: for (const encoding of fromEncodings) {
|
|
737
|
+
switch (encoding) {
|
|
738
|
+
case "hex":
|
|
739
|
+
if (typeof value === "string" && hexReg.test(value)) {
|
|
740
|
+
try {
|
|
741
|
+
buffer = Buffer.from(value.replace(/^0x/i, ""), "hex");
|
|
742
|
+
break loop;
|
|
743
|
+
} catch {
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
break;
|
|
747
|
+
case "base64":
|
|
748
|
+
if (typeof value === "string" && base64Reg.test(value)) {
|
|
749
|
+
try {
|
|
750
|
+
buffer = Buffer.from(value, "base64");
|
|
751
|
+
break loop;
|
|
752
|
+
} catch {
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
} else {
|
|
758
|
+
buffer = value;
|
|
759
|
+
}
|
|
760
|
+
if (!buffer) {
|
|
761
|
+
return ValidateResult.deny(i18n.t("validator.buffer.must_be_buffer", { label }));
|
|
762
|
+
}
|
|
763
|
+
return ValidateResult.accept(buffer);
|
|
764
|
+
}
|
|
765
|
+
toDocument() {
|
|
766
|
+
const defaultValue = this.getDefaultValue(
|
|
767
|
+
this.config.defaultValue
|
|
768
|
+
);
|
|
769
|
+
return {
|
|
770
|
+
type: "string",
|
|
771
|
+
format: "byte",
|
|
772
|
+
// JSON 无法处理buffer类型
|
|
773
|
+
default: defaultValue && defaultValue.toString("base64")
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// src/validators/date-time.ts
|
|
779
|
+
import { DateTime } from "luxon";
|
|
780
|
+
var regTimestamp = /^[0-9.]+$/;
|
|
781
|
+
var regUnixTimeWithMS = /^[0-9]{10}\.[0-9]{3}$/;
|
|
782
|
+
var DateTimeValidator = class extends Validator {
|
|
783
|
+
constructor(formats) {
|
|
784
|
+
super();
|
|
785
|
+
this.config.formats = formats;
|
|
786
|
+
}
|
|
787
|
+
min(freshDate, inclusive = true) {
|
|
788
|
+
const validator = this.copy();
|
|
789
|
+
validator.config.min = freshDate;
|
|
790
|
+
validator.config.minInclusive = inclusive;
|
|
791
|
+
return validator;
|
|
792
|
+
}
|
|
793
|
+
max(freshDate, inclusive = true) {
|
|
794
|
+
const validator = this.copy();
|
|
795
|
+
validator.config.max = freshDate;
|
|
796
|
+
validator.config.maxInclusive = inclusive;
|
|
797
|
+
return validator;
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* 尝试把时间戳数字解析成时间对象。默认已开启
|
|
801
|
+
*
|
|
802
|
+
* 支持如下格式:
|
|
803
|
+
* - 13位:1711257956199
|
|
804
|
+
* - 14位:1711257956.199
|
|
805
|
+
* - 10位:1711257956
|
|
806
|
+
*/
|
|
807
|
+
parseFromTimestamp(is = true) {
|
|
808
|
+
const validator = this.copy();
|
|
809
|
+
validator.config.parseFromTimestamp = is;
|
|
810
|
+
return validator;
|
|
811
|
+
}
|
|
812
|
+
validateValue(value, _key, label) {
|
|
813
|
+
const date = this.toDate(value);
|
|
814
|
+
if (date === false || date.toString() === "Invalid Date") {
|
|
815
|
+
return ValidateResult.deny(i18n.t("validator.dateTime.must_be_date", { label }));
|
|
816
|
+
}
|
|
817
|
+
if (!this.compare(date)) {
|
|
818
|
+
return ValidateResult.deny(i18n.t("validator.dateTime.not_in_range", { label }));
|
|
819
|
+
}
|
|
820
|
+
return ValidateResult.accept(date);
|
|
821
|
+
}
|
|
822
|
+
toDate(value) {
|
|
823
|
+
const { parseFromTimestamp = true, formats } = this.config;
|
|
824
|
+
if (value instanceof Date) return value;
|
|
825
|
+
if (parseFromTimestamp && (typeof value === "number" || typeof value === "string" && regTimestamp.test(value))) {
|
|
826
|
+
const timestamp = value.toString();
|
|
827
|
+
if (timestamp.length === 10)
|
|
828
|
+
return DateTime.fromSeconds(Number(timestamp)).toJSDate();
|
|
829
|
+
if (timestamp.length === 13)
|
|
830
|
+
return DateTime.fromMillis(Number(timestamp)).toJSDate();
|
|
831
|
+
if (timestamp.length === 14 && regUnixTimeWithMS.test(timestamp)) {
|
|
832
|
+
return DateTime.fromSeconds(Number(timestamp)).toJSDate();
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if (typeof value === "string") {
|
|
836
|
+
if (formats && formats.length) {
|
|
837
|
+
for (const format of formats) {
|
|
838
|
+
const date = DateTime.fromFormat(value, format, { setZone: true });
|
|
839
|
+
if (date.isValid) return date.toJSDate();
|
|
840
|
+
}
|
|
841
|
+
} else {
|
|
842
|
+
return DateTime.fromISO(value).toJSDate();
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return false;
|
|
846
|
+
}
|
|
847
|
+
compare(date) {
|
|
848
|
+
const { min, minInclusive, max, maxInclusive } = this.config;
|
|
849
|
+
const timestamp = +date;
|
|
850
|
+
if (min !== void 0 && (minInclusive ? timestamp < +min() : timestamp <= +min())) {
|
|
851
|
+
return false;
|
|
852
|
+
}
|
|
853
|
+
if (max !== void 0 && (maxInclusive ? timestamp > +max() : timestamp >= +max())) {
|
|
854
|
+
return false;
|
|
855
|
+
}
|
|
856
|
+
return true;
|
|
857
|
+
}
|
|
858
|
+
toDocument() {
|
|
859
|
+
const defaultValue = this.getDefaultValue(this.config.defaultValue);
|
|
860
|
+
return {
|
|
861
|
+
type: "string",
|
|
862
|
+
format: "date-time",
|
|
863
|
+
default: defaultValue?.toISOString()
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
// src/validators/email.ts
|
|
869
|
+
import emailValidator from "email-validator";
|
|
870
|
+
var EmailValidator = class extends BaseStringValidator {
|
|
871
|
+
validateString(email, _key, label) {
|
|
872
|
+
if (!emailValidator.validate(email)) {
|
|
873
|
+
return ValidateResult.deny(i18n.t("validator.string.must_be_email", { label }));
|
|
874
|
+
}
|
|
875
|
+
return ValidateResult.accept(email);
|
|
876
|
+
}
|
|
877
|
+
toDocument() {
|
|
878
|
+
return {
|
|
879
|
+
...super.toDocument(),
|
|
880
|
+
format: "email"
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
// src/validators/enum.ts
|
|
886
|
+
var EnumValidator = class extends Validator {
|
|
887
|
+
constructor(ranges = []) {
|
|
888
|
+
super();
|
|
889
|
+
this.config.ranges = ranges;
|
|
890
|
+
ranges.forEach((item) => {
|
|
891
|
+
if (this.isEmpty(item)) {
|
|
892
|
+
throw new Error(i18n.t("validator.enum.can_not_be_empty", { item }));
|
|
893
|
+
}
|
|
894
|
+
if (typeof item !== "string" && typeof item !== "number") {
|
|
895
|
+
throw new Error(i18n.t("validator.enum.only_support_string_number"));
|
|
896
|
+
}
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
validateValue(value, _key, label) {
|
|
900
|
+
const { ranges, strict } = this.config;
|
|
901
|
+
if (ranges.includes(value)) return ValidateResult.accept(value);
|
|
902
|
+
if (!strict && typeof value === "string") {
|
|
903
|
+
const num = Number(value);
|
|
904
|
+
if (!Number.isNaN(num)) {
|
|
905
|
+
const matched = ranges.find(
|
|
906
|
+
(item) => typeof item === "number" ? item === num : false
|
|
907
|
+
);
|
|
908
|
+
if (matched) return ValidateResult.accept(matched);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
return ValidateResult.deny(i18n.t("validator.enum.not_in_range", { label }));
|
|
912
|
+
}
|
|
913
|
+
toDocument() {
|
|
914
|
+
const { ranges } = this.config;
|
|
915
|
+
const types = ranges.map((value) => typeof value);
|
|
916
|
+
return {
|
|
917
|
+
type: new Set(types).size === 1 ? types[0] : void 0,
|
|
918
|
+
enum: ranges
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
};
|
|
922
|
+
|
|
923
|
+
// src/validators/hash.ts
|
|
924
|
+
var HashValidator = class _HashValidator extends BaseStringValidator {
|
|
925
|
+
/**
|
|
926
|
+
* @see https://github.com/validatorjs/validator.js
|
|
927
|
+
*/
|
|
928
|
+
static algorithmLength = {
|
|
929
|
+
md5: 32,
|
|
930
|
+
md4: 32,
|
|
931
|
+
sha1: 40,
|
|
932
|
+
sha256: 64,
|
|
933
|
+
sha384: 96,
|
|
934
|
+
sha512: 128,
|
|
935
|
+
ripemd128: 32,
|
|
936
|
+
ripemd160: 40,
|
|
937
|
+
tiger128: 32,
|
|
938
|
+
tiger160: 40,
|
|
939
|
+
tiger192: 48,
|
|
940
|
+
crc32: 8,
|
|
941
|
+
crc32b: 8
|
|
942
|
+
};
|
|
943
|
+
static algorithmPattern = (() => {
|
|
944
|
+
const patterns = {};
|
|
945
|
+
Object.values(_HashValidator.algorithmLength).forEach((length) => {
|
|
946
|
+
patterns[length] ||= new RegExp(`^[a-f0-9]{${length}}$`, "i");
|
|
947
|
+
});
|
|
948
|
+
return patterns;
|
|
949
|
+
})();
|
|
950
|
+
constructor(algorithm) {
|
|
951
|
+
super();
|
|
952
|
+
this.config.algorithm = algorithm;
|
|
953
|
+
if (!Object.hasOwn(_HashValidator.algorithmLength, algorithm)) {
|
|
954
|
+
throw new Error("\u672A\u77E5\u7684\u54C8\u5E0C\u7C7B\u578B");
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
validateString(hash, _key, label) {
|
|
958
|
+
const { algorithm } = this.config;
|
|
959
|
+
const length = _HashValidator.algorithmLength[algorithm];
|
|
960
|
+
if (!_HashValidator.algorithmPattern[length].test(hash)) {
|
|
961
|
+
return ValidateResult.deny(i18n.t("validator.string.must_be_hash", { label }));
|
|
962
|
+
}
|
|
963
|
+
return ValidateResult.accept(hash);
|
|
964
|
+
}
|
|
965
|
+
toDocument() {
|
|
966
|
+
const { algorithm } = this.config;
|
|
967
|
+
const length = _HashValidator.algorithmLength[algorithm];
|
|
968
|
+
return {
|
|
969
|
+
...super.toDocument(),
|
|
970
|
+
minLength: length,
|
|
971
|
+
maxLength: length,
|
|
972
|
+
format: this.config.algorithm
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
// src/validators/int.ts
|
|
978
|
+
var IntValidator = class extends BaseNumberValidator {
|
|
979
|
+
validateNumber(num, _key, label) {
|
|
980
|
+
if (!Number.isInteger(num)) {
|
|
981
|
+
return ValidateResult.deny(i18n.t("validator.number.must_be_integer", { label }));
|
|
982
|
+
}
|
|
983
|
+
return ValidateResult.accept(num);
|
|
984
|
+
}
|
|
985
|
+
toDocument() {
|
|
986
|
+
return {
|
|
987
|
+
...super.toDocument(),
|
|
988
|
+
type: "integer"
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
// src/validators/ip.ts
|
|
994
|
+
import { isIP, isIPv4, isIPv6 } from "node:net";
|
|
995
|
+
var IpValidator = class extends BaseStringValidator {
|
|
996
|
+
constructor(version) {
|
|
997
|
+
super();
|
|
998
|
+
this.config.ipVersion = [...new Set(version)];
|
|
999
|
+
}
|
|
1000
|
+
validateString(ip, _key, label) {
|
|
1001
|
+
const { ipVersion } = this.config;
|
|
1002
|
+
let valid = false;
|
|
1003
|
+
const hasV4 = ipVersion.includes("v4");
|
|
1004
|
+
const hasV6 = ipVersion.includes("v6");
|
|
1005
|
+
if (hasV4 && hasV6) {
|
|
1006
|
+
valid = isIP(ip) > 0;
|
|
1007
|
+
} else if (hasV4) {
|
|
1008
|
+
valid = isIPv4(ip);
|
|
1009
|
+
} else {
|
|
1010
|
+
valid = isIPv6(ip);
|
|
1011
|
+
}
|
|
1012
|
+
if (!valid) {
|
|
1013
|
+
return ValidateResult.deny(
|
|
1014
|
+
i18n.t("validator.string.must_be_ip", {
|
|
1015
|
+
label,
|
|
1016
|
+
versions: ipVersion.toString()
|
|
1017
|
+
})
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
return ValidateResult.accept(ip);
|
|
1021
|
+
}
|
|
1022
|
+
toDocument() {
|
|
1023
|
+
const { ipVersion } = this.config;
|
|
1024
|
+
return {
|
|
1025
|
+
...super.toDocument(),
|
|
1026
|
+
format: ipVersion.length === 1 ? `ip${ipVersion[0]}` : "ip"
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
|
|
1031
|
+
// src/validators/number.ts
|
|
1032
|
+
var NumberValidator = class extends BaseNumberValidator {
|
|
1033
|
+
validateNumber(num) {
|
|
1034
|
+
return ValidateResult.accept(num);
|
|
1035
|
+
}
|
|
1036
|
+
toDocument() {
|
|
1037
|
+
return {
|
|
1038
|
+
...super.toDocument(),
|
|
1039
|
+
type: "number"
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
|
|
1044
|
+
// src/validators/object.ts
|
|
1045
|
+
import { toArray } from "@aomex/internal-tools";
|
|
1046
|
+
var ObjectValidator = class extends Validator {
|
|
1047
|
+
constructor(properties) {
|
|
1048
|
+
super();
|
|
1049
|
+
this.config.properties = properties;
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* 尝试使用`JSON.parse`把字符串转换为对象,在非严格模式下默认开启
|
|
1053
|
+
*/
|
|
1054
|
+
parseFromString(is = true) {
|
|
1055
|
+
const validator = this.copy();
|
|
1056
|
+
validator.config.stringToObject = is;
|
|
1057
|
+
return validator;
|
|
1058
|
+
}
|
|
1059
|
+
additional(opts = {}) {
|
|
1060
|
+
const validator = this.copy();
|
|
1061
|
+
const keys = opts.key ? toArray(opts.key) : [];
|
|
1062
|
+
const key = keys.length ? keys : void 0;
|
|
1063
|
+
const value = opts.value;
|
|
1064
|
+
validator.config.keepAdditional = !value && !key ? true : { key, value };
|
|
1065
|
+
return validator;
|
|
1066
|
+
}
|
|
1067
|
+
isPlainObject(value) {
|
|
1068
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
1069
|
+
}
|
|
1070
|
+
async validateValue(origin, key, label) {
|
|
1071
|
+
const { properties, stringToObject, strict, keepAdditional = false } = this.config;
|
|
1072
|
+
if (!this.isPlainObject(origin)) {
|
|
1073
|
+
let valid = false;
|
|
1074
|
+
if ((!strict && stringToObject !== false || strict && stringToObject) && typeof origin === "string") {
|
|
1075
|
+
try {
|
|
1076
|
+
origin = JSON.parse(origin);
|
|
1077
|
+
valid = this.isPlainObject(origin);
|
|
1078
|
+
} catch {
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
if (!valid) {
|
|
1082
|
+
return ValidateResult.deny(i18n.t("validator.object.must_be_object", { label }));
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
let obj = {};
|
|
1086
|
+
if (properties) {
|
|
1087
|
+
const error = {
|
|
1088
|
+
errors: []
|
|
1089
|
+
};
|
|
1090
|
+
await Promise.all(
|
|
1091
|
+
Object.entries(properties).map(async ([propKey, validator]) => {
|
|
1092
|
+
const result = await validator["validate"](
|
|
1093
|
+
origin[propKey],
|
|
1094
|
+
propKey,
|
|
1095
|
+
label && key ? `${label}.${propKey}` : propKey
|
|
1096
|
+
);
|
|
1097
|
+
if (ValidateResult.noError(result)) {
|
|
1098
|
+
obj[propKey] = result.data;
|
|
1099
|
+
} else {
|
|
1100
|
+
error.errors = error.errors.concat(result.errors);
|
|
1101
|
+
}
|
|
1102
|
+
})
|
|
1103
|
+
);
|
|
1104
|
+
if (error.errors.length) return error;
|
|
1105
|
+
}
|
|
1106
|
+
if (keepAdditional) {
|
|
1107
|
+
const trustedKeys = properties ? Object.keys(properties) : [];
|
|
1108
|
+
for (const propKey of Object.keys(origin)) {
|
|
1109
|
+
if (trustedKeys.includes(propKey)) continue;
|
|
1110
|
+
if (keepAdditional === true) {
|
|
1111
|
+
obj[propKey] = origin[propKey];
|
|
1112
|
+
continue;
|
|
1113
|
+
}
|
|
1114
|
+
if (keepAdditional.key) {
|
|
1115
|
+
if (keepAdditional.key.every((reg) => !reg.test(propKey))) continue;
|
|
1116
|
+
}
|
|
1117
|
+
if (keepAdditional.value) {
|
|
1118
|
+
const result = await keepAdditional.value["validate"](origin[propKey], propKey);
|
|
1119
|
+
if (ValidateResult.noError(result)) {
|
|
1120
|
+
obj[propKey] = result.data;
|
|
1121
|
+
}
|
|
1122
|
+
} else {
|
|
1123
|
+
obj[propKey] = origin[propKey];
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
if (!properties && !keepAdditional) {
|
|
1128
|
+
Object.assign(obj, origin);
|
|
1129
|
+
}
|
|
1130
|
+
return ValidateResult.accept(obj);
|
|
1131
|
+
}
|
|
1132
|
+
copyConfig(prev) {
|
|
1133
|
+
super.copyConfig(prev);
|
|
1134
|
+
this.config.properties = prev.config.properties && {
|
|
1135
|
+
...prev.config.properties
|
|
1136
|
+
};
|
|
1137
|
+
return this;
|
|
1138
|
+
}
|
|
1139
|
+
toDocument() {
|
|
1140
|
+
const { properties = {}, keepAdditional } = this.config;
|
|
1141
|
+
const hasSpecificProperties = !!Object.keys(properties).length;
|
|
1142
|
+
const schemas = {};
|
|
1143
|
+
const requiredProperties = [];
|
|
1144
|
+
if (hasSpecificProperties) {
|
|
1145
|
+
Object.entries(properties).forEach(([key, validator]) => {
|
|
1146
|
+
const docs = Validator.toDocument(validator);
|
|
1147
|
+
schemas[key] = docs.schema;
|
|
1148
|
+
docs.required && requiredProperties.push(key);
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
let additionalProperties;
|
|
1152
|
+
if (keepAdditional === void 0) {
|
|
1153
|
+
additionalProperties = !hasSpecificProperties;
|
|
1154
|
+
} else if (typeof keepAdditional === "boolean") {
|
|
1155
|
+
additionalProperties = keepAdditional;
|
|
1156
|
+
} else if (!keepAdditional.value) {
|
|
1157
|
+
additionalProperties = true;
|
|
1158
|
+
} else {
|
|
1159
|
+
additionalProperties = Validator.toDocument(keepAdditional.value).schema;
|
|
1160
|
+
}
|
|
1161
|
+
return {
|
|
1162
|
+
type: "object",
|
|
1163
|
+
properties: hasSpecificProperties ? schemas : void 0,
|
|
1164
|
+
required: requiredProperties.length ? requiredProperties : void 0,
|
|
1165
|
+
additionalProperties
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
// src/validators/one-of.ts
|
|
1171
|
+
var OneOfValidator = class extends Validator {
|
|
1172
|
+
constructor(validators) {
|
|
1173
|
+
super();
|
|
1174
|
+
this.config.validators = validators;
|
|
1175
|
+
}
|
|
1176
|
+
isEmpty(_) {
|
|
1177
|
+
return false;
|
|
1178
|
+
}
|
|
1179
|
+
async validateValue(value, key, label) {
|
|
1180
|
+
const { validators } = this.config;
|
|
1181
|
+
let matched;
|
|
1182
|
+
for (let i = 0; i < validators.length; ++i) {
|
|
1183
|
+
const validator = validators[i];
|
|
1184
|
+
const result = await validator.validate(value, key, label);
|
|
1185
|
+
if (ValidateResult.noError(result)) {
|
|
1186
|
+
if (!matched) {
|
|
1187
|
+
matched = result;
|
|
1188
|
+
} else {
|
|
1189
|
+
return ValidateResult.deny(
|
|
1190
|
+
i18n.t("validator.one_of.match_multiple_rule", { label })
|
|
1191
|
+
);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
return matched || ValidateResult.deny(i18n.t("validator.one_of.not_match_rule", { label }));
|
|
1196
|
+
}
|
|
1197
|
+
copyConfig(prev) {
|
|
1198
|
+
super.copyConfig(prev);
|
|
1199
|
+
this.config.validators = [...prev.config.validators];
|
|
1200
|
+
return this;
|
|
1201
|
+
}
|
|
1202
|
+
toDocument() {
|
|
1203
|
+
const { validators } = this.config;
|
|
1204
|
+
return {
|
|
1205
|
+
oneOf: validators.map((validator) => Validator.toDocument(validator).schema)
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
// src/validators/stream.ts
|
|
1211
|
+
import stream from "node:stream";
|
|
1212
|
+
var StreamValidator = class extends Validator {
|
|
1213
|
+
validateValue(value, _key, label) {
|
|
1214
|
+
if (!(value instanceof stream.Stream)) {
|
|
1215
|
+
return ValidateResult.deny(i18n.t("validator.stream.must_be_stream", { label }));
|
|
1216
|
+
}
|
|
1217
|
+
return ValidateResult.accept(value);
|
|
1218
|
+
}
|
|
1219
|
+
toDocument() {
|
|
1220
|
+
return {
|
|
1221
|
+
type: "string",
|
|
1222
|
+
format: "binary"
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
|
|
1227
|
+
// src/validators/string.ts
|
|
1228
|
+
var StringValidator = class extends BaseStringValidator {
|
|
1229
|
+
/**
|
|
1230
|
+
* 把空字符串`''`设置为合法的值
|
|
1231
|
+
*/
|
|
1232
|
+
allowEmptyString() {
|
|
1233
|
+
const validator = this.copy();
|
|
1234
|
+
validator.config.allowEmpty = true;
|
|
1235
|
+
return validator;
|
|
1236
|
+
}
|
|
1237
|
+
isEmpty(value) {
|
|
1238
|
+
if (value === "" && this.config.allowEmpty) return false;
|
|
1239
|
+
return super.isEmpty(value);
|
|
1240
|
+
}
|
|
1241
|
+
validateString(value) {
|
|
1242
|
+
return ValidateResult.accept(value);
|
|
1243
|
+
}
|
|
1244
|
+
toDocument() {
|
|
1245
|
+
return super.toDocument();
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
|
|
1249
|
+
// src/validators/ulid.ts
|
|
1250
|
+
var pattern = /^[0-7][0-9A-HJKMNP-TV-Z]{25}$/;
|
|
1251
|
+
var UlidValidator = class extends BaseStringValidator {
|
|
1252
|
+
validateString(ulid, _key, label) {
|
|
1253
|
+
if (!pattern.test(ulid)) {
|
|
1254
|
+
return ValidateResult.deny(i18n.t("validator.string.must_be_ulid", { label }));
|
|
1255
|
+
}
|
|
1256
|
+
return ValidateResult.accept(ulid);
|
|
1257
|
+
}
|
|
1258
|
+
toDocument() {
|
|
1259
|
+
return {
|
|
1260
|
+
example: "01BJQE4QTHMFP0S5J153XCFSP9",
|
|
1261
|
+
...super.toDocument(),
|
|
1262
|
+
format: "ulid"
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
};
|
|
1266
|
+
|
|
1267
|
+
// src/validators/url.ts
|
|
1268
|
+
import { URL } from "node:url";
|
|
1269
|
+
var UrlValidator = class extends BaseStringValidator {
|
|
1270
|
+
constructor() {
|
|
1271
|
+
super();
|
|
1272
|
+
this.config.allowedScheme = ["http", "https"];
|
|
1273
|
+
}
|
|
1274
|
+
/**
|
|
1275
|
+
* 允许使用的协议,不区分大小写。默认配置:`['http', 'https']`
|
|
1276
|
+
*/
|
|
1277
|
+
scheme(scheme) {
|
|
1278
|
+
const validator = this.copy();
|
|
1279
|
+
validator.config.allowedScheme = scheme.map((item) => item.toLowerCase());
|
|
1280
|
+
return validator;
|
|
1281
|
+
}
|
|
1282
|
+
validateString(url, _key, label) {
|
|
1283
|
+
const { allowedScheme } = this.config;
|
|
1284
|
+
if (!/^[a-z0-9]+:\/\/.+/.test(url) || !URL.canParse(url)) {
|
|
1285
|
+
return ValidateResult.deny(i18n.t("validator.url.must_be_url", { label }));
|
|
1286
|
+
}
|
|
1287
|
+
const parsedUrl = new URL(url);
|
|
1288
|
+
const scheme = parsedUrl.protocol.slice(0, -1);
|
|
1289
|
+
if (!allowedScheme.includes(scheme)) {
|
|
1290
|
+
return ValidateResult.deny(
|
|
1291
|
+
i18n.t("validator.url.unsupported_scheme", { label, scheme })
|
|
1292
|
+
);
|
|
1293
|
+
}
|
|
1294
|
+
return ValidateResult.accept(url);
|
|
1295
|
+
}
|
|
1296
|
+
toDocument() {
|
|
1297
|
+
return {
|
|
1298
|
+
example: "https://example.com/path/to?id=1",
|
|
1299
|
+
...super.toDocument(),
|
|
1300
|
+
format: "uri"
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
};
|
|
1304
|
+
|
|
1305
|
+
// src/validators/uuid.ts
|
|
1306
|
+
var createRegexp = (version) => {
|
|
1307
|
+
const group = "[0-9a-f]";
|
|
1308
|
+
const diff = version === "all" ? `${group}{4}` : `${version}${group}{3}`;
|
|
1309
|
+
return new RegExp(
|
|
1310
|
+
`^${group}{8}-${group}{4}-${diff}-[89ab]${group}{3}-${group}{12}$`,
|
|
1311
|
+
"i"
|
|
1312
|
+
);
|
|
1313
|
+
};
|
|
1314
|
+
var UuidValidator = class _UuidValidator extends BaseStringValidator {
|
|
1315
|
+
static versions = ["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"];
|
|
1316
|
+
static patterns = {
|
|
1317
|
+
v1: createRegexp(1),
|
|
1318
|
+
v2: createRegexp(2),
|
|
1319
|
+
v3: createRegexp(3),
|
|
1320
|
+
v4: createRegexp(4),
|
|
1321
|
+
v5: createRegexp(5),
|
|
1322
|
+
v6: createRegexp(6),
|
|
1323
|
+
v7: createRegexp(7),
|
|
1324
|
+
v8: createRegexp(8),
|
|
1325
|
+
all: createRegexp("all")
|
|
1326
|
+
};
|
|
1327
|
+
constructor(versions) {
|
|
1328
|
+
super();
|
|
1329
|
+
this.config.uuidVersion = [...new Set(versions)];
|
|
1330
|
+
}
|
|
1331
|
+
validateString(uuid, _key, label) {
|
|
1332
|
+
const { uuidVersion } = this.config;
|
|
1333
|
+
let valid = false;
|
|
1334
|
+
if (uuidVersion.length === _UuidValidator.versions.length) {
|
|
1335
|
+
valid = _UuidValidator.patterns.all.test(uuid);
|
|
1336
|
+
} else {
|
|
1337
|
+
for (let i = uuidVersion.length; i-- > 0; ) {
|
|
1338
|
+
if (_UuidValidator.patterns[uuidVersion[i]].test(uuid)) {
|
|
1339
|
+
valid = true;
|
|
1340
|
+
break;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
if (!valid) {
|
|
1345
|
+
return ValidateResult.deny(
|
|
1346
|
+
i18n.t("validator.string.must_be_uuid", {
|
|
1347
|
+
label,
|
|
1348
|
+
versions: uuidVersion.join(",")
|
|
1349
|
+
})
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1352
|
+
return ValidateResult.accept(uuid);
|
|
1353
|
+
}
|
|
1354
|
+
toDocument() {
|
|
1355
|
+
return {
|
|
1356
|
+
...super.toDocument(),
|
|
1357
|
+
format: "uuid"
|
|
1358
|
+
};
|
|
1359
|
+
}
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
// src/api/to-validator.ts
|
|
1363
|
+
function toValidator(validator) {
|
|
1364
|
+
if (!validator) return;
|
|
1365
|
+
if (validator instanceof Validator) return validator;
|
|
1366
|
+
return new ObjectValidator(validator);
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
// src/api/rule.ts
|
|
1370
|
+
import { toArray as toArray2 } from "@aomex/internal-tools";
|
|
1371
|
+
var Rule = class {
|
|
1372
|
+
static register(name, SubValidator) {
|
|
1373
|
+
this.prototype[name] = () => new SubValidator();
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* 规则以管道形式执行,需要匹配所有规则
|
|
1377
|
+
* ```typescript
|
|
1378
|
+
* rule.allOf([rule.number(), rule.string()]);
|
|
1379
|
+
* ```
|
|
1380
|
+
*/
|
|
1381
|
+
allOf(rules) {
|
|
1382
|
+
return new AllOfValidator(rules);
|
|
1383
|
+
}
|
|
1384
|
+
/**
|
|
1385
|
+
* 允许任何类型:
|
|
1386
|
+
* - string
|
|
1387
|
+
* - number
|
|
1388
|
+
* - boolean
|
|
1389
|
+
* - array
|
|
1390
|
+
* - object
|
|
1391
|
+
* - buffer
|
|
1392
|
+
*/
|
|
1393
|
+
any() {
|
|
1394
|
+
return new AnyValidator();
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* 规则以管道形式执行,需要至少匹配一个规则
|
|
1398
|
+
* ```typescript
|
|
1399
|
+
* rule.anyOf([rule.number(), rule.string()]);
|
|
1400
|
+
* ```
|
|
1401
|
+
*/
|
|
1402
|
+
anyOf(rules) {
|
|
1403
|
+
return new AnyOfValidator(rules);
|
|
1404
|
+
}
|
|
1405
|
+
array(values) {
|
|
1406
|
+
return new ArrayValidator(toValidator(values));
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* 大整数类型
|
|
1410
|
+
*
|
|
1411
|
+
* 注意:非严格模式下,`number` 和 `string` 会尝试被转换成 `bigint` 类型
|
|
1412
|
+
*/
|
|
1413
|
+
bigint() {
|
|
1414
|
+
return new BigIntValidator();
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* - 默认真值:`[1, '1', true, 'true']`
|
|
1418
|
+
* - 默认假值:`[0, '0', false, 'false']`
|
|
1419
|
+
*/
|
|
1420
|
+
boolean() {
|
|
1421
|
+
return new BooleanValidator();
|
|
1422
|
+
}
|
|
1423
|
+
buffer() {
|
|
1424
|
+
return new BufferValidator();
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* @param formats 设置解析字符串的格式,支持同时指定不同的格式并从左到右依次尝试。如果是标准的ISO时间格式或者时间戳则无需指定。
|
|
1428
|
+
*
|
|
1429
|
+
* @link https://moment.github.io/luxon/#/formatting?id=table-of-tokens
|
|
1430
|
+
*/
|
|
1431
|
+
dateTime(...formats) {
|
|
1432
|
+
return new DateTimeValidator(formats);
|
|
1433
|
+
}
|
|
1434
|
+
email() {
|
|
1435
|
+
return new EmailValidator();
|
|
1436
|
+
}
|
|
1437
|
+
enum(ranges) {
|
|
1438
|
+
return new EnumValidator(toArray2(ranges, true));
|
|
1439
|
+
}
|
|
1440
|
+
hash(algorithm) {
|
|
1441
|
+
return new HashValidator(algorithm);
|
|
1442
|
+
}
|
|
1443
|
+
/**
|
|
1444
|
+
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
1445
|
+
*/
|
|
1446
|
+
int() {
|
|
1447
|
+
return new IntValidator();
|
|
1448
|
+
}
|
|
1449
|
+
/**
|
|
1450
|
+
* ```typescript
|
|
1451
|
+
* rule.ip('v4');
|
|
1452
|
+
* rule.ip('v6');
|
|
1453
|
+
* rule.ip(['v4', 'v6']);
|
|
1454
|
+
* ```
|
|
1455
|
+
*/
|
|
1456
|
+
ip(version) {
|
|
1457
|
+
return new IpValidator(toArray2(version));
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* 注意:非严格模式下,`string` 会尝试被转换成 `number` 类型
|
|
1461
|
+
*/
|
|
1462
|
+
number() {
|
|
1463
|
+
return new NumberValidator();
|
|
1464
|
+
}
|
|
1465
|
+
/**
|
|
1466
|
+
* ```typescript
|
|
1467
|
+
* rule.object();
|
|
1468
|
+
* rule.object({
|
|
1469
|
+
* id: rule.int(),
|
|
1470
|
+
* profile: rule.object({
|
|
1471
|
+
* sex: rule.enum(['unknown', 'male', 'female']),
|
|
1472
|
+
* }),
|
|
1473
|
+
* });
|
|
1474
|
+
* ```
|
|
1475
|
+
*/
|
|
1476
|
+
object(properties) {
|
|
1477
|
+
return new ObjectValidator(properties);
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* 必须且只能匹配其中一个规则,如果匹配上多个则失败
|
|
1481
|
+
* ```typescript
|
|
1482
|
+
* rule.oneOf([rule.number(), rule.string()]);
|
|
1483
|
+
* ```
|
|
1484
|
+
*/
|
|
1485
|
+
oneOf(rules) {
|
|
1486
|
+
return new OneOfValidator(rules);
|
|
1487
|
+
}
|
|
1488
|
+
/**
|
|
1489
|
+
* 数据流
|
|
1490
|
+
*/
|
|
1491
|
+
stream() {
|
|
1492
|
+
return new StreamValidator();
|
|
1493
|
+
}
|
|
1494
|
+
string() {
|
|
1495
|
+
return new StringValidator();
|
|
1496
|
+
}
|
|
1497
|
+
ulid() {
|
|
1498
|
+
return new UlidValidator();
|
|
1499
|
+
}
|
|
1500
|
+
url() {
|
|
1501
|
+
return new UrlValidator();
|
|
1502
|
+
}
|
|
1503
|
+
uuid(versions) {
|
|
1504
|
+
return new UuidValidator(toArray2(versions || UuidValidator.versions));
|
|
1505
|
+
}
|
|
1506
|
+
};
|
|
1507
|
+
var rule = new Rule();
|
|
1508
|
+
|
|
1509
|
+
// src/api/validate.ts
|
|
1510
|
+
var validate = async (untrusted, validators, options = {}) => {
|
|
1511
|
+
const { errorFormatter = defaultErrorFormatter } = options;
|
|
1512
|
+
const validator = toValidator(validators);
|
|
1513
|
+
const source = await untrusted;
|
|
1514
|
+
const trusted = await validator["validate"](source);
|
|
1515
|
+
if (ValidateResult.noError(trusted)) return trusted.data;
|
|
1516
|
+
const msg = await errorFormatter(trusted.errors);
|
|
1517
|
+
throw new ValidateDeniedError(msg, trusted.errors);
|
|
1518
|
+
};
|
|
1519
|
+
var defaultErrorFormatter = (errors) => {
|
|
1520
|
+
let msg = i18n.t("validator.validation_failed");
|
|
1521
|
+
msg += "\n";
|
|
1522
|
+
errors.forEach((err) => {
|
|
1523
|
+
msg += `
|
|
1524
|
+
- ${err}`;
|
|
1525
|
+
});
|
|
1526
|
+
msg += "\n";
|
|
1527
|
+
return msg;
|
|
1528
|
+
};
|
|
1529
|
+
export {
|
|
1530
|
+
AllOfValidator,
|
|
1531
|
+
AnyOfValidator,
|
|
1532
|
+
AnyValidator,
|
|
1533
|
+
ArrayValidator,
|
|
1534
|
+
BaseNumberValidator,
|
|
1535
|
+
BaseStringValidator,
|
|
1536
|
+
BigIntValidator,
|
|
1537
|
+
BooleanValidator,
|
|
1538
|
+
BufferValidator,
|
|
1539
|
+
DateTimeValidator,
|
|
1540
|
+
EmailValidator,
|
|
1541
|
+
EnumValidator,
|
|
1542
|
+
HashValidator,
|
|
1543
|
+
IntValidator,
|
|
1544
|
+
IpValidator,
|
|
1545
|
+
NumberValidator,
|
|
1546
|
+
ObjectValidator,
|
|
1547
|
+
OneOfValidator,
|
|
1548
|
+
Rule,
|
|
1549
|
+
StreamValidator,
|
|
1550
|
+
StringValidator,
|
|
1551
|
+
UlidValidator,
|
|
1552
|
+
UrlValidator,
|
|
1553
|
+
UuidValidator,
|
|
1554
|
+
ValidateDeniedError,
|
|
1555
|
+
ValidateResult,
|
|
1556
|
+
Validator,
|
|
1557
|
+
rule,
|
|
1558
|
+
toValidator,
|
|
1559
|
+
validate
|
|
1560
|
+
};
|
|
1561
|
+
//# sourceMappingURL=index.js.map
|