@palakpreet/quick-validate 2.0.0 → 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/package.json +1 -1
- package/src/index.js +749 -126
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,20 +1,36 @@
|
|
|
1
1
|
/** quick-validate v2 — lightweight JavaScript validators. */
|
|
2
|
+
|
|
2
3
|
const str = (v) => (typeof v === "string" ? v.trim() : "");
|
|
4
|
+
|
|
5
|
+
const numericString = (v) =>
|
|
6
|
+
typeof v === "number" && Number.isFinite(v) ? String(v) : str(v);
|
|
7
|
+
|
|
3
8
|
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
4
|
-
|
|
9
|
+
|
|
10
|
+
const custom = Object.create(null);
|
|
11
|
+
|
|
12
|
+
/* -------------------------------------------------------------------------- */
|
|
13
|
+
/* Basic validation */
|
|
14
|
+
/* -------------------------------------------------------------------------- */
|
|
5
15
|
|
|
6
16
|
export const isPresent = (v) =>
|
|
7
17
|
v !== null &&
|
|
8
18
|
v !== undefined &&
|
|
9
19
|
!(typeof v === "string" && !v.trim()) &&
|
|
10
20
|
!(Array.isArray(v) && !v.length);
|
|
21
|
+
|
|
11
22
|
export const isEmail = (v) => {
|
|
12
23
|
const s = str(v);
|
|
24
|
+
|
|
13
25
|
if (!s || s.length > 254 || /\s/.test(s)) return false;
|
|
26
|
+
|
|
14
27
|
const p = s.split("@");
|
|
28
|
+
|
|
15
29
|
if (p.length !== 2) return false;
|
|
30
|
+
|
|
16
31
|
const [local, domain] = p;
|
|
17
32
|
const labels = domain.split(".");
|
|
33
|
+
|
|
18
34
|
return (
|
|
19
35
|
!!local &&
|
|
20
36
|
local.length <= 64 &&
|
|
@@ -30,37 +46,88 @@ export const isEmail = (v) => {
|
|
|
30
46
|
/^[A-Za-z]{2,}$/.test(labels.at(-1))
|
|
31
47
|
);
|
|
32
48
|
};
|
|
49
|
+
|
|
50
|
+
/* -------------------------------------------------------------------------- */
|
|
51
|
+
/* Phone numbers */
|
|
52
|
+
/* -------------------------------------------------------------------------- */
|
|
53
|
+
|
|
33
54
|
export const isIndianPhone = (v, o = {}) => {
|
|
34
55
|
let s = str(v).replace(/[\s().-]/g, "");
|
|
56
|
+
|
|
35
57
|
if (!s) return false;
|
|
58
|
+
|
|
59
|
+
const hasCountryCode =
|
|
60
|
+
s.startsWith("+91") ||
|
|
61
|
+
s.startsWith("0091") ||
|
|
62
|
+
(s.length === 12 && s.startsWith("91"));
|
|
63
|
+
|
|
64
|
+
if (s.startsWith("+91")) {
|
|
65
|
+
s = s.slice(3);
|
|
66
|
+
} else if (s.startsWith("0091")) {
|
|
67
|
+
s = s.slice(4);
|
|
68
|
+
} else if (s.length === 12 && s.startsWith("91")) {
|
|
69
|
+
s = s.slice(2);
|
|
70
|
+
}
|
|
71
|
+
|
|
36
72
|
if (o.allowLandline) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
73
|
+
if (/^0?[1-9]\d{2,4}\d{6,8}$/.test(s)) return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Allow 10-digit mobile numbers by default.
|
|
77
|
+
// Set allowWithoutCountryCode: false to require +91 / 0091 / 91.
|
|
78
|
+
if (o.allowWithoutCountryCode === false && !hasCountryCode) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return /^[6-9]\d{9}$/.test(s);
|
|
45
83
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
84
|
+
|
|
85
|
+
export const isPhone = (v, country) => {
|
|
86
|
+
const countryCode = String(country ?? "").toUpperCase();
|
|
87
|
+
|
|
88
|
+
if (countryCode === "IN") {
|
|
89
|
+
return isIndianPhone(v, {
|
|
90
|
+
allowWithoutCountryCode: true,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const s = str(v).replace(/[\s().-]/g, "");
|
|
95
|
+
|
|
96
|
+
return /^\+?[1-9]\d{7,14}$/.test(s);
|
|
97
|
+
};
|
|
98
|
+
|
|
50
99
|
export const isIndianPinCode = (v) => /^[1-9]\d{5}$/.test(str(v));
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (["
|
|
100
|
+
|
|
101
|
+
export const isPostalCode = (v, country) => {
|
|
102
|
+
const s = str(v);
|
|
103
|
+
const c = String(country || "").toUpperCase();
|
|
104
|
+
|
|
105
|
+
if (["IN", "INDIA"].includes(c)) {
|
|
106
|
+
return isIndianPinCode(s);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (["US", "USA"].includes(c)) {
|
|
110
|
+
return /^\d{5}(-\d{4})?$/.test(s);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (["CA", "CANADA"].includes(c)) {
|
|
57
114
|
return /^[A-Z]\d[A-Z][ -]?\d[A-Z]\d$/i.test(s);
|
|
58
|
-
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (["GB", "UK"].includes(c)) {
|
|
59
118
|
return /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i.test(s);
|
|
119
|
+
}
|
|
120
|
+
|
|
60
121
|
return /^[A-Z0-9][A-Z0-9 -]{1,10}[A-Z0-9]$/i.test(s);
|
|
61
122
|
};
|
|
123
|
+
|
|
124
|
+
/* -------------------------------------------------------------------------- */
|
|
125
|
+
/* Password validation */
|
|
126
|
+
/* -------------------------------------------------------------------------- */
|
|
127
|
+
|
|
62
128
|
export const isStrongPassword = (v, o = {}) => {
|
|
63
129
|
const s = typeof v === "string" ? v : "";
|
|
130
|
+
|
|
64
131
|
return (
|
|
65
132
|
s.length >= (o.minLength ?? 8) &&
|
|
66
133
|
s.length <= (o.maxLength ?? 128) &&
|
|
@@ -71,8 +138,10 @@ export const isStrongPassword = (v, o = {}) => {
|
|
|
71
138
|
(o.noSpaces === false || !/\s/.test(s))
|
|
72
139
|
);
|
|
73
140
|
};
|
|
141
|
+
|
|
74
142
|
export const getPasswordStrength = (v) => {
|
|
75
143
|
const s = typeof v === "string" ? v : "";
|
|
144
|
+
|
|
76
145
|
const checks = {
|
|
77
146
|
length: s.length >= 8,
|
|
78
147
|
lowercase: /[a-z]/.test(s),
|
|
@@ -81,21 +150,36 @@ export const getPasswordStrength = (v) => {
|
|
|
81
150
|
symbol: /[^A-Za-z0-9\s]/.test(s),
|
|
82
151
|
noSpaces: !/\s/.test(s),
|
|
83
152
|
};
|
|
153
|
+
|
|
84
154
|
let score = Object.values(checks).filter(Boolean).length;
|
|
155
|
+
|
|
85
156
|
if (s.length >= 14) score++;
|
|
86
|
-
|
|
157
|
+
|
|
158
|
+
if (s.length < 8) {
|
|
159
|
+
score = Math.min(score, 1);
|
|
160
|
+
}
|
|
161
|
+
|
|
87
162
|
score = Math.min(4, score);
|
|
163
|
+
|
|
88
164
|
return {
|
|
89
165
|
score,
|
|
90
166
|
label: ["very-weak", "weak", "fair", "strong", "very-strong"][score],
|
|
91
167
|
checks,
|
|
92
168
|
};
|
|
93
169
|
};
|
|
170
|
+
|
|
171
|
+
/* -------------------------------------------------------------------------- */
|
|
172
|
+
/* URLs and domains */
|
|
173
|
+
/* -------------------------------------------------------------------------- */
|
|
174
|
+
|
|
94
175
|
export const isURL = (v, o = {}) => {
|
|
95
176
|
const s = str(v);
|
|
177
|
+
|
|
96
178
|
if (!s || /\s/.test(s)) return false;
|
|
179
|
+
|
|
97
180
|
try {
|
|
98
181
|
const u = new URL(/^[a-z][a-z\d+.-]*:/i.test(s) ? s : `https://${s}`);
|
|
182
|
+
|
|
99
183
|
return (
|
|
100
184
|
["http:", "https:"].includes(u.protocol) &&
|
|
101
185
|
(!o.requireHttps || u.protocol === "https:") &&
|
|
@@ -107,33 +191,69 @@ export const isURL = (v, o = {}) => {
|
|
|
107
191
|
return false;
|
|
108
192
|
}
|
|
109
193
|
};
|
|
194
|
+
|
|
110
195
|
export const isDomain = (v) => {
|
|
111
|
-
const s = str(v);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
s
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
196
|
+
const s = str(v).toLowerCase();
|
|
197
|
+
|
|
198
|
+
if (
|
|
199
|
+
!s ||
|
|
200
|
+
s.length > 253 ||
|
|
201
|
+
s.includes("/") ||
|
|
202
|
+
s.includes("://") ||
|
|
203
|
+
s.includes("@") ||
|
|
204
|
+
s.includes(":")
|
|
205
|
+
) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const labels = s.split(".");
|
|
210
|
+
|
|
211
|
+
if (labels.length < 2) return false;
|
|
212
|
+
|
|
213
|
+
if (
|
|
214
|
+
!labels.every(
|
|
215
|
+
(label) =>
|
|
216
|
+
label.length >= 1 &&
|
|
217
|
+
label.length <= 63 &&
|
|
218
|
+
/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(label),
|
|
219
|
+
)
|
|
220
|
+
) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return /^[a-z]{2,}$/.test(labels.at(-1));
|
|
119
225
|
};
|
|
226
|
+
|
|
227
|
+
/* -------------------------------------------------------------------------- */
|
|
228
|
+
/* UUID and IP addresses */
|
|
229
|
+
/* -------------------------------------------------------------------------- */
|
|
230
|
+
|
|
120
231
|
export const isUUID = (v) =>
|
|
121
232
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
|
|
122
233
|
str(v),
|
|
123
234
|
);
|
|
235
|
+
|
|
124
236
|
export const isIPv4 = (v) => {
|
|
125
237
|
const p = str(v).split(".");
|
|
238
|
+
|
|
126
239
|
return (
|
|
127
240
|
p.length === 4 &&
|
|
128
241
|
p.every(
|
|
129
242
|
(x) =>
|
|
130
|
-
/^\d{1,3}$/.test(x) &&
|
|
243
|
+
/^\d{1,3}$/.test(x) &&
|
|
244
|
+
Number(x) <= 255 &&
|
|
245
|
+
(x === "0" || !x.startsWith("0")),
|
|
131
246
|
)
|
|
132
247
|
);
|
|
133
248
|
};
|
|
249
|
+
|
|
134
250
|
export const isIPv6 = (v) => {
|
|
135
251
|
const s = str(v);
|
|
136
|
-
|
|
252
|
+
|
|
253
|
+
if (!s.includes(":") || !/^[\da-f:]+$/i.test(s)) {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
|
|
137
257
|
try {
|
|
138
258
|
new URL(`http://[${s}]/`);
|
|
139
259
|
return true;
|
|
@@ -141,71 +261,170 @@ export const isIPv6 = (v) => {
|
|
|
141
261
|
return false;
|
|
142
262
|
}
|
|
143
263
|
};
|
|
264
|
+
|
|
144
265
|
export const isIP = (v) => isIPv4(v) || isIPv6(v);
|
|
266
|
+
|
|
267
|
+
/* -------------------------------------------------------------------------- */
|
|
268
|
+
/* Credit cards */
|
|
269
|
+
/* -------------------------------------------------------------------------- */
|
|
270
|
+
|
|
145
271
|
export const isCreditCard = (v) => {
|
|
146
272
|
const d = str(v).replace(/[\s-]/g, "");
|
|
273
|
+
|
|
147
274
|
if (!/^\d{13,19}$/.test(d)) return false;
|
|
148
|
-
|
|
149
|
-
|
|
275
|
+
|
|
276
|
+
let sum = 0;
|
|
277
|
+
let twice = false;
|
|
278
|
+
|
|
150
279
|
for (let i = d.length - 1; i >= 0; i--) {
|
|
151
|
-
let n =
|
|
152
|
-
|
|
280
|
+
let n = Number(d[i]);
|
|
281
|
+
|
|
282
|
+
if (twice) {
|
|
283
|
+
n *= 2;
|
|
284
|
+
|
|
285
|
+
if (n > 9) n -= 9;
|
|
286
|
+
}
|
|
287
|
+
|
|
153
288
|
sum += n;
|
|
154
289
|
twice = !twice;
|
|
155
290
|
}
|
|
291
|
+
|
|
156
292
|
return sum % 10 === 0;
|
|
157
293
|
};
|
|
294
|
+
|
|
295
|
+
/* -------------------------------------------------------------------------- */
|
|
296
|
+
/* Date validation */
|
|
297
|
+
/* -------------------------------------------------------------------------- */
|
|
298
|
+
|
|
158
299
|
export const isDate = (v) => {
|
|
159
300
|
const s = str(v);
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
);
|
|
301
|
+
|
|
302
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const [year, month, day] = s.split("-").map(Number);
|
|
307
|
+
|
|
308
|
+
if (year < 1 || year > 9999) return false;
|
|
309
|
+
|
|
310
|
+
const daysInMonth = [
|
|
311
|
+
31,
|
|
312
|
+
year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28,
|
|
313
|
+
31,
|
|
314
|
+
30,
|
|
315
|
+
31,
|
|
316
|
+
30,
|
|
317
|
+
31,
|
|
318
|
+
31,
|
|
319
|
+
30,
|
|
320
|
+
31,
|
|
321
|
+
30,
|
|
322
|
+
31,
|
|
323
|
+
];
|
|
324
|
+
|
|
325
|
+
return month >= 1 && month <= 12 && day >= 1 && day <= daysInMonth[month - 1];
|
|
168
326
|
};
|
|
327
|
+
|
|
169
328
|
export const isDateTime = (v) => {
|
|
170
329
|
const s = str(v);
|
|
171
|
-
|
|
330
|
+
|
|
331
|
+
const pattern =
|
|
332
|
+
/^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?(Z|[+-]\d{2}:\d{2})?$/;
|
|
333
|
+
|
|
334
|
+
const match = s.match(pattern);
|
|
335
|
+
|
|
336
|
+
if (!match) return false;
|
|
337
|
+
|
|
338
|
+
const [, date, hours, minutes, seconds = "0", , timezone] = match;
|
|
339
|
+
|
|
340
|
+
if (!isDate(date)) return false;
|
|
341
|
+
|
|
342
|
+
if (Number(hours) > 23) return false;
|
|
343
|
+
if (Number(minutes) > 59) return false;
|
|
344
|
+
if (Number(seconds) > 59) return false;
|
|
345
|
+
|
|
346
|
+
if (timezone && timezone !== "Z") {
|
|
347
|
+
const [, offsetHours, offsetMinutes] = timezone.match(
|
|
348
|
+
/^[+-](\d{2}):(\d{2})$/,
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
if (Number(offsetHours) > 14) return false;
|
|
352
|
+
if (Number(offsetMinutes) > 59) return false;
|
|
353
|
+
|
|
354
|
+
if (Number(offsetHours) === 14 && Number(offsetMinutes) !== 0) {
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return !Number.isNaN(Date.parse(s));
|
|
172
360
|
};
|
|
361
|
+
|
|
362
|
+
/* -------------------------------------------------------------------------- */
|
|
363
|
+
/* Usernames and text formats */
|
|
364
|
+
/* -------------------------------------------------------------------------- */
|
|
365
|
+
|
|
173
366
|
export const isUsername = (v, o = {}) => {
|
|
174
367
|
const s = str(v);
|
|
368
|
+
|
|
175
369
|
return (
|
|
176
370
|
s.length >= (o.minLength ?? 3) &&
|
|
177
371
|
s.length <= (o.maxLength ?? 20) &&
|
|
178
372
|
/^[A-Za-z0-9](?:[A-Za-z0-9._-]*[A-Za-z0-9])?$/.test(s)
|
|
179
373
|
);
|
|
180
374
|
};
|
|
375
|
+
|
|
181
376
|
export const isSlug = (v) => /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(str(v));
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
377
|
+
|
|
378
|
+
export const isHexColor = (v, alpha = false) => {
|
|
379
|
+
const pattern = alpha
|
|
380
|
+
? /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i
|
|
381
|
+
: /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
|
|
382
|
+
|
|
383
|
+
return pattern.test(str(v));
|
|
384
|
+
};
|
|
385
|
+
|
|
187
386
|
export const isAlphanumeric = (v) => /^[A-Za-z0-9]+$/.test(str(v));
|
|
387
|
+
|
|
388
|
+
/* -------------------------------------------------------------------------- */
|
|
389
|
+
/* Numeric validation */
|
|
390
|
+
/* -------------------------------------------------------------------------- */
|
|
391
|
+
|
|
188
392
|
export const isNumeric = (v, o = {}) => {
|
|
189
|
-
const s =
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
393
|
+
const s = numericString(v);
|
|
394
|
+
|
|
395
|
+
if (!s) return false;
|
|
396
|
+
|
|
397
|
+
const sign = o.negative ? "-?" : "";
|
|
398
|
+
const decimal = o.decimal === false ? "" : "(?:\\.\\d+)?";
|
|
399
|
+
|
|
400
|
+
return new RegExp(`^${sign}\\d+${decimal}$`).test(s);
|
|
196
401
|
};
|
|
402
|
+
|
|
197
403
|
export const isInteger = (v, o = {}) => {
|
|
198
|
-
const s =
|
|
199
|
-
|
|
404
|
+
const s = numericString(v);
|
|
405
|
+
|
|
406
|
+
if (!s) return false;
|
|
407
|
+
|
|
408
|
+
const pattern = o.negative === false ? /^\d+$/ : /^-?\d+$/;
|
|
409
|
+
|
|
410
|
+
if (!pattern.test(s)) return false;
|
|
411
|
+
|
|
200
412
|
const n = Number(s);
|
|
413
|
+
|
|
201
414
|
return (
|
|
202
415
|
Number.isSafeInteger(n) &&
|
|
203
416
|
(o.min === undefined || n >= o.min) &&
|
|
204
417
|
(o.max === undefined || n <= o.max)
|
|
205
418
|
);
|
|
206
419
|
};
|
|
420
|
+
|
|
421
|
+
/* -------------------------------------------------------------------------- */
|
|
422
|
+
/* JSON and length validation */
|
|
423
|
+
/* -------------------------------------------------------------------------- */
|
|
424
|
+
|
|
207
425
|
export const isJSON = (v) => {
|
|
208
426
|
if (typeof v !== "string") return false;
|
|
427
|
+
|
|
209
428
|
try {
|
|
210
429
|
JSON.parse(v);
|
|
211
430
|
return true;
|
|
@@ -213,41 +432,80 @@ export const isJSON = (v) => {
|
|
|
213
432
|
return false;
|
|
214
433
|
}
|
|
215
434
|
};
|
|
435
|
+
|
|
216
436
|
export const hasMinLength = (v, n) => typeof v === "string" && v.length >= n;
|
|
437
|
+
|
|
217
438
|
export const hasMaxLength = (v, n) => typeof v === "string" && v.length <= n;
|
|
439
|
+
|
|
440
|
+
/* -------------------------------------------------------------------------- */
|
|
441
|
+
/* Range, regex and option validation */
|
|
442
|
+
/* -------------------------------------------------------------------------- */
|
|
443
|
+
|
|
218
444
|
export const isInRange = (v, min, max) =>
|
|
219
445
|
typeof v === "number" && Number.isFinite(v) && v >= min && v <= max;
|
|
446
|
+
|
|
220
447
|
export const matchesRegex = (v, r) => {
|
|
221
|
-
if (typeof v !== "string" || !(r instanceof RegExp))
|
|
448
|
+
if (typeof v !== "string" || !(r instanceof RegExp)) {
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
r.lastIndex = 0;
|
|
453
|
+
|
|
454
|
+
const result = r.test(v);
|
|
455
|
+
|
|
222
456
|
r.lastIndex = 0;
|
|
223
|
-
|
|
457
|
+
|
|
458
|
+
return result;
|
|
224
459
|
};
|
|
460
|
+
|
|
225
461
|
export const isOneOf = (v, values) =>
|
|
226
462
|
Array.isArray(values) && values.includes(v);
|
|
463
|
+
|
|
464
|
+
/* -------------------------------------------------------------------------- */
|
|
465
|
+
/* Indian business identifiers */
|
|
466
|
+
/* -------------------------------------------------------------------------- */
|
|
467
|
+
|
|
227
468
|
export const isPAN = (v) => /^[A-Z]{5}\d{4}[A-Z]$/.test(str(v).toUpperCase());
|
|
469
|
+
|
|
228
470
|
export const isGSTIN = (v) => {
|
|
229
471
|
const s = str(v).toUpperCase();
|
|
472
|
+
|
|
230
473
|
return (
|
|
231
474
|
/^\d{2}[A-Z]{5}\d{4}[A-Z]\dZ[A-Z0-9]$/.test(s) && isPAN(s.slice(2, 12))
|
|
232
475
|
);
|
|
233
476
|
};
|
|
477
|
+
|
|
234
478
|
export const isIFSC = (v) =>
|
|
235
479
|
/^[A-Z]{4}0[A-Z0-9]{6}$/.test(str(v).toUpperCase());
|
|
480
|
+
|
|
236
481
|
export const isUPI = (v) => /^[\w.-]{2,256}@[A-Za-z]{2,64}$/.test(str(v));
|
|
482
|
+
|
|
237
483
|
export const isIndianVehicleNumber = (v) =>
|
|
238
484
|
/^[A-Z]{2}\d{1,2}[A-Z]{1,3}\d{4}$/.test(
|
|
239
485
|
str(v).replace(/\s/g, "").toUpperCase(),
|
|
240
486
|
);
|
|
487
|
+
|
|
241
488
|
export const isBankAccount = (v, o = {}) => {
|
|
242
489
|
const s = str(v).replace(/\s/g, "");
|
|
490
|
+
|
|
243
491
|
return (
|
|
244
492
|
/^\d+$/.test(s) &&
|
|
245
493
|
s.length >= (o.minLength ?? 9) &&
|
|
246
494
|
s.length <= (o.maxLength ?? 18)
|
|
247
495
|
);
|
|
248
496
|
};
|
|
497
|
+
|
|
498
|
+
/* -------------------------------------------------------------------------- */
|
|
499
|
+
/* Password confirmation */
|
|
500
|
+
/* -------------------------------------------------------------------------- */
|
|
501
|
+
|
|
249
502
|
export const isConfirmPassword = (a, b) =>
|
|
250
503
|
typeof a === "string" && typeof b === "string" && a === b;
|
|
504
|
+
|
|
505
|
+
/* -------------------------------------------------------------------------- */
|
|
506
|
+
/* File validation */
|
|
507
|
+
/* -------------------------------------------------------------------------- */
|
|
508
|
+
|
|
251
509
|
export const isFileValid = (file, o = {}) =>
|
|
252
510
|
!!file &&
|
|
253
511
|
typeof file === "object" &&
|
|
@@ -259,22 +517,49 @@ export const isFileValid = (file, o = {}) =>
|
|
|
259
517
|
o.extensions.some((x) =>
|
|
260
518
|
String(file.name || "")
|
|
261
519
|
.toLowerCase()
|
|
262
|
-
.endsWith(x.toLowerCase()),
|
|
520
|
+
.endsWith(String(x).toLowerCase()),
|
|
263
521
|
));
|
|
522
|
+
|
|
523
|
+
/* -------------------------------------------------------------------------- */
|
|
524
|
+
/* Array validation */
|
|
525
|
+
/* -------------------------------------------------------------------------- */
|
|
526
|
+
|
|
264
527
|
export const isArrayOf = (v, fn) =>
|
|
265
528
|
Array.isArray(v) && typeof fn === "function" && v.every(fn);
|
|
529
|
+
|
|
530
|
+
/* -------------------------------------------------------------------------- */
|
|
531
|
+
/* Custom validators */
|
|
532
|
+
/* -------------------------------------------------------------------------- */
|
|
533
|
+
|
|
266
534
|
export const addValidator = (name, fn) => {
|
|
267
|
-
if (
|
|
535
|
+
if (
|
|
536
|
+
typeof name !== "string" ||
|
|
537
|
+
!/^[A-Za-z][\w]*$/.test(name) ||
|
|
538
|
+
typeof fn !== "function"
|
|
539
|
+
) {
|
|
268
540
|
throw new TypeError("Use a valid validator name and function.");
|
|
541
|
+
}
|
|
542
|
+
|
|
269
543
|
custom[name] = fn;
|
|
270
544
|
};
|
|
271
|
-
|
|
545
|
+
|
|
546
|
+
export const getCustomValidator = (name) =>
|
|
547
|
+
typeof name === "string" && Object.hasOwn(custom, name)
|
|
548
|
+
? custom[name]
|
|
549
|
+
: undefined;
|
|
550
|
+
|
|
551
|
+
/* -------------------------------------------------------------------------- */
|
|
552
|
+
/* Built-in validation rules */
|
|
553
|
+
/* -------------------------------------------------------------------------- */
|
|
272
554
|
|
|
273
555
|
const rules = {
|
|
274
556
|
required: isPresent,
|
|
275
557
|
email: isEmail,
|
|
276
558
|
phone: isPhone,
|
|
277
|
-
indianPhone: (v) =>
|
|
559
|
+
indianPhone: (v) =>
|
|
560
|
+
isIndianPhone(v, {
|
|
561
|
+
allowWithoutCountryCode: true,
|
|
562
|
+
}),
|
|
278
563
|
pinCode: isIndianPinCode,
|
|
279
564
|
postalCode: isPostalCode,
|
|
280
565
|
password: isStrongPassword,
|
|
@@ -301,6 +586,11 @@ const rules = {
|
|
|
301
586
|
vehicleNumber: isIndianVehicleNumber,
|
|
302
587
|
bankAccount: isBankAccount,
|
|
303
588
|
};
|
|
589
|
+
|
|
590
|
+
/* -------------------------------------------------------------------------- */
|
|
591
|
+
/* Default error messages */
|
|
592
|
+
/* -------------------------------------------------------------------------- */
|
|
593
|
+
|
|
304
594
|
const messages = {
|
|
305
595
|
required: "This field is required.",
|
|
306
596
|
email: "Please enter a valid email address.",
|
|
@@ -333,131 +623,463 @@ const messages = {
|
|
|
333
623
|
bankAccount: "Enter a valid bank account number.",
|
|
334
624
|
};
|
|
335
625
|
|
|
336
|
-
|
|
626
|
+
/* -------------------------------------------------------------------------- */
|
|
627
|
+
/* Validate one value */
|
|
628
|
+
/* -------------------------------------------------------------------------- */
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Validate one value.
|
|
632
|
+
*
|
|
633
|
+
* Rules may be:
|
|
634
|
+
* - Strings
|
|
635
|
+
* - Arrays of rules
|
|
636
|
+
* - Rule objects
|
|
637
|
+
* - Custom functions
|
|
638
|
+
*/
|
|
337
639
|
export const validate = (value, ruleList, options = {}) => {
|
|
338
640
|
const errors = [];
|
|
641
|
+
|
|
339
642
|
const list = Array.isArray(ruleList) ? ruleList : [ruleList];
|
|
643
|
+
|
|
340
644
|
for (const rule of list) {
|
|
341
|
-
let name
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
645
|
+
let name;
|
|
646
|
+
let opts = {};
|
|
647
|
+
let msg;
|
|
648
|
+
|
|
649
|
+
if (typeof rule === "string") {
|
|
650
|
+
name = rule;
|
|
651
|
+
} else if (typeof rule === "function") {
|
|
652
|
+
try {
|
|
653
|
+
if (!rule(value)) {
|
|
654
|
+
errors.push(options.message || "Value is invalid.");
|
|
655
|
+
}
|
|
656
|
+
} catch {
|
|
657
|
+
errors.push(options.message || "Value is invalid.");
|
|
658
|
+
}
|
|
659
|
+
|
|
347
660
|
continue;
|
|
348
661
|
} else if (plain(rule)) {
|
|
349
662
|
name = rule.name || rule.rule;
|
|
350
663
|
opts = rule;
|
|
351
664
|
msg = rule.message;
|
|
352
|
-
|
|
665
|
+
|
|
666
|
+
if (typeof name !== "string" || !name.trim()) {
|
|
667
|
+
errors.push("Invalid validation rule configuration.");
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
if (["min", "max", "minLength", "maxLength"].includes(name)) {
|
|
672
|
+
const n = opts.value ?? opts.min ?? opts.max;
|
|
673
|
+
|
|
674
|
+
if (
|
|
675
|
+
typeof n !== "number" ||
|
|
676
|
+
!Number.isFinite(n) ||
|
|
677
|
+
(["minLength", "maxLength"].includes(name) &&
|
|
678
|
+
(!Number.isInteger(n) || n < 0))
|
|
679
|
+
) {
|
|
680
|
+
errors.push("Invalid validation rule configuration.");
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
if (name === "oneOf") {
|
|
686
|
+
const choices = opts.values ?? opts.choices;
|
|
687
|
+
|
|
688
|
+
if (!Array.isArray(choices)) {
|
|
689
|
+
errors.push("Invalid validation rule configuration.");
|
|
690
|
+
continue;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (name === "matches" && !(opts.pattern instanceof RegExp)) {
|
|
695
|
+
errors.push("Invalid validation rule configuration.");
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
} else {
|
|
699
|
+
errors.push("Invalid validation rule configuration.");
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
|
|
353
703
|
if (name === "minLength") {
|
|
354
704
|
const n = opts.value ?? opts.min;
|
|
355
|
-
|
|
705
|
+
|
|
706
|
+
if (!hasMinLength(value, n)) {
|
|
356
707
|
errors.push(msg || `Must be at least ${n} characters.`);
|
|
708
|
+
}
|
|
709
|
+
|
|
357
710
|
continue;
|
|
358
711
|
}
|
|
712
|
+
|
|
359
713
|
if (name === "maxLength") {
|
|
360
714
|
const n = opts.value ?? opts.max;
|
|
361
|
-
|
|
715
|
+
|
|
716
|
+
if (!hasMaxLength(value, n)) {
|
|
362
717
|
errors.push(msg || `Must be no more than ${n} characters.`);
|
|
718
|
+
}
|
|
719
|
+
|
|
363
720
|
continue;
|
|
364
721
|
}
|
|
722
|
+
|
|
365
723
|
if (name === "min") {
|
|
366
724
|
const n = opts.value ?? opts.min;
|
|
367
|
-
|
|
725
|
+
|
|
726
|
+
if (typeof value !== "number" || value < n) {
|
|
368
727
|
errors.push(msg || `Must be at least ${n}.`);
|
|
728
|
+
}
|
|
729
|
+
|
|
369
730
|
continue;
|
|
370
731
|
}
|
|
732
|
+
|
|
371
733
|
if (name === "max") {
|
|
372
734
|
const n = opts.value ?? opts.max;
|
|
373
|
-
|
|
735
|
+
|
|
736
|
+
if (typeof value !== "number" || value > n) {
|
|
374
737
|
errors.push(msg || `Must be no more than ${n}.`);
|
|
738
|
+
}
|
|
739
|
+
|
|
375
740
|
continue;
|
|
376
741
|
}
|
|
742
|
+
|
|
377
743
|
if (name === "oneOf") {
|
|
378
744
|
const choices = opts.values ?? opts.choices;
|
|
379
|
-
|
|
745
|
+
|
|
746
|
+
if (!isOneOf(value, choices)) {
|
|
380
747
|
errors.push(msg || "Value is not an allowed option.");
|
|
748
|
+
}
|
|
749
|
+
|
|
381
750
|
continue;
|
|
382
751
|
}
|
|
752
|
+
|
|
383
753
|
if (name === "matches") {
|
|
384
|
-
if (!matchesRegex(value, opts.pattern))
|
|
754
|
+
if (!matchesRegex(value, opts.pattern)) {
|
|
385
755
|
errors.push(msg || "Value format is invalid.");
|
|
756
|
+
}
|
|
757
|
+
|
|
386
758
|
continue;
|
|
387
759
|
}
|
|
388
|
-
|
|
389
|
-
|
|
760
|
+
|
|
761
|
+
const fn = Object.hasOwn(rules, name)
|
|
762
|
+
? rules[name]
|
|
763
|
+
: getCustomValidator(name);
|
|
764
|
+
|
|
765
|
+
if (typeof fn !== "function") {
|
|
390
766
|
errors.push(`Unknown validation rule: ${name}`);
|
|
391
767
|
continue;
|
|
392
768
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
769
|
+
|
|
770
|
+
try {
|
|
771
|
+
if (!fn(value, opts)) {
|
|
772
|
+
errors.push(
|
|
773
|
+
msg ||
|
|
774
|
+
options.messages?.[name] ||
|
|
775
|
+
messages[name] ||
|
|
776
|
+
`${name} validation failed.`,
|
|
777
|
+
);
|
|
778
|
+
}
|
|
779
|
+
} catch {
|
|
780
|
+
errors.push(msg || `${name} validation failed.`);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
return {
|
|
785
|
+
valid: errors.length === 0,
|
|
786
|
+
errors,
|
|
787
|
+
};
|
|
402
788
|
};
|
|
403
789
|
|
|
404
|
-
|
|
790
|
+
/* -------------------------------------------------------------------------- */
|
|
791
|
+
/* Schema validation */
|
|
792
|
+
/* -------------------------------------------------------------------------- */
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Schema format:
|
|
796
|
+
*
|
|
797
|
+
* {
|
|
798
|
+
* field: ["required", "email"]
|
|
799
|
+
* }
|
|
800
|
+
*
|
|
801
|
+
* Or:
|
|
802
|
+
*
|
|
803
|
+
* {
|
|
804
|
+
* field: {
|
|
805
|
+
* rules: ["required", "email"],
|
|
806
|
+
* messages: {
|
|
807
|
+
* email: "Custom error message"
|
|
808
|
+
* }
|
|
809
|
+
* }
|
|
810
|
+
* }
|
|
811
|
+
*/
|
|
405
812
|
export const validateSchema = (data, schema) => {
|
|
406
813
|
const errors = {};
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
814
|
+
|
|
815
|
+
if (!plain(data)) {
|
|
816
|
+
return {
|
|
817
|
+
valid: false,
|
|
818
|
+
errors: {
|
|
819
|
+
_form: ["Expected an object."],
|
|
820
|
+
},
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if (!plain(schema)) {
|
|
825
|
+
return {
|
|
826
|
+
valid: false,
|
|
827
|
+
errors: {
|
|
828
|
+
_schema: ["Expected a schema object."],
|
|
829
|
+
},
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
for (const [field, config] of Object.entries(schema)) {
|
|
834
|
+
let cfg;
|
|
835
|
+
|
|
836
|
+
if (Array.isArray(config)) {
|
|
837
|
+
cfg = { rules: config };
|
|
838
|
+
} else if (plain(config) && Array.isArray(config.rules)) {
|
|
839
|
+
cfg = config;
|
|
840
|
+
} else {
|
|
841
|
+
errors[field] = ["Invalid field validation configuration."];
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
const result = validate(data[field], cfg.rules, {
|
|
412
846
|
messages: cfg.messages,
|
|
413
847
|
});
|
|
414
|
-
|
|
848
|
+
|
|
849
|
+
if (!result.valid) {
|
|
850
|
+
errors[field] = result.errors;
|
|
851
|
+
}
|
|
415
852
|
}
|
|
416
|
-
|
|
853
|
+
|
|
854
|
+
return {
|
|
855
|
+
valid: Object.keys(errors).length === 0,
|
|
856
|
+
errors,
|
|
857
|
+
};
|
|
417
858
|
};
|
|
859
|
+
|
|
860
|
+
/* -------------------------------------------------------------------------- */
|
|
861
|
+
/* Single-field schema validation */
|
|
862
|
+
/* -------------------------------------------------------------------------- */
|
|
863
|
+
|
|
418
864
|
export const validateField = (data, schema, field) => {
|
|
419
|
-
if (!(
|
|
420
|
-
return {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
865
|
+
if (!plain(schema) || !Object.hasOwn(schema, field)) {
|
|
866
|
+
return {
|
|
867
|
+
valid: false,
|
|
868
|
+
errors: ["Unknown field."],
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
const config = schema[field];
|
|
873
|
+
|
|
874
|
+
let cfg;
|
|
875
|
+
|
|
876
|
+
if (Array.isArray(config)) {
|
|
877
|
+
cfg = { rules: config };
|
|
878
|
+
} else if (plain(config) && Array.isArray(config.rules)) {
|
|
879
|
+
cfg = config;
|
|
880
|
+
} else {
|
|
881
|
+
return {
|
|
882
|
+
valid: false,
|
|
883
|
+
errors: ["Invalid field validation configuration."],
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
return validate(data?.[field], cfg.rules, {
|
|
888
|
+
messages: cfg.messages,
|
|
889
|
+
});
|
|
425
890
|
};
|
|
891
|
+
|
|
892
|
+
/* -------------------------------------------------------------------------- */
|
|
893
|
+
/* Array schema validation */
|
|
894
|
+
/* -------------------------------------------------------------------------- */
|
|
895
|
+
|
|
426
896
|
export const validateArray = (value, itemRules, options = {}) => {
|
|
427
|
-
if (!Array.isArray(value))
|
|
428
|
-
return {
|
|
897
|
+
if (!Array.isArray(value)) {
|
|
898
|
+
return {
|
|
899
|
+
valid: false,
|
|
900
|
+
errors: ["Expected an array."],
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
if (!plain(options)) {
|
|
905
|
+
return {
|
|
906
|
+
valid: false,
|
|
907
|
+
errors: ["Invalid array validation options."],
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
|
|
429
911
|
const errors = [];
|
|
430
|
-
|
|
912
|
+
|
|
913
|
+
const validCount = (count) => Number.isInteger(count) && count >= 0;
|
|
914
|
+
|
|
915
|
+
if (options.minItems !== undefined && !validCount(options.minItems)) {
|
|
916
|
+
errors.push("Invalid minItems configuration.");
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (options.maxItems !== undefined && !validCount(options.maxItems)) {
|
|
920
|
+
errors.push("Invalid maxItems configuration.");
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
if (
|
|
924
|
+
validCount(options.minItems) &&
|
|
925
|
+
validCount(options.maxItems) &&
|
|
926
|
+
options.minItems > options.maxItems
|
|
927
|
+
) {
|
|
928
|
+
errors.push("minItems cannot be greater than maxItems.");
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
if (errors.length > 0) {
|
|
932
|
+
return {
|
|
933
|
+
valid: false,
|
|
934
|
+
errors,
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
if (options.minItems !== undefined && value.length < options.minItems) {
|
|
431
939
|
errors.push(`Must contain at least ${options.minItems} items.`);
|
|
432
|
-
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
if (options.maxItems !== undefined && value.length > options.maxItems) {
|
|
433
943
|
errors.push(`Must contain no more than ${options.maxItems} items.`);
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
for (let index = 0; index < value.length; index++) {
|
|
947
|
+
const result = validate(value[index], itemRules);
|
|
948
|
+
|
|
949
|
+
if (!result.valid) {
|
|
950
|
+
errors.push({
|
|
951
|
+
index,
|
|
952
|
+
errors: result.errors,
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
return {
|
|
958
|
+
valid: errors.length === 0,
|
|
959
|
+
errors,
|
|
960
|
+
};
|
|
439
961
|
};
|
|
962
|
+
|
|
963
|
+
/* -------------------------------------------------------------------------- */
|
|
964
|
+
/* Nested validation */
|
|
965
|
+
/* -------------------------------------------------------------------------- */
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* Nested schema example:
|
|
969
|
+
*
|
|
970
|
+
* {
|
|
971
|
+
* name: ["required"],
|
|
972
|
+
* address: {
|
|
973
|
+
* type: "object",
|
|
974
|
+
* schema: {
|
|
975
|
+
* city: ["required"]
|
|
976
|
+
* }
|
|
977
|
+
* },
|
|
978
|
+
* emails: {
|
|
979
|
+
* type: "array",
|
|
980
|
+
* rules: ["email"],
|
|
981
|
+
* minItems: 1
|
|
982
|
+
* }
|
|
983
|
+
* }
|
|
984
|
+
*/
|
|
440
985
|
export const validateNested = (data, schema) => {
|
|
986
|
+
if (!plain(schema)) {
|
|
987
|
+
return {
|
|
988
|
+
valid: false,
|
|
989
|
+
errors: {
|
|
990
|
+
_schema: ["Expected a schema object."],
|
|
991
|
+
},
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
|
|
441
995
|
const errors = {};
|
|
442
|
-
|
|
996
|
+
|
|
997
|
+
for (const [key, cfg] of Object.entries(schema)) {
|
|
443
998
|
const value = data?.[key];
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
999
|
+
|
|
1000
|
+
let result;
|
|
1001
|
+
|
|
1002
|
+
if (plain(cfg) && cfg.type !== undefined) {
|
|
1003
|
+
if (cfg.type === "object") {
|
|
1004
|
+
result = validateSchema(value, cfg.schema);
|
|
1005
|
+
} else if (cfg.type === "array") {
|
|
1006
|
+
result = validateArray(value, cfg.rules ?? [], cfg);
|
|
1007
|
+
} else {
|
|
1008
|
+
errors[key] = ["Invalid validation type."];
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
} else if (plain(cfg) && Object.hasOwn(cfg, "rules")) {
|
|
1012
|
+
if (!Array.isArray(cfg.rules)) {
|
|
1013
|
+
errors[key] = ["Invalid field validation configuration."];
|
|
1014
|
+
continue;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
result = validate(value, cfg.rules);
|
|
1018
|
+
} else {
|
|
1019
|
+
result = validate(value, cfg);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
if (!result.valid) {
|
|
1023
|
+
errors[key] = result.errors;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
return {
|
|
1028
|
+
valid: Object.keys(errors).length === 0,
|
|
1029
|
+
errors,
|
|
1030
|
+
};
|
|
452
1031
|
};
|
|
1032
|
+
|
|
1033
|
+
/* -------------------------------------------------------------------------- */
|
|
1034
|
+
/* Object validation */
|
|
1035
|
+
/* -------------------------------------------------------------------------- */
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Validate object fields using validator functions.
|
|
1039
|
+
*
|
|
1040
|
+
* Example:
|
|
1041
|
+
*
|
|
1042
|
+
* validateObject(user, {
|
|
1043
|
+
* email: isEmail,
|
|
1044
|
+
* age: (v) => typeof v === "number" && v >= 18
|
|
1045
|
+
* })
|
|
1046
|
+
*/
|
|
453
1047
|
export const validateObject = (data, validators) => {
|
|
454
1048
|
const errors = {};
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
1049
|
+
|
|
1050
|
+
if (!plain(data)) {
|
|
1051
|
+
return {
|
|
1052
|
+
valid: false,
|
|
1053
|
+
errors: {
|
|
1054
|
+
_form: "Expected an object.",
|
|
1055
|
+
},
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
for (const [key, fn] of Object.entries(validators || {})) {
|
|
1060
|
+
if (typeof fn !== "function") {
|
|
1061
|
+
continue;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
try {
|
|
1065
|
+
if (!fn(data[key])) {
|
|
1066
|
+
errors[key] = `Invalid ${key}.`;
|
|
1067
|
+
}
|
|
1068
|
+
} catch {
|
|
1069
|
+
errors[key] = `Invalid ${key}.`;
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
return {
|
|
1074
|
+
valid: Object.keys(errors).length === 0,
|
|
1075
|
+
errors,
|
|
1076
|
+
};
|
|
460
1077
|
};
|
|
1078
|
+
|
|
1079
|
+
/* -------------------------------------------------------------------------- */
|
|
1080
|
+
/* Main exported object */
|
|
1081
|
+
/* -------------------------------------------------------------------------- */
|
|
1082
|
+
|
|
461
1083
|
export const quickValidate = {
|
|
462
1084
|
isPresent,
|
|
463
1085
|
isEmail,
|
|
@@ -506,4 +1128,5 @@ export const quickValidate = {
|
|
|
506
1128
|
validateNested,
|
|
507
1129
|
validateObject,
|
|
508
1130
|
};
|
|
1131
|
+
|
|
509
1132
|
export default quickValidate;
|