validator-fns 1.10.1 → 2.0.1
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/dist/index.cjs +2 -553
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +409 -0
- package/dist/index.d.ts +10 -10
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -36
- package/dist/index.mjs +0 -517
- package/dist/index.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,553 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
function isObject(value) {
|
|
4
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
5
|
-
}
|
|
6
|
-
function hasOwnProperty(object, property) {
|
|
7
|
-
return Object.prototype.hasOwnProperty.call(object, property);
|
|
8
|
-
}
|
|
9
|
-
const formatPattern = /{(\w+)}/g;
|
|
10
|
-
function formatMessage(template, parameters) {
|
|
11
|
-
if (typeof template === "string") {
|
|
12
|
-
return template.replaceAll(
|
|
13
|
-
formatPattern,
|
|
14
|
-
(_, key) => String(parameters[key])
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
return template(parameters);
|
|
18
|
-
}
|
|
19
|
-
async function valid({
|
|
20
|
-
value,
|
|
21
|
-
field
|
|
22
|
-
}) {
|
|
23
|
-
return {
|
|
24
|
-
isValid: true,
|
|
25
|
-
state: "valid",
|
|
26
|
-
value,
|
|
27
|
-
field
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
async function invalid({
|
|
31
|
-
errors,
|
|
32
|
-
field,
|
|
33
|
-
message,
|
|
34
|
-
value,
|
|
35
|
-
extras
|
|
36
|
-
}) {
|
|
37
|
-
return {
|
|
38
|
-
isValid: false,
|
|
39
|
-
state: "invalid",
|
|
40
|
-
message: formatMessage(message, { ...extras, value, field }),
|
|
41
|
-
value,
|
|
42
|
-
field,
|
|
43
|
-
errors
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
function createTypeValidatorTest(defaultConfig, applyConfig) {
|
|
47
|
-
return (config, ...tests) => {
|
|
48
|
-
const cache = {};
|
|
49
|
-
let finalConfig = defaultConfig;
|
|
50
|
-
let allTests = tests;
|
|
51
|
-
if (config !== void 0) {
|
|
52
|
-
if (typeof config === "function") {
|
|
53
|
-
allTests = [config, ...tests];
|
|
54
|
-
} else {
|
|
55
|
-
finalConfig = {
|
|
56
|
-
...defaultConfig,
|
|
57
|
-
...config
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return async (value, field) => {
|
|
62
|
-
try {
|
|
63
|
-
const parsedValue = applyConfig(value, finalConfig);
|
|
64
|
-
const parsedValueString = String(parsedValue);
|
|
65
|
-
if (hasOwnProperty(cache, parsedValueString)) {
|
|
66
|
-
return cache[parsedValueString];
|
|
67
|
-
}
|
|
68
|
-
for (const validatorTest of allTests) {
|
|
69
|
-
const result = await validatorTest(parsedValue, field);
|
|
70
|
-
if (result.state === "invalid") {
|
|
71
|
-
cache[parsedValueString] = result;
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
cache[parsedValueString] = await valid({ value: parsedValue, field });
|
|
76
|
-
return cache[parsedValueString];
|
|
77
|
-
} catch (error) {
|
|
78
|
-
return invalid({
|
|
79
|
-
message: error instanceof Error ? error.message : String(error),
|
|
80
|
-
value,
|
|
81
|
-
field
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function createValidatorTest(test, message, getExtras) {
|
|
88
|
-
return async (value, field) => {
|
|
89
|
-
if (test(value, field)) {
|
|
90
|
-
return valid({ value, field });
|
|
91
|
-
}
|
|
92
|
-
return invalid({
|
|
93
|
-
message,
|
|
94
|
-
value,
|
|
95
|
-
field,
|
|
96
|
-
extras: getExtras ? getExtras(value) : void 0
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function required(message, nullable) {
|
|
101
|
-
return createValidatorTest(
|
|
102
|
-
(value) => nullable && value === null || value !== null && value !== void 0 && !(typeof value === "string" && value === "") && !(typeof value === "number" && Number.isNaN(value)) && !(value instanceof Date && Number.isNaN(value.valueOf())),
|
|
103
|
-
message
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
function getAmount(value) {
|
|
107
|
-
return typeof value === "string" || Array.isArray(value) ? value.length : value;
|
|
108
|
-
}
|
|
109
|
-
function max(limit, message, exclusive) {
|
|
110
|
-
return createValidatorTest(
|
|
111
|
-
(value) => {
|
|
112
|
-
const amount = getAmount(value);
|
|
113
|
-
return amount === void 0 || amount === null || value === "" || (exclusive ? amount < limit : amount <= limit);
|
|
114
|
-
},
|
|
115
|
-
message,
|
|
116
|
-
(value) => ({
|
|
117
|
-
max: limit,
|
|
118
|
-
limit,
|
|
119
|
-
amount: getAmount(value),
|
|
120
|
-
exclusive
|
|
121
|
-
})
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
function min(limit, message, exclusive) {
|
|
125
|
-
return createValidatorTest(
|
|
126
|
-
(value) => {
|
|
127
|
-
const amount = getAmount(value);
|
|
128
|
-
return amount === void 0 || amount === null || value === "" || (exclusive ? amount > limit : amount >= limit);
|
|
129
|
-
},
|
|
130
|
-
message,
|
|
131
|
-
(value) => ({
|
|
132
|
-
min: limit,
|
|
133
|
-
limit,
|
|
134
|
-
amount: getAmount(value),
|
|
135
|
-
exclusive
|
|
136
|
-
})
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
function exact(limit, message) {
|
|
140
|
-
return createValidatorTest(
|
|
141
|
-
(value) => {
|
|
142
|
-
const amount = getAmount(value);
|
|
143
|
-
return amount === void 0 || amount === null || value === "" || amount === limit;
|
|
144
|
-
},
|
|
145
|
-
message,
|
|
146
|
-
(value) => ({
|
|
147
|
-
amount: getAmount(value),
|
|
148
|
-
limit,
|
|
149
|
-
exact: limit
|
|
150
|
-
})
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
function oneOf(values, message) {
|
|
154
|
-
return createValidatorTest(
|
|
155
|
-
(value) => value === void 0 || value === null || values.includes(value) || value instanceof Date && values.some((x) => x.valueOf() === value.valueOf()),
|
|
156
|
-
message,
|
|
157
|
-
() => ({ values })
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function parseArray(value) {
|
|
162
|
-
if (Array.isArray(value)) {
|
|
163
|
-
return value;
|
|
164
|
-
}
|
|
165
|
-
if (value === null || value === void 0) {
|
|
166
|
-
return value;
|
|
167
|
-
}
|
|
168
|
-
const iterable = value;
|
|
169
|
-
if (iterable[Symbol.iterator]) {
|
|
170
|
-
return [...iterable];
|
|
171
|
-
}
|
|
172
|
-
return [];
|
|
173
|
-
}
|
|
174
|
-
function applyArrayConfig(config, value) {
|
|
175
|
-
let parsedValue = config.parser(value);
|
|
176
|
-
if (config.default !== void 0 && (parsedValue === void 0 || parsedValue === null || parsedValue.length === 0)) {
|
|
177
|
-
parsedValue = config.default;
|
|
178
|
-
}
|
|
179
|
-
return parsedValue;
|
|
180
|
-
}
|
|
181
|
-
function array(config, ...tests) {
|
|
182
|
-
let allTests = tests;
|
|
183
|
-
let finalConfig = {
|
|
184
|
-
parser: parseArray
|
|
185
|
-
};
|
|
186
|
-
if (config !== void 0) {
|
|
187
|
-
if (typeof config === "function") {
|
|
188
|
-
allTests = [config, ...tests];
|
|
189
|
-
} else {
|
|
190
|
-
finalConfig = {
|
|
191
|
-
...finalConfig,
|
|
192
|
-
...config
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return async (value, field) => {
|
|
197
|
-
const arrayValue = applyArrayConfig(finalConfig, value);
|
|
198
|
-
let arrayInvalidResult;
|
|
199
|
-
let isValid = true;
|
|
200
|
-
const errors = [];
|
|
201
|
-
const [validateItem, ...arrayTests] = allTests;
|
|
202
|
-
for (const validatorTest of arrayTests) {
|
|
203
|
-
const result = await validatorTest(arrayValue, field);
|
|
204
|
-
if (result.state === "invalid") {
|
|
205
|
-
isValid = false;
|
|
206
|
-
arrayInvalidResult = result;
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
if (arrayValue && validateItem) {
|
|
211
|
-
for (const [index, item] of arrayValue.entries()) {
|
|
212
|
-
const result = await validateItem(
|
|
213
|
-
item,
|
|
214
|
-
field ? `${field}[${index}]` : `[${index}]`
|
|
215
|
-
);
|
|
216
|
-
if (result.state === "invalid") {
|
|
217
|
-
isValid = false;
|
|
218
|
-
errors.push(result.message || result.errors);
|
|
219
|
-
} else {
|
|
220
|
-
errors.push(null);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
if (isValid) {
|
|
225
|
-
return valid({
|
|
226
|
-
value: arrayValue,
|
|
227
|
-
field
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
return invalid({
|
|
231
|
-
message: arrayInvalidResult?.state === "invalid" ? arrayInvalidResult.message : "",
|
|
232
|
-
value,
|
|
233
|
-
field,
|
|
234
|
-
errors
|
|
235
|
-
});
|
|
236
|
-
};
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
function parseBoolean(value) {
|
|
240
|
-
if (value === void 0 || value === null) {
|
|
241
|
-
return value;
|
|
242
|
-
}
|
|
243
|
-
if (typeof value === "boolean") {
|
|
244
|
-
return value;
|
|
245
|
-
}
|
|
246
|
-
return Boolean(value);
|
|
247
|
-
}
|
|
248
|
-
function applyBooleanConfig(value, config) {
|
|
249
|
-
let parsedValue = config.parser(value);
|
|
250
|
-
if (config.default !== void 0 && (parsedValue === void 0 || parsedValue === null)) {
|
|
251
|
-
parsedValue = config.default;
|
|
252
|
-
}
|
|
253
|
-
return parsedValue;
|
|
254
|
-
}
|
|
255
|
-
const boolean = createTypeValidatorTest(
|
|
256
|
-
{ parser: parseBoolean },
|
|
257
|
-
applyBooleanConfig
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
const invalidDate = /* @__PURE__ */ new Date("");
|
|
261
|
-
const isoDatePattern = /^(\d{4})(?:-([0-2]\d)(?:-([0-3]\d)(?:[ T]([0-2]\d):([0-5]\d)(?::([0-5]\d)(\.\d+)?(Z|[+-][0-2]\d:?[0-5]\d)?)?)?)?)?$/;
|
|
262
|
-
const timezonePattern = /^([+-])([0-2]\d):?([0-5]\d)$/;
|
|
263
|
-
function parseTimezone(timezone) {
|
|
264
|
-
const parts = timezonePattern.exec(timezone);
|
|
265
|
-
if (timezone === "Z" || !parts) {
|
|
266
|
-
return 0;
|
|
267
|
-
}
|
|
268
|
-
const [, sign, hour, minute] = parts;
|
|
269
|
-
const multiplier = sign === "-" ? -1 : 1;
|
|
270
|
-
const hoursInMinutes = Number.parseInt(hour, 10) * 60;
|
|
271
|
-
const offset = hoursInMinutes + Number.parseInt(minute, 10);
|
|
272
|
-
return multiplier * offset;
|
|
273
|
-
}
|
|
274
|
-
const BASE = 10;
|
|
275
|
-
const dateParts = [
|
|
276
|
-
// Skip
|
|
277
|
-
() => 0,
|
|
278
|
-
// Year
|
|
279
|
-
(part = "0") => Number.parseInt(part, BASE),
|
|
280
|
-
// Month
|
|
281
|
-
(part = "1") => Number.parseInt(part, BASE) - 1,
|
|
282
|
-
// Day
|
|
283
|
-
(part = "1") => Number.parseInt(part, BASE),
|
|
284
|
-
// Hour
|
|
285
|
-
(part = "0") => Number.parseInt(part, BASE),
|
|
286
|
-
// Minute
|
|
287
|
-
(part = "0") => Number.parseInt(part, BASE),
|
|
288
|
-
// Second
|
|
289
|
-
(part = "0") => Number.parseInt(part, BASE),
|
|
290
|
-
// Millisecond
|
|
291
|
-
(part = "0") => Number.parseFloat(part) * 1e3
|
|
292
|
-
];
|
|
293
|
-
function parseDate(value) {
|
|
294
|
-
if (value === null || value === void 0) {
|
|
295
|
-
return value;
|
|
296
|
-
}
|
|
297
|
-
if (value instanceof Date) {
|
|
298
|
-
return value;
|
|
299
|
-
}
|
|
300
|
-
if (typeof value === "number") {
|
|
301
|
-
return new Date(value);
|
|
302
|
-
}
|
|
303
|
-
if (typeof value !== "string") {
|
|
304
|
-
return invalidDate;
|
|
305
|
-
}
|
|
306
|
-
const parts = isoDatePattern.exec(value);
|
|
307
|
-
if (parts === null) {
|
|
308
|
-
return invalidDate;
|
|
309
|
-
}
|
|
310
|
-
const [, year, month, day, hour, minute, second, millisecond] = dateParts.map(
|
|
311
|
-
(parse, index) => parse(parts[index])
|
|
312
|
-
);
|
|
313
|
-
const timezone = parts[8];
|
|
314
|
-
if (!timezone) {
|
|
315
|
-
return new Date(year, month, day, hour, minute, second, millisecond);
|
|
316
|
-
}
|
|
317
|
-
const minuteOffset = parseTimezone(timezone);
|
|
318
|
-
return new Date(
|
|
319
|
-
Date.UTC(
|
|
320
|
-
year,
|
|
321
|
-
month,
|
|
322
|
-
day,
|
|
323
|
-
hour,
|
|
324
|
-
minute + minuteOffset,
|
|
325
|
-
second,
|
|
326
|
-
millisecond
|
|
327
|
-
)
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
function applyDateConfig(value, config) {
|
|
331
|
-
let parsedValue = config.parser(value);
|
|
332
|
-
if (config.default !== void 0 && (parsedValue === void 0 || parsedValue === null || Number.isNaN(parsedValue.getTime()))) {
|
|
333
|
-
parsedValue = config.default;
|
|
334
|
-
}
|
|
335
|
-
return parsedValue;
|
|
336
|
-
}
|
|
337
|
-
function minDate(limit, message, exclusive) {
|
|
338
|
-
const parsedDate = parseDate(limit);
|
|
339
|
-
return createValidatorTest(
|
|
340
|
-
(value) => parsedDate !== null && parsedDate !== void 0 && (value === null || value === void 0 || value instanceof Date && (exclusive ? value > parsedDate : value >= parsedDate)),
|
|
341
|
-
message,
|
|
342
|
-
() => ({
|
|
343
|
-
min: parsedDate,
|
|
344
|
-
limit: parsedDate,
|
|
345
|
-
exclusive
|
|
346
|
-
})
|
|
347
|
-
);
|
|
348
|
-
}
|
|
349
|
-
function maxDate(limit, message, exclusive) {
|
|
350
|
-
const parsedDate = parseDate(limit);
|
|
351
|
-
return createValidatorTest(
|
|
352
|
-
(value) => parsedDate !== null && parsedDate !== void 0 && (value === null || value === void 0 || value instanceof Date && (exclusive ? value < parsedDate : value <= parsedDate)),
|
|
353
|
-
message,
|
|
354
|
-
() => ({
|
|
355
|
-
max: parsedDate,
|
|
356
|
-
limit: parsedDate,
|
|
357
|
-
exclusive
|
|
358
|
-
})
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
const date = createTypeValidatorTest(
|
|
362
|
-
{ parser: parseDate },
|
|
363
|
-
applyDateConfig
|
|
364
|
-
);
|
|
365
|
-
|
|
366
|
-
function parseNumber(value) {
|
|
367
|
-
if (value === null || value === void 0) {
|
|
368
|
-
return value;
|
|
369
|
-
}
|
|
370
|
-
if (typeof value === "number") {
|
|
371
|
-
return value;
|
|
372
|
-
}
|
|
373
|
-
if (value instanceof Date) {
|
|
374
|
-
return value.getTime();
|
|
375
|
-
}
|
|
376
|
-
if (typeof value === "string") {
|
|
377
|
-
return Number.parseFloat(value);
|
|
378
|
-
}
|
|
379
|
-
return Number.parseFloat(String(value));
|
|
380
|
-
}
|
|
381
|
-
function applyNumberConfig(value, config) {
|
|
382
|
-
let parsedValue = config.parser(value);
|
|
383
|
-
if (config.default !== void 0 && (parsedValue === void 0 || parsedValue === null || Number.isNaN(parsedValue))) {
|
|
384
|
-
parsedValue = config.default;
|
|
385
|
-
}
|
|
386
|
-
if (config.round && typeof parsedValue === "number") {
|
|
387
|
-
switch (config.round) {
|
|
388
|
-
case "nearest": {
|
|
389
|
-
parsedValue = Math.round(parsedValue);
|
|
390
|
-
break;
|
|
391
|
-
}
|
|
392
|
-
case "ceil": {
|
|
393
|
-
parsedValue = Math.ceil(parsedValue);
|
|
394
|
-
break;
|
|
395
|
-
}
|
|
396
|
-
case "floor": {
|
|
397
|
-
parsedValue = Math.floor(parsedValue);
|
|
398
|
-
break;
|
|
399
|
-
}
|
|
400
|
-
default: {
|
|
401
|
-
throw new Error(`Unknown round type: ${String(config.round)}`);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
return parsedValue;
|
|
406
|
-
}
|
|
407
|
-
function integer(message) {
|
|
408
|
-
return async (value, field) => {
|
|
409
|
-
if (value === void 0 || value === null || typeof value === "number" && Number.isFinite(value) && Math.floor(value) === value) {
|
|
410
|
-
return valid({ value, field });
|
|
411
|
-
}
|
|
412
|
-
return invalid({
|
|
413
|
-
message,
|
|
414
|
-
value,
|
|
415
|
-
field
|
|
416
|
-
});
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
const number = createTypeValidatorTest(
|
|
420
|
-
{
|
|
421
|
-
parser: parseNumber
|
|
422
|
-
},
|
|
423
|
-
applyNumberConfig
|
|
424
|
-
);
|
|
425
|
-
|
|
426
|
-
function object(properties) {
|
|
427
|
-
if (typeof properties !== "object" || properties === null) {
|
|
428
|
-
throw new TypeError("`properties` must be a configuration object");
|
|
429
|
-
}
|
|
430
|
-
return async (values, field) => {
|
|
431
|
-
const definedValues = values ?? {};
|
|
432
|
-
const errors = {};
|
|
433
|
-
const resolvedValues = {};
|
|
434
|
-
let isValid = true;
|
|
435
|
-
for (const key in properties) {
|
|
436
|
-
if (hasOwnProperty(properties, key)) {
|
|
437
|
-
const validator = properties[key];
|
|
438
|
-
const value = definedValues[key];
|
|
439
|
-
const result = typeof validator === "function" ? await validator(value, key) : await invalid({ field: key, message: "No validator set", value });
|
|
440
|
-
resolvedValues[key] = result.value;
|
|
441
|
-
if (result.state === "invalid") {
|
|
442
|
-
isValid = false;
|
|
443
|
-
errors[key] = result.message || result.errors;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
if (isValid) {
|
|
448
|
-
return valid({ value: resolvedValues, field });
|
|
449
|
-
}
|
|
450
|
-
return invalid({
|
|
451
|
-
message: "",
|
|
452
|
-
value: values,
|
|
453
|
-
field,
|
|
454
|
-
errors
|
|
455
|
-
});
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
const emailPattern = /^[\w!#$%&'*+./=?^`{|}~-]+@[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?)*$/;
|
|
460
|
-
function parseString(value) {
|
|
461
|
-
if (value === null || value === void 0) {
|
|
462
|
-
return value;
|
|
463
|
-
}
|
|
464
|
-
if (typeof value === "string") {
|
|
465
|
-
return value;
|
|
466
|
-
}
|
|
467
|
-
if (value instanceof Date) {
|
|
468
|
-
return value.toISOString();
|
|
469
|
-
}
|
|
470
|
-
if (isObject(value)) {
|
|
471
|
-
throw new TypeError("Failed to parse value to string.");
|
|
472
|
-
}
|
|
473
|
-
return String(value);
|
|
474
|
-
}
|
|
475
|
-
function applyStringConfig(value, config) {
|
|
476
|
-
let parsedValue = config.parser(value);
|
|
477
|
-
if (config.default !== void 0 && (parsedValue === void 0 || parsedValue === null || parsedValue === "")) {
|
|
478
|
-
parsedValue = config.default;
|
|
479
|
-
}
|
|
480
|
-
if (config.trim && typeof parsedValue === "string") {
|
|
481
|
-
parsedValue = parsedValue.trim();
|
|
482
|
-
}
|
|
483
|
-
return parsedValue;
|
|
484
|
-
}
|
|
485
|
-
function matches(pattern, message) {
|
|
486
|
-
return createValidatorTest(
|
|
487
|
-
(value) => value === void 0 || value === null || value === "" || typeof value === "string" && pattern.test(value),
|
|
488
|
-
message,
|
|
489
|
-
() => ({ pattern })
|
|
490
|
-
);
|
|
491
|
-
}
|
|
492
|
-
function email(message) {
|
|
493
|
-
return matches(emailPattern, message);
|
|
494
|
-
}
|
|
495
|
-
function url(message, protocols) {
|
|
496
|
-
return createValidatorTest(
|
|
497
|
-
(value) => {
|
|
498
|
-
try {
|
|
499
|
-
let url2;
|
|
500
|
-
if (value === void 0 || value === null || value === "" || typeof value === "string" && (url2 = new URL(value))) {
|
|
501
|
-
return !(url2 && protocols && !protocols.includes(url2.protocol));
|
|
502
|
-
}
|
|
503
|
-
} catch {
|
|
504
|
-
}
|
|
505
|
-
return false;
|
|
506
|
-
},
|
|
507
|
-
message,
|
|
508
|
-
() => ({ protocols })
|
|
509
|
-
);
|
|
510
|
-
}
|
|
511
|
-
const string = createTypeValidatorTest(
|
|
512
|
-
{
|
|
513
|
-
parser: parseString
|
|
514
|
-
},
|
|
515
|
-
applyStringConfig
|
|
516
|
-
);
|
|
517
|
-
|
|
518
|
-
exports.applyArrayConfig = applyArrayConfig;
|
|
519
|
-
exports.applyBooleanConfig = applyBooleanConfig;
|
|
520
|
-
exports.applyDateConfig = applyDateConfig;
|
|
521
|
-
exports.applyNumberConfig = applyNumberConfig;
|
|
522
|
-
exports.applyStringConfig = applyStringConfig;
|
|
523
|
-
exports.array = array;
|
|
524
|
-
exports.boolean = boolean;
|
|
525
|
-
exports.createTypeValidatorTest = createTypeValidatorTest;
|
|
526
|
-
exports.createValidatorTest = createValidatorTest;
|
|
527
|
-
exports.date = date;
|
|
528
|
-
exports.email = email;
|
|
529
|
-
exports.exact = exact;
|
|
530
|
-
exports.formatMessage = formatMessage;
|
|
531
|
-
exports.hasOwnProperty = hasOwnProperty;
|
|
532
|
-
exports.integer = integer;
|
|
533
|
-
exports.invalid = invalid;
|
|
534
|
-
exports.invalidDate = invalidDate;
|
|
535
|
-
exports.isObject = isObject;
|
|
536
|
-
exports.matches = matches;
|
|
537
|
-
exports.max = max;
|
|
538
|
-
exports.maxDate = maxDate;
|
|
539
|
-
exports.min = min;
|
|
540
|
-
exports.minDate = minDate;
|
|
541
|
-
exports.number = number;
|
|
542
|
-
exports.object = object;
|
|
543
|
-
exports.oneOf = oneOf;
|
|
544
|
-
exports.parseArray = parseArray;
|
|
545
|
-
exports.parseBoolean = parseBoolean;
|
|
546
|
-
exports.parseDate = parseDate;
|
|
547
|
-
exports.parseNumber = parseNumber;
|
|
548
|
-
exports.parseString = parseString;
|
|
549
|
-
exports.required = required;
|
|
550
|
-
exports.string = string;
|
|
551
|
-
exports.url = url;
|
|
552
|
-
exports.valid = valid;
|
|
553
|
-
//# sourceMappingURL=index.cjs.map
|
|
1
|
+
"use strict";var E=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames;var te=Object.prototype.hasOwnProperty;var re=(e,r)=>{for(var t in r)E(e,t,{get:r[t],enumerable:!0})},ae=(e,r,t,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of ee(r))!te.call(e,n)&&n!==t&&E(e,n,{get:()=>r[n],enumerable:!(a=v(r,n))||a.enumerable});return e};var ne=e=>ae(E({},"__esModule",{value:!0}),e);var fe={};re(fe,{applyArrayConfig:()=>k,applyBooleanConfig:()=>N,applyDateConfig:()=>R,applyNumberConfig:()=>A,applyStringConfig:()=>B,array:()=>U,boolean:()=>q,createTypeValidatorTest:()=>V,createValidatorTest:()=>f,date:()=>G,email:()=>W,exact:()=>$,formatMessage:()=>h,hasOwnProperty:()=>x,integer:()=>H,invalid:()=>p,invalidDate:()=>D,isObject:()=>M,matches:()=>j,max:()=>F,maxDate:()=>_,min:()=>Z,minDate:()=>L,number:()=>J,object:()=>Q,oneOf:()=>K,parseArray:()=>C,parseBoolean:()=>S,parseDate:()=>b,parseNumber:()=>O,parseString:()=>I,required:()=>z,string:()=>Y,url:()=>X,valid:()=>g});module.exports=ne(fe);function M(e){return Object.prototype.toString.call(e)==="[object Object]"}function x(e,r){return Object.prototype.hasOwnProperty.call(e,r)}var se=/{(\w+)}/g;function h(e,r){return typeof e=="string"?e.replaceAll(se,(t,a)=>String(r[a])):e(r)}async function g({value:e,field:r}){return{isValid:!0,state:"valid",value:e,field:r}}async function p({errors:e,field:r,message:t,value:a,extras:n}){return{isValid:!1,state:"invalid",message:h(t,{...n,value:a,field:r}),value:a,field:r,errors:e}}function V(e,r){return(t,...a)=>{let n={},i=e,l=a;return t!==void 0&&(typeof t=="function"?l=[t,...a]:i={...e,...t}),async(s,u)=>{try{let o=r(s,i),d=String(o);if(x(n,d))return n[d];for(let T of l){let m=await T(o,u);if(m.state==="invalid")return n[d]=m,m}return n[d]=await g({value:o,field:u}),n[d]}catch(o){return p({message:o instanceof Error?o.message:String(o),value:s,field:u})}}}}function f(e,r,t){return async(a,n)=>e(a,n)?g({value:a,field:n}):p({message:r,value:a,field:n,extras:t?t(a):void 0})}function z(e,r){return f(t=>r&&t===null||t!=null&&!(typeof t=="string"&&t==="")&&!(typeof t=="number"&&Number.isNaN(t))&&!(t instanceof Date&&Number.isNaN(t.valueOf())),e)}function c(e){return typeof e=="string"||Array.isArray(e)?e.length:e}function F(e,r,t){return f(a=>{let n=c(a);return n==null||a===""||(t?n<e:n<=e)},r,a=>({max:e,limit:e,amount:c(a),exclusive:t}))}function Z(e,r,t){return f(a=>{let n=c(a);return n==null||a===""||(t?n>e:n>=e)},r,a=>({min:e,limit:e,amount:c(a),exclusive:t}))}function $(e,r){return f(t=>{let a=c(t);return a==null||t===""||a===e},r,t=>({amount:c(t),limit:e,exact:e}))}function K(e,r){return f(t=>t==null||e.includes(t)||t instanceof Date&&e.some(a=>a.valueOf()===t.valueOf()),r,()=>({values:e}))}function C(e){if(Array.isArray(e)||e==null)return e;let r=e;return r[Symbol.iterator]?[...r]:[]}function k(e,r){let t=e.parser(r);return e.default!==void 0&&(t==null||t.length===0)&&(t=e.default),t}function U(e,...r){let t=r,a={parser:C};return e!==void 0&&(typeof e=="function"?t=[e,...r]:a={...a,...e}),async(n,i)=>{let l=k(a,n),s,u=!0,o=[],[d,...T]=t;for(let m of T){let P=await m(l,i);if(P.state==="invalid"){u=!1,s=P;break}}if(l&&d)for(let[m,P]of l.entries()){let w=await d(P,i?`${i}[${m}]`:`[${m}]`);w.state==="invalid"?(u=!1,o.push(w.message||w.errors)):o.push(null)}return u?g({value:l,field:i}):p({message:s?.state==="invalid"?s.message:"",value:n,field:i,errors:o})}}function S(e){return e==null||typeof e=="boolean"?e:!!e}function N(e,r){let t=r.parser(e);return r.default!==void 0&&t==null&&(t=r.default),t}var q=V({parser:S},N);var D=new Date(""),oe=/^(\d{4})(?:-([0-2]\d)(?:-([0-3]\d)(?:[ T]([0-2]\d):([0-5]\d)(?::([0-5]\d)(\.\d+)?(Z|[+-][0-2]\d:?[0-5]\d)?)?)?)?)?$/,ie=/^([+-])([0-2]\d):?([0-5]\d)$/;function de(e){let r=ie.exec(e);if(e==="Z"||!r)return 0;let[,t,a="0",n="0"]=r,i=t==="-"?-1:1,s=Number.parseInt(a,10)*60+Number.parseInt(n,10);return i*s}var y=10,le=[()=>0,(e="0")=>Number.parseInt(e,y),(e="1")=>Number.parseInt(e,y)-1,(e="1")=>Number.parseInt(e,y),(e="0")=>Number.parseInt(e,y),(e="0")=>Number.parseInt(e,y),(e="0")=>Number.parseInt(e,y),(e="0")=>Number.parseFloat(e)*1e3];function b(e){if(e==null||e instanceof Date)return e;if(typeof e=="number")return new Date(e);if(typeof e!="string")return D;let r=oe.exec(e);if(r===null)return D;let[,t=0,a=0,n=0,i=0,l=0,s=0,u=0]=le.map((T,m)=>T(r[m])),o=r[8];if(!o)return new Date(t,a,n,i,l,s,u);let d=de(o);return new Date(Date.UTC(t,a,n,i,l+d,s,u))}function R(e,r){let t=r.parser(e);return r.default!==void 0&&(t==null||Number.isNaN(t.getTime()))&&(t=r.default),t}function L(e,r,t){let a=b(e);return f(n=>a!=null&&(n==null||n instanceof Date&&(t?n>a:n>=a)),r,()=>({min:a,limit:a,exclusive:t}))}function _(e,r,t){let a=b(e);return f(n=>a!=null&&(n==null||n instanceof Date&&(t?n<a:n<=a)),r,()=>({max:a,limit:a,exclusive:t}))}var G=V({parser:b},R);function O(e){return e==null||typeof e=="number"?e:e instanceof Date?e.getTime():typeof e=="string"?Number.parseFloat(e):Number.parseFloat(String(e))}function A(e,r){let t=r.parser(e);if(r.default!==void 0&&(t==null||Number.isNaN(t))&&(t=r.default),r.round&&typeof t=="number")switch(r.round){case"nearest":{t=Math.round(t);break}case"ceil":{t=Math.ceil(t);break}case"floor":{t=Math.floor(t);break}default:throw new Error(`Unknown round type: ${String(r.round)}`)}return t}function H(e){return async(r,t)=>r==null||typeof r=="number"&&Number.isFinite(r)&&Math.floor(r)===r?g({value:r,field:t}):p({message:e,value:r,field:t})}var J=V({parser:O},A);function Q(e){if(typeof e!="object"||e===null)throw new TypeError("`properties` must be a configuration object");return async(r,t)=>{let a=r??{},n={},i={},l=!0;for(let s in e)if(x(e,s)){let u=e[s],o=a[s],d=typeof u=="function"?await u(o,s):await p({field:s,message:"No validator set",value:o});i[s]=d.value,d.state==="invalid"&&(l=!1,n[s]=d.message||d.errors)}return l?g({value:i,field:t}):p({message:"",value:r,field:t,errors:n})}}var ue=/^[\w!#$%&'*+./=?^`{|}~-]+@[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?(?:\.[\dA-Za-z](?:[\dA-Za-z-]{0,61}[\dA-Za-z])?)*$/;function I(e){if(e==null||typeof e=="string")return e;if(e instanceof Date)return e.toISOString();if(M(e))throw new TypeError("Failed to parse value to string.");return String(e)}function B(e,r){let t=r.parser(e);return r.default!==void 0&&(t==null||t==="")&&(t=r.default),r.trim&&typeof t=="string"&&(t=t.trim()),t}function j(e,r){return f(t=>t==null||t===""||typeof t=="string"&&e.test(t),r,()=>({pattern:e}))}function W(e){return j(ue,e)}function X(e,r){return f(t=>{try{let a;if(t==null||t===""||typeof t=="string"&&(a=new URL(t)))return!(a&&r&&!r.includes(a.protocol))}catch{}return!1},e,()=>({protocols:r}))}var Y=V({parser:I},B);0&&(module.exports={applyArrayConfig,applyBooleanConfig,applyDateConfig,applyNumberConfig,applyStringConfig,array,boolean,createTypeValidatorTest,createValidatorTest,date,email,exact,formatMessage,hasOwnProperty,integer,invalid,invalidDate,isObject,matches,max,maxDate,min,minDate,number,object,oneOf,parseArray,parseBoolean,parseDate,parseNumber,parseString,required,string,url,valid});
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|