@kklab/fortress-validator 1.0.9

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.
Files changed (67) hide show
  1. package/README.md +202 -0
  2. package/dist/FieldValidator.d.ts +272 -0
  3. package/dist/FormValidator.d.ts +19 -0
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.js +919 -0
  6. package/dist/index.umd.js +1 -0
  7. package/dist/locales/en.d.ts +3 -0
  8. package/dist/locales/index.d.ts +3 -0
  9. package/dist/locales/zh-TW.d.ts +3 -0
  10. package/dist/rules/accepted.d.ts +3 -0
  11. package/dist/rules/alpha.d.ts +3 -0
  12. package/dist/rules/alphaDash.d.ts +3 -0
  13. package/dist/rules/alphaDashDot.d.ts +3 -0
  14. package/dist/rules/alphaNum.d.ts +3 -0
  15. package/dist/rules/array.d.ts +3 -0
  16. package/dist/rules/ascii.d.ts +3 -0
  17. package/dist/rules/between.d.ts +7 -0
  18. package/dist/rules/betweenLength.d.ts +7 -0
  19. package/dist/rules/boolean.d.ts +3 -0
  20. package/dist/rules/containsAll.d.ts +6 -0
  21. package/dist/rules/containsAny.d.ts +6 -0
  22. package/dist/rules/declined.d.ts +3 -0
  23. package/dist/rules/different.d.ts +7 -0
  24. package/dist/rules/distinct.d.ts +3 -0
  25. package/dist/rules/domain.d.ts +3 -0
  26. package/dist/rules/email.d.ts +3 -0
  27. package/dist/rules/endsWith.d.ts +6 -0
  28. package/dist/rules/equals.d.ts +6 -0
  29. package/dist/rules/file.d.ts +3 -0
  30. package/dist/rules/fileBetweenSize.d.ts +7 -0
  31. package/dist/rules/fileMaxSize.d.ts +6 -0
  32. package/dist/rules/fileMinSize.d.ts +6 -0
  33. package/dist/rules/fileSize.d.ts +6 -0
  34. package/dist/rules/http.d.ts +3 -0
  35. package/dist/rules/https.d.ts +3 -0
  36. package/dist/rules/in.d.ts +6 -0
  37. package/dist/rules/index.d.ts +3 -0
  38. package/dist/rules/integer.d.ts +3 -0
  39. package/dist/rules/json.d.ts +3 -0
  40. package/dist/rules/length.d.ts +6 -0
  41. package/dist/rules/lowercase.d.ts +3 -0
  42. package/dist/rules/max.d.ts +6 -0
  43. package/dist/rules/maxLength.d.ts +6 -0
  44. package/dist/rules/min.d.ts +6 -0
  45. package/dist/rules/minLength.d.ts +6 -0
  46. package/dist/rules/notEquals.d.ts +6 -0
  47. package/dist/rules/notIn.d.ts +6 -0
  48. package/dist/rules/number.d.ts +3 -0
  49. package/dist/rules/numeric.d.ts +3 -0
  50. package/dist/rules/regex.d.ts +6 -0
  51. package/dist/rules/required.d.ts +3 -0
  52. package/dist/rules/same.d.ts +7 -0
  53. package/dist/rules/size.d.ts +6 -0
  54. package/dist/rules/startsWith.d.ts +6 -0
  55. package/dist/rules/string.d.ts +3 -0
  56. package/dist/rules/stringBetweenLength.d.ts +7 -0
  57. package/dist/rules/stringLength.d.ts +6 -0
  58. package/dist/rules/stringMaxLength.d.ts +6 -0
  59. package/dist/rules/stringMinLength.d.ts +6 -0
  60. package/dist/rules/unique.d.ts +7 -0
  61. package/dist/rules/uppercase.d.ts +3 -0
  62. package/dist/rules/url.d.ts +3 -0
  63. package/dist/types/Conditions.d.ts +4 -0
  64. package/dist/types/FieldValidatorArguments.d.ts +9 -0
  65. package/dist/types/FormValidatorArguments.d.ts +7 -0
  66. package/dist/types/index.d.ts +4 -0
  67. package/package.json +65 -0
package/dist/index.js ADDED
@@ -0,0 +1,919 @@
1
+ var L = Object.defineProperty;
2
+ var S = (t, e, r) => e in t ? L(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var l = (t, e, r) => (S(t, typeof e != "symbol" ? e + "" : e, r), r);
4
+ const $ = (t) => t.replace(/([^\x20-\x7E])([\x20-\x7E])/gu, "$1 $2").replace(/([\x20-\x7E])([^\x20-\x7E])/gu, "$1 $2").replace(/ +/g, " "), n = (t, e = 0) => new Intl.NumberFormat(void 0, {
5
+ minimumFractionDigits: e,
6
+ maximumFractionDigits: e
7
+ }).format(t), z = (t) => Object.prototype.toString.call(t).toLowerCase().slice(8, -1), a = (t) => t == null || t === "" || Array.isArray(t) && t.length < 1;
8
+ class x {
9
+ constructor({
10
+ name: e,
11
+ locale: r,
12
+ fallbackLocale: s,
13
+ locales: i,
14
+ rules: u
15
+ }) {
16
+ l(this, "name");
17
+ l(this, "locale");
18
+ l(this, "fallbackLocale");
19
+ l(this, "locales");
20
+ l(this, "rules");
21
+ l(this, "ruleFunctions", []);
22
+ l(this, "conditions", {});
23
+ l(this, "shouldSkip", !1);
24
+ this.name = e, this.locale = r, this.fallbackLocale = s, this.locales = i, this.rules = u;
25
+ }
26
+ get formattedName() {
27
+ return this.name.toLowerCase();
28
+ }
29
+ get messages() {
30
+ return this.locales[this.locale] || {};
31
+ }
32
+ get fallbackMessages() {
33
+ return this.locales[this.fallbackLocale] || {};
34
+ }
35
+ get mandatoryRules() {
36
+ return [
37
+ this.required.name,
38
+ this.string.name,
39
+ this.array.name,
40
+ this.equals.name,
41
+ this.notEquals.name
42
+ ];
43
+ }
44
+ getMessage(e) {
45
+ return this.messages[e] || this.fallbackMessages[e] || ((r) => `The ${r} field is invalid.`);
46
+ }
47
+ getRule(e) {
48
+ if (!(e in this.rules))
49
+ throw new Error(`The "${e}" rule is not registered.`);
50
+ return this.rules[e];
51
+ }
52
+ buildRuleFunction(e, r) {
53
+ return (s) => {
54
+ if (a(s) && !this.mandatoryRules.includes(e))
55
+ return !0;
56
+ const i = this.getRule(e)(r)(s);
57
+ return typeof i == "string" ? i : i === !0 ? !0 : this.buildRuleFunctionMessage(e, r, s);
58
+ };
59
+ }
60
+ buildRuleFunctionMessage(e, r, s) {
61
+ const i = this.getMessage(e)(this.formattedName, r);
62
+ if (typeof i == "object") {
63
+ const u = z(s);
64
+ if (!(u in i))
65
+ throw new Error(`The message for the "${e}" rule of the "${u}" type is missing.`);
66
+ return $(i[u]);
67
+ }
68
+ return $(i);
69
+ }
70
+ pushRuleFunction(e, r) {
71
+ if (e in this.conditions && !this.conditions[e])
72
+ return this;
73
+ const s = this.buildRuleFunction(e, r);
74
+ return this.ruleFunctions.push(s), this;
75
+ }
76
+ getRuleFunctions() {
77
+ return this.ruleFunctions;
78
+ }
79
+ /**
80
+ * Collects the rule functions.
81
+ */
82
+ collect() {
83
+ return this.shouldSkip ? [] : this.getRuleFunctions();
84
+ }
85
+ /**
86
+ * Validates the field's value.
87
+ */
88
+ validate(e) {
89
+ if (this.shouldSkip)
90
+ return !0;
91
+ for (const r of this.ruleFunctions) {
92
+ const s = r(e);
93
+ if (typeof s == "string")
94
+ return s;
95
+ }
96
+ return !0;
97
+ }
98
+ /**
99
+ * Applies the specified rule with the given arguments.
100
+ */
101
+ apply(e, r = {}) {
102
+ return this.pushRuleFunction(e, r);
103
+ }
104
+ /**
105
+ * Passes if the field's value is considered accepted (i.e., "y", "yes", "on", "1", "true").
106
+ */
107
+ accepted() {
108
+ return this.apply(this.accepted.name);
109
+ }
110
+ /**
111
+ * Passes if the field's value is a date that occurs after the specified date.
112
+ */
113
+ after(e, r, s, i = !0) {
114
+ return this.apply(this.after.name, { date: e, format: r, displayFormat: s, strict: i });
115
+ }
116
+ /**
117
+ * Passes if the field's value contains only letters.
118
+ */
119
+ alpha() {
120
+ return this.apply(this.alpha.name);
121
+ }
122
+ /**
123
+ * Passes if the field's value contains only letters, numbers, dashes and underscores.
124
+ */
125
+ alphaDash() {
126
+ return this.apply(this.alphaDash.name);
127
+ }
128
+ /**
129
+ * Passes if the field's value contains only letters, numbers, dashes, underscores and dots.
130
+ */
131
+ alphaDashDot() {
132
+ return this.apply(this.alphaDashDot.name);
133
+ }
134
+ /**
135
+ * Passes if the field's value contains only letters and numbers.
136
+ */
137
+ alphaNum() {
138
+ return this.apply(this.alphaNum.name);
139
+ }
140
+ /**
141
+ * Passes if the field's value is an array.
142
+ */
143
+ array() {
144
+ return this.apply(this.array.name);
145
+ }
146
+ /**
147
+ * Passes if the field's value contains only ASCII characters and symbols.
148
+ */
149
+ ascii() {
150
+ return this.apply(this.ascii.name);
151
+ }
152
+ /**
153
+ * Passes if the field's value is a date that occurs before the specified date.
154
+ */
155
+ before(e, r, s, i = !0) {
156
+ return this.apply(this.before.name, { date: e, format: r, displayFormat: s, strict: i });
157
+ }
158
+ /**
159
+ * Passes if the field's value is between the specified minimum and maximum values.
160
+ */
161
+ between(e, r) {
162
+ return this.apply(this.between.name, { min: e, max: r });
163
+ }
164
+ /**
165
+ * Passes if the field's value is between the specified minimum and maximum lengths.
166
+ */
167
+ betweenLength(e, r) {
168
+ return this.apply(this.betweenLength.name, { min: e, max: r });
169
+ }
170
+ /**
171
+ * Passes if the field's value is a boolean.
172
+ */
173
+ boolean() {
174
+ return this.apply(this.boolean.name);
175
+ }
176
+ /**
177
+ * Passes if the field's value contains all the specified values.
178
+ */
179
+ containsAll(e) {
180
+ return this.apply(this.containsAll.name, { values: e });
181
+ }
182
+ /**
183
+ * Passes if the field's value contains at least one of the specified values.
184
+ */
185
+ containsAny(e) {
186
+ return this.apply(this.containsAny.name, { values: e });
187
+ }
188
+ /**
189
+ * Passes if the field's value matches the specified date format.
190
+ */
191
+ date(e, r = !0) {
192
+ return this.apply(this.date.name, { format: e, strict: r });
193
+ }
194
+ /**
195
+ * Passes if the field's value is considered declined (i.e., "n", "no", "off", "0", "false").
196
+ */
197
+ declined() {
198
+ return this.apply(this.declined.name);
199
+ }
200
+ /**
201
+ * Passes if the field's value is different from the specified value in the given field.
202
+ */
203
+ different(e, r) {
204
+ return this.apply(this.different.name, { field: e, value: r });
205
+ }
206
+ /**
207
+ * Passes if all the items in the array field's value are unique.
208
+ */
209
+ distinct() {
210
+ return this.apply(this.distinct.name);
211
+ }
212
+ /**
213
+ * Passes if the field's value is a valid domain.
214
+ */
215
+ domain() {
216
+ return this.apply(this.domain.name);
217
+ }
218
+ /**
219
+ * Passes if the field's value is a valid email address.
220
+ */
221
+ email() {
222
+ return this.apply(this.email.name);
223
+ }
224
+ /**
225
+ * Passes if the field's value ends with the specified value.
226
+ */
227
+ endsWith(e) {
228
+ return this.apply(this.endsWith.name, { value: e });
229
+ }
230
+ /**
231
+ * Passes if the field's value is equal to the specified value.
232
+ */
233
+ equals(e) {
234
+ return this.apply(this.equals.name, { value: e });
235
+ }
236
+ /**
237
+ * Passes if the field's value is a file.
238
+ */
239
+ file() {
240
+ return this.apply(this.file.name);
241
+ }
242
+ /**
243
+ * Passes if the field's value is between the specified minimum and maximum file sizes.
244
+ */
245
+ fileBetweenSize(e, r) {
246
+ return this.apply(this.fileBetweenSize.name, { min: e, max: r });
247
+ }
248
+ /**
249
+ * Passes if the field's value is not greater than the specified maximum file size.
250
+ */
251
+ fileMaxSize(e) {
252
+ return this.apply(this.fileMaxSize.name, { size: e });
253
+ }
254
+ /**
255
+ * Passes if the field's value is at least the specified minimum file size.
256
+ */
257
+ fileMinSize(e) {
258
+ return this.apply(this.fileMinSize.name, { size: e });
259
+ }
260
+ /**
261
+ * Passes if the field's value matches the specified file size.
262
+ */
263
+ fileSize(e) {
264
+ return this.apply(this.fileSize.name, { size: e });
265
+ }
266
+ /**
267
+ * Passes if the field's value starts with "http://" or "https://".
268
+ */
269
+ http() {
270
+ return this.apply(this.http.name);
271
+ }
272
+ /**
273
+ * Passes if the field's value starts with "https://".
274
+ */
275
+ https() {
276
+ return this.apply(this.https.name);
277
+ }
278
+ /**
279
+ * Passes if the field's value is one of the specified values.
280
+ */
281
+ in(e) {
282
+ return this.apply(this.in.name, { values: e });
283
+ }
284
+ /**
285
+ * Passes if the field's value is an integer.
286
+ */
287
+ integer() {
288
+ return this.apply(this.integer.name);
289
+ }
290
+ /**
291
+ * Passes if the field's value is a valid ISO 8601 date.
292
+ */
293
+ iso8601() {
294
+ return this.apply(this.iso8601.name);
295
+ }
296
+ /**
297
+ * Passes if the field's value is a valid JSON string.
298
+ */
299
+ json() {
300
+ return this.apply(this.json.name);
301
+ }
302
+ /**
303
+ * Passes if the field's value matches the specified JSON schema.
304
+ */
305
+ jsonSchema(e) {
306
+ return this.apply(this.jsonSchema.name, { schema: e, locale: this.locale, field: this.name });
307
+ }
308
+ /**
309
+ * Passes if the field's value matches the specified length.
310
+ */
311
+ length(e) {
312
+ return this.apply(this.length.name, { length: e });
313
+ }
314
+ /**
315
+ * Passes if the field's value contains only lowercase characters.
316
+ */
317
+ lowercase() {
318
+ return this.apply(this.lowercase.name);
319
+ }
320
+ /**
321
+ * Passes if the field's value is not greater than the specified maximum.
322
+ */
323
+ max(e) {
324
+ return this.apply(this.max.name, { max: e });
325
+ }
326
+ /**
327
+ * Passes if the field's value is not greater than the specified maximum length.
328
+ */
329
+ maxLength(e) {
330
+ return this.apply(this.maxLength.name, { length: e });
331
+ }
332
+ /**
333
+ * Passes if the field's value is at least the specified minimum.
334
+ */
335
+ min(e) {
336
+ return this.apply(this.min.name, { min: e });
337
+ }
338
+ /**
339
+ * Passes if the field's value is at least the specified minimum length.
340
+ */
341
+ minLength(e) {
342
+ return this.apply(this.minLength.name, { length: e });
343
+ }
344
+ /**
345
+ * Passes if the field's value is not equal to the specified value.
346
+ */
347
+ notEquals(e) {
348
+ return this.apply(this.notEquals.name, { value: e });
349
+ }
350
+ /**
351
+ * Passes if the field's value is not one of the specified values.
352
+ */
353
+ notIn(e) {
354
+ return this.apply(this.notIn.name, { values: e });
355
+ }
356
+ /**
357
+ * Passes if the field's value is a number.
358
+ */
359
+ number() {
360
+ return this.apply(this.number.name);
361
+ }
362
+ /**
363
+ * Passes if the field's value contains only numeric characters.
364
+ */
365
+ numeric() {
366
+ return this.apply(this.numeric.name);
367
+ }
368
+ /**
369
+ * Passes if the field's value matches the specified regular expression.
370
+ */
371
+ regex(e) {
372
+ return this.apply(this.regex.name, { expression: e });
373
+ }
374
+ /**
375
+ * Passes if the field's value is not empty.
376
+ */
377
+ required() {
378
+ return this.apply(this.required.name);
379
+ }
380
+ /**
381
+ * Passes if the field's value is not empty when the specified condition is true.
382
+ */
383
+ requiredWhen(e) {
384
+ return this.when({ required: e }).required();
385
+ }
386
+ /**
387
+ * Passes if the field's value is the same as the specified value in the given field.
388
+ */
389
+ same(e, r) {
390
+ return this.apply(this.same.name, { field: e, value: r });
391
+ }
392
+ /**
393
+ * Passes if the field's value matches the specified size.
394
+ */
395
+ size(e) {
396
+ return this.apply(this.size.name, { size: e });
397
+ }
398
+ /**
399
+ * Passes if the field's value starts with the specified value.
400
+ */
401
+ startsWith(e) {
402
+ return this.apply(this.startsWith.name, { value: e });
403
+ }
404
+ /**
405
+ * Passes if the field's value is a string.
406
+ */
407
+ string() {
408
+ return this.apply(this.string.name);
409
+ }
410
+ /**
411
+ * Passes if the field's value is between the specified minimum and maximum string lengths.
412
+ */
413
+ stringBetweenLength(e, r) {
414
+ return this.apply(this.stringBetweenLength.name, { min: e, max: r });
415
+ }
416
+ /**
417
+ * Passes if the field's value matches the specified string length.
418
+ */
419
+ stringLength(e) {
420
+ return this.apply(this.stringLength.name, { length: e });
421
+ }
422
+ /**
423
+ * Passes if the field's value is not greater than the specified maximum string length.
424
+ */
425
+ stringMaxLength(e) {
426
+ return this.apply(this.stringMaxLength.name, { length: e });
427
+ }
428
+ /**
429
+ * Passes if the field's value is at least the specified minimum string length.
430
+ */
431
+ stringMinLength(e) {
432
+ return this.apply(this.stringMinLength.name, { length: e });
433
+ }
434
+ /**
435
+ * Passes if the field's value contains only unique items, with optional ignored values.
436
+ */
437
+ unique(e, r = []) {
438
+ return this.apply(this.unique.name, { values: e, ignored: r });
439
+ }
440
+ /**
441
+ * Passes if the field's value contains only uppercase characters.
442
+ */
443
+ uppercase() {
444
+ return this.apply(this.uppercase.name);
445
+ }
446
+ /**
447
+ * Passes if the field's value is a valid URL.
448
+ */
449
+ url() {
450
+ return this.apply(this.url.name);
451
+ }
452
+ /**
453
+ * Determines whether to apply or skip validation based on the provided conditions.
454
+ */
455
+ when(e) {
456
+ return typeof e == "object" ? (this.conditions = e, this) : (e || (this.shouldSkip = !0), this);
457
+ }
458
+ }
459
+ const v = {
460
+ accepted: (t) => `The ${t} field must be accepted.`,
461
+ alpha: (t) => `The ${t} field must only contain letters.`,
462
+ alphaDash: (t) => `The ${t} field must only contain letters, numbers, dashes and underscores.`,
463
+ alphaDashDot: (t) => `The ${t} field must only contain letters, numbers, dashes, underscores and dots.`,
464
+ alphaNum: (t) => `The ${t} field must only contain letters and numbers.`,
465
+ array: (t) => `The ${t} field must be an array.`,
466
+ ascii: (t) => `The ${t} field must only contain ASCII characters and symbols.`,
467
+ between: (t, e) => {
468
+ const { min: r, max: s } = e;
469
+ return {
470
+ number: `The ${t} field must be between ${n(r)} and ${n(s)}.`,
471
+ array: `The ${t} field must contain items where each item is between ${n(r)} and ${n(s)}.`
472
+ };
473
+ },
474
+ betweenLength: (t, e) => {
475
+ const { min: r, max: s } = e;
476
+ return `The ${t} field must be between ${n(r)} and ${n(s)} items.`;
477
+ },
478
+ boolean: (t) => `The ${t} field must be a boolean value.`,
479
+ containsAll: (t, e) => {
480
+ const { values: r } = e;
481
+ return `The ${t} field must contain all of the following: ${r.join(", ")}.`;
482
+ },
483
+ containsAny: (t, e) => {
484
+ const { values: r } = e;
485
+ return `The ${t} field must contain at least one of the following: ${r.join(", ")}.`;
486
+ },
487
+ declined: (t) => `The ${t} field must be declined.`,
488
+ different: (t, e) => {
489
+ const { field: r } = e;
490
+ return `The ${t} and ${r} fields must be different.`;
491
+ },
492
+ distinct: (t) => `The ${t} field must not contain duplicate values.`,
493
+ domain: (t) => `The ${t} field must be a valid domain.`,
494
+ email: (t) => `The ${t} field must be a valid email address.`,
495
+ endsWith: (t, e) => {
496
+ const { value: r } = e;
497
+ return `The ${t} field must end with ${r}.`;
498
+ },
499
+ equals: (t, e) => {
500
+ const { value: r } = e;
501
+ return `The ${t} field must be equal to ${r}.`;
502
+ },
503
+ file: (t) => `The ${t} field must be a file.`,
504
+ fileBetweenSize: (t, e) => {
505
+ const { min: r, max: s } = e;
506
+ return {
507
+ file: `The ${t} field must be between ${n(r)} and ${n(s)} kilobytes.`,
508
+ array: `The ${t} field must contain items where each item is between ${n(r)} and ${n(s)} kilobytes.`
509
+ };
510
+ },
511
+ fileMaxSize: (t, e) => {
512
+ const { size: r } = e;
513
+ return {
514
+ file: `The ${t} field must not be greater than ${n(r)} kilobytes.`,
515
+ array: `The ${t} field must contain items where each item is not greater than ${n(r)} kilobytes.`
516
+ };
517
+ },
518
+ fileMinSize: (t, e) => {
519
+ const { size: r } = e;
520
+ return {
521
+ file: `The ${t} field must be at least ${n(r)} kilobytes.`,
522
+ array: `The ${t} field must contain items where each item is at least ${n(r)} kilobytes.`
523
+ };
524
+ },
525
+ fileSize: (t, e) => {
526
+ const { size: r } = e;
527
+ return {
528
+ file: `The ${t} field must be ${n(r)} kilobytes.`,
529
+ array: `The ${t} field must contain items where each item is ${n(r)} kilobytes.`
530
+ };
531
+ },
532
+ http: (t) => `The ${t} field must start with either "http://" or "https://".`,
533
+ https: (t) => `The ${t} field must start with "http://".`,
534
+ in: (t, e) => {
535
+ const { values: r } = e;
536
+ return `The ${t} field must be one of the following: ${r.join(", ")}.`;
537
+ },
538
+ integer: (t) => `The ${t} field must be an integer.`,
539
+ json: (t) => `The ${t} field must be a valid JSON string.`,
540
+ length: (t, e) => {
541
+ const { length: r } = e;
542
+ return `The ${t} field must be ${n(r)} items.`;
543
+ },
544
+ lowercase: (t) => `The ${t} field must be lowercase.`,
545
+ max: (t, e) => {
546
+ const { max: r } = e;
547
+ return {
548
+ number: `The ${t} field must not be greater than ${n(r)}.`,
549
+ array: `The ${t} field must contain items where each item is not greater than ${n(r)}.`
550
+ };
551
+ },
552
+ maxLength: (t, e) => {
553
+ const { length: r } = e;
554
+ return `The ${t} field must not be greater than ${n(r)} items.`;
555
+ },
556
+ min: (t, e) => {
557
+ const { min: r } = e;
558
+ return {
559
+ number: `The ${t} field must be at least ${n(r)}.`,
560
+ array: `The ${t} field must contain items where each item is at least ${n(r)}.`
561
+ };
562
+ },
563
+ minLength: (t, e) => {
564
+ const { length: r } = e;
565
+ return `The ${t} field must be at least ${n(r)} items.`;
566
+ },
567
+ notEquals: (t, e) => {
568
+ const { value: r } = e;
569
+ return `The ${t} field must not be equal to ${r}.`;
570
+ },
571
+ notIn: (t, e) => {
572
+ const { values: r } = e;
573
+ return `The ${t} field must not be one of the following: ${r.join(", ")}.`;
574
+ },
575
+ number: (t) => `The ${t} field must be a number.`,
576
+ numeric: (t) => `The ${t} field must be a number.`,
577
+ regex: (t) => `The ${t} field must match the required format.`,
578
+ required: (t) => `The ${t} field is required.`,
579
+ same: (t, e) => {
580
+ const { field: r } = e;
581
+ return `The ${t} and ${r} fields must match.`;
582
+ },
583
+ size: (t, e) => {
584
+ const { size: r } = e;
585
+ return {
586
+ number: `The ${t} field must be ${n(r)}.`,
587
+ array: `The ${t} field must contain items where each item is ${n(r)}.`
588
+ };
589
+ },
590
+ startsWith: (t, e) => {
591
+ const { value: r } = e;
592
+ return `The ${t} field must start with ${r}.`;
593
+ },
594
+ string: (t) => `The ${t} field must be a string.`,
595
+ stringBetweenLength: (t, e) => {
596
+ const { min: r, max: s } = e;
597
+ return {
598
+ string: `The ${t} field must be between ${n(r)} and ${n(s)} characters.`,
599
+ array: `The ${t} field must contain items where each item is between ${n(r)} and ${n(s)} characters.`
600
+ };
601
+ },
602
+ stringLength: (t, e) => {
603
+ const { length: r } = e;
604
+ return {
605
+ string: `The ${t} field must be ${n(r)} characters.`,
606
+ array: `The ${t} field must contain items where each item is ${n(r)} characters.`
607
+ };
608
+ },
609
+ stringMaxLength: (t, e) => {
610
+ const { length: r } = e;
611
+ return {
612
+ string: `The ${t} field must not be greater than ${n(r)} characters.`,
613
+ array: `The ${t} field must contain items where each item is not greater than ${n(r)} characters.`
614
+ };
615
+ },
616
+ stringMinLength: (t, e) => {
617
+ const { length: r } = e;
618
+ return {
619
+ string: `The ${t} field must be at least ${n(r)} characters.`,
620
+ array: `The ${t} field must contain items where each item is at least ${n(r)} characters.`
621
+ };
622
+ },
623
+ unique: (t) => `The ${t} field has already been taken.`,
624
+ uppercase: (t) => `The ${t} field must be uppercase.`,
625
+ url: (t) => `The ${t} field must be a valid URL.`
626
+ }, q = {
627
+ accepted: () => "此欄位必須被同意",
628
+ alpha: () => "此欄位只能包含字母",
629
+ alphaDash: () => "此欄位只能包含字母、數字、連接號和底線",
630
+ alphaDashDot: () => "此欄位只能包含字母、數字、連接號、底線和點",
631
+ alphaNum: () => "此欄位只能包含字母和數字",
632
+ array: () => "此欄位必須是一個陣列",
633
+ ascii: () => "此欄位只能包含ASCII字元和符號",
634
+ between: (t, e) => {
635
+ const { min: r, max: s } = e;
636
+ return {
637
+ number: `此欄位必須介於${n(r)}到${n(s)}`,
638
+ array: `此欄位中的每個項目都必須介於${n(r)}到${n(s)}`
639
+ };
640
+ },
641
+ betweenLength: (t, e) => {
642
+ const { min: r, max: s } = e;
643
+ return `此欄位必須介於${n(r)}到${n(s)}個項目之間`;
644
+ },
645
+ boolean: () => "此欄位必須是一個布林值",
646
+ containsAll: (t, e) => {
647
+ const { values: r } = e;
648
+ return `此欄位必須包含以下所有項目:${r.join(", ")}`;
649
+ },
650
+ containsAny: (t, e) => {
651
+ const { values: r } = e;
652
+ return `此欄位必須包含以下其中一個項目:${r.join(", ")}`;
653
+ },
654
+ declined: () => "此欄位必須被拒絕",
655
+ different: (t, e) => {
656
+ const { field: r } = e;
657
+ return `此欄位必須和${r}欄位不同`;
658
+ },
659
+ distinct: () => "此欄位不能包含重複的值",
660
+ domain: () => "此欄位必須是有效的網域",
661
+ email: () => "此欄位必須是有效的電子郵件地址",
662
+ endsWith: (t, e) => {
663
+ const { value: r } = e;
664
+ return `此欄位必須以${r}結尾`;
665
+ },
666
+ equals: (t, e) => {
667
+ const { value: r } = e;
668
+ return `此欄位必須是${r}`;
669
+ },
670
+ file: () => "此欄位必須是檔案",
671
+ fileBetweenSize: (t, e) => {
672
+ const { min: r, max: s } = e;
673
+ return {
674
+ file: `此欄位必須介於${n(r)}到${n(s)} KB之間`,
675
+ array: `此欄位中的每個項目都必須介於${n(r)}到${n(s)} KB之間`
676
+ };
677
+ },
678
+ fileMaxSize: (t, e) => {
679
+ const { size: r } = e;
680
+ return {
681
+ file: `此欄位不能大於${n(r)} KB`,
682
+ array: `此欄位中的每個項目都不能大於${n(r)} KB`
683
+ };
684
+ },
685
+ fileMinSize: (t, e) => {
686
+ const { size: r } = e;
687
+ return {
688
+ file: `此欄位不能小於${n(r)} KB`,
689
+ array: `此欄位中的每個項目都不能小於${n(r)} KB`
690
+ };
691
+ },
692
+ fileSize: (t, e) => {
693
+ const { size: r } = e;
694
+ return {
695
+ file: `此欄位必須是${n(r)} KB`,
696
+ array: `此欄位中的每個項目都必須是${n(r)} KB`
697
+ };
698
+ },
699
+ http: () => "此欄位必須以 http:// 或 https:// 開頭",
700
+ https: () => "此欄位必須以 https:// 開頭",
701
+ in: (t, e) => {
702
+ const { values: r } = e;
703
+ return `此欄位必須是以下之一:${r.join(", ")}`;
704
+ },
705
+ integer: () => "此欄位必須是整數",
706
+ json: () => "此欄位必須是有效的 JSON 字串",
707
+ length: (t, e) => {
708
+ const { length: r } = e;
709
+ return `此欄位必須包含${n(r)}個項目`;
710
+ },
711
+ lowercase: () => "此欄位必須是小寫",
712
+ max: (t, e) => {
713
+ const { max: r } = e;
714
+ return {
715
+ number: `此欄位不能大於${n(r)}`,
716
+ array: `此欄位中的每個項目都不能大於${n(r)}`
717
+ };
718
+ },
719
+ min: (t, e) => {
720
+ const { min: r } = e;
721
+ return {
722
+ number: `此欄位不能小於${n(r)}`,
723
+ array: `此欄位中的每個項目都不能小於${n(r)}`
724
+ };
725
+ },
726
+ notEquals: (t, e) => {
727
+ const { value: r } = e;
728
+ return `此欄位不能是${r}`;
729
+ },
730
+ notIn: (t, e) => {
731
+ const { values: r } = e;
732
+ return `此欄位不能是以下之一:${r.join(", ")}`;
733
+ },
734
+ number: () => "此欄位必須是數字",
735
+ numeric: () => "此欄位必須是數字",
736
+ regex: () => "此欄位必須符合所需的格式",
737
+ required: () => "此欄位為必填",
738
+ same: (t, e) => {
739
+ const { field: r } = e;
740
+ return `此欄位必須與${r}欄位相同`;
741
+ },
742
+ size: (t, e) => {
743
+ const { size: r } = e;
744
+ return {
745
+ number: `此欄位必須是${n(r)}`,
746
+ array: `此欄位中的每個項目都必須是${n(r)}`
747
+ };
748
+ },
749
+ startsWith: (t, e) => {
750
+ const { value: r } = e;
751
+ return `此欄位必須以${r}開頭`;
752
+ },
753
+ string: () => "此欄位必須是字串",
754
+ stringBetweenLength: (t, e) => {
755
+ const { min: r, max: s } = e;
756
+ return {
757
+ string: `此欄位必須介於${n(r)}到${n(s)}個字元之間`,
758
+ array: `此欄位中的每個項目都必須介於${n(r)}到${n(s)}個字元之間`
759
+ };
760
+ },
761
+ stringLength: (t, e) => {
762
+ const { length: r } = e;
763
+ return {
764
+ string: `此欄位必須是${n(r)}個字元`,
765
+ array: `此欄位中的每個項目都必須是${n(r)}個字元`
766
+ };
767
+ },
768
+ stringMaxLength: (t, e) => {
769
+ const { length: r } = e;
770
+ return {
771
+ string: `此欄位不能大於${n(r)}個字元`,
772
+ array: `此欄位中的每個項目都不能大於${n(r)}個字元`
773
+ };
774
+ },
775
+ stringMinLength: (t, e) => {
776
+ const { length: r } = e;
777
+ return {
778
+ string: `此欄位不能小於${n(r)}個字元`,
779
+ array: `此欄位中的每個項目都不能小於${n(r)}個字元`
780
+ };
781
+ },
782
+ unique: () => "此欄位已經存在",
783
+ uppercase: () => "此欄位必須是大寫",
784
+ url: () => "此欄位必須是有效的網址"
785
+ }, M = {
786
+ en: v,
787
+ "zh-TW": q
788
+ }, F = () => (t) => a(t) ? !1 : ["y", "yes", "on", "1", "true"].includes(String(t).toLowerCase()), _ = () => (t) => a(t) ? !1 : /^[a-zA-Z]+$/.test(String(t)), k = () => (t) => a(t) ? !1 : /^[a-zA-Z0-9-_]+$/.test(String(t)), j = () => (t) => a(t) ? !1 : /^[a-zA-Z0-9-_.]+$/.test(String(t)), B = () => (t) => a(t) ? !1 : /^[a-zA-Z0-9]+$/.test(String(t)), D = () => (t) => Array.isArray(t), E = () => (t) => a(t) ? !1 : /^[\x20-\x7E]+$/.test(String(t)), o = ({ max: t }) => (e) => a(e) ? !1 : typeof e == "number" ? e <= t : Array.isArray(e) ? e.every((r) => o({ max: t })(r)) : !1, c = ({ min: t }) => (e) => a(e) ? !1 : typeof e == "number" ? e >= t : Array.isArray(e) ? e.every((r) => c({ min: t })(r)) : !1, W = ({ min: t, max: e }) => (r) => a(r) ? !1 : c({ min: t })(r) && o({ max: e })(r), b = ({ length: t }) => (e) => a(e) ? !1 : Array.isArray(e) ? e.length <= t : !1, p = ({ length: t }) => (e) => a(e) ? !1 : Array.isArray(e) ? e.length >= t : !1, I = ({ min: t, max: e }) => (r) => a(r) ? !1 : p({ length: t })(r) && b({ length: e })(r), Z = () => (t) => a(t) ? !1 : typeof t == "boolean", C = ({ values: t }) => (e) => a(e) || !Array.isArray(e) ? !1 : t.every((r) => e.includes(r)), K = ({ values: t }) => (e) => a(e) || !Array.isArray(e) ? !1 : t.some((r) => e.includes(r)), N = () => (t) => a(t) ? !1 : ["n", "no", "off", "0", "false"].includes(String(t).toLowerCase()), O = ({ value: t }) => (e) => a(e) ? !1 : e !== t, J = () => (t) => a(t) ? !1 : Array.isArray(t) ? new Set(t).size === t.length : !0, P = () => (t) => a(t) ? !1 : /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(String(t)), U = () => (t) => a(t) ? !1 : /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(String(t)), V = ({ value: t }) => (e) => a(e) ? !1 : String(e).endsWith(t), G = ({ value: t }) => (e) => e === t, H = () => (t) => t instanceof File, f = ({ size: t }) => (e) => a(e) ? !1 : e instanceof File ? e.size <= t * 1024 : Array.isArray(e) ? e.every((r) => f({ size: t })(r)) : !1, m = ({ size: t }) => (e) => a(e) ? !1 : e instanceof File ? e.size >= t * 1024 : Array.isArray(e) ? e.every((r) => m({ size: t })(r)) : !1, Q = ({ min: t, max: e }) => (r) => a(r) ? !1 : m({ size: t })(r) && f({ size: e })(r), w = ({ size: t }) => (e) => {
789
+ if (a(e))
790
+ return !1;
791
+ if (e instanceof File) {
792
+ const r = t * 1024, s = (t + 1) * 1024;
793
+ return e.size >= r && e.size < s;
794
+ }
795
+ return Array.isArray(e) ? e.every((r) => w({ size: t })(r)) : !1;
796
+ }, h = ({ value: t }) => (e) => a(e) ? !1 : String(e).startsWith(t), X = () => (t) => a(t) ? !1 : h({ value: "http://" })(t) || h({ value: "https://" })(t), Y = () => (t) => a(t) ? !1 : h({ value: "https://" })(t), ee = ({ values: t }) => (e) => a(e) ? !1 : Array.isArray(e) ? e.every((r) => t.includes(r)) : t.includes(e), g = () => (t) => a(t) ? !1 : typeof t == "number", te = () => (t) => a(t) ? !1 : g()(t) && Number.isInteger(t), re = () => (t) => {
797
+ if (a(t))
798
+ return !1;
799
+ try {
800
+ return JSON.parse(String(t)), !0;
801
+ } catch {
802
+ return !1;
803
+ }
804
+ }, ne = ({ length: t }) => (e) => a(e) ? !1 : Array.isArray(e) ? e.length === t : !1, se = () => (t) => a(t) ? !1 : String(t) === String(t).toLowerCase(), ae = ({ value: t }) => (e) => e !== t, ie = ({ values: t }) => (e) => a(e) ? !1 : Array.isArray(e) ? e.every((r) => !t.includes(r)) : !t.includes(e), T = () => (t) => typeof t == "string", le = () => (t) => a(t) ? !1 : T()(t) ? /^-?\d+(\.\d+)?$/.test(String(t)) : g()(t), ue = ({ expression: t }) => (e) => {
805
+ if (a(e))
806
+ return !1;
807
+ if (!(t instanceof RegExp))
808
+ throw new TypeError("The expression provided is not a valid RegExp.");
809
+ return t.test(String(e));
810
+ }, he = () => (t) => !a(t), oe = ({ value: t }) => (e) => a(e) ? !1 : Array.isArray(e) ? e.every((r) => r === t) : e === t, R = ({ size: t }) => (e) => a(e) ? !1 : typeof e == "number" ? e === t : Array.isArray(e) ? e.every((r) => R({ size: t })(r)) : !1, y = ({ length: t }) => (e) => a(e) ? !1 : typeof e == "string" ? e.length <= t : Array.isArray(e) ? e.every((r) => y({ length: t })(r)) : !1, d = ({ length: t }) => (e) => a(e) ? !1 : typeof e == "string" ? e.length >= t : Array.isArray(e) ? e.every((r) => d({ length: t })(r)) : !1, ce = ({ min: t, max: e }) => (r) => a(r) ? !1 : d({ length: t })(r) && y({ length: e })(r), A = ({ length: t }) => (e) => a(e) ? !1 : typeof e == "string" ? e.length === t : Array.isArray(e) ? e.every((r) => A({ length: t })(r)) : !1, fe = ({ values: t, ignored: e = [] }) => (r) => a(r) ? !1 : (Array.isArray(e) ? e : [e]).some((s) => s === r) ? !0 : !t.some((s) => s === r), me = () => (t) => a(t) ? !1 : String(t) === String(t).toUpperCase(), ge = () => (t) => a(t) ? !1 : /^(https?):\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}.*$/.test(String(t)), ye = {
811
+ accepted: F,
812
+ alpha: _,
813
+ alphaDash: k,
814
+ alphaDashDot: j,
815
+ alphaNum: B,
816
+ array: D,
817
+ ascii: E,
818
+ between: W,
819
+ betweenLength: I,
820
+ boolean: Z,
821
+ containsAll: C,
822
+ containsAny: K,
823
+ declined: N,
824
+ different: O,
825
+ distinct: J,
826
+ domain: P,
827
+ email: U,
828
+ endsWith: V,
829
+ equals: G,
830
+ file: H,
831
+ fileBetweenSize: Q,
832
+ fileMaxSize: f,
833
+ fileMinSize: m,
834
+ fileSize: w,
835
+ http: X,
836
+ https: Y,
837
+ in: ee,
838
+ integer: te,
839
+ json: re,
840
+ length: ne,
841
+ lowercase: se,
842
+ max: o,
843
+ maxLength: b,
844
+ min: c,
845
+ minLength: p,
846
+ notEquals: ae,
847
+ notIn: ie,
848
+ number: g,
849
+ numeric: le,
850
+ regex: ue,
851
+ required: he,
852
+ same: oe,
853
+ size: R,
854
+ startsWith: h,
855
+ string: T,
856
+ stringBetweenLength: ce,
857
+ stringLength: A,
858
+ stringMaxLength: y,
859
+ stringMinLength: d,
860
+ unique: fe,
861
+ uppercase: me,
862
+ url: ge
863
+ };
864
+ class $e {
865
+ constructor({
866
+ fallbackLocale: e,
867
+ locale: r,
868
+ plugins: s
869
+ } = {}) {
870
+ l(this, "locale", "en");
871
+ l(this, "fallbackLocale", "en");
872
+ l(this, "locales", {});
873
+ l(this, "rules", {});
874
+ this.registerLocales(M), this.registerRules(ye), e && this.setFallbackLocale(e), r && this.setLocale(r), s && s.forEach((i) => this.registerPlugin(i));
875
+ }
876
+ getLocale() {
877
+ return this.locale;
878
+ }
879
+ getFallbackLocale() {
880
+ return this.fallbackLocale;
881
+ }
882
+ setLocale(e) {
883
+ if (!(e in this.locales))
884
+ throw new Error(`The "${e}" locale is not registered.`);
885
+ return this.locale = e, this;
886
+ }
887
+ setFallbackLocale(e) {
888
+ if (!(e in this.locales))
889
+ throw new Error(`The "${e}" fallback locale is not registered.`);
890
+ return this.fallbackLocale = e, this;
891
+ }
892
+ defineField(e) {
893
+ return new x({
894
+ name: e,
895
+ locale: this.locale,
896
+ fallbackLocale: this.fallbackLocale,
897
+ locales: this.locales,
898
+ rules: this.rules
899
+ });
900
+ }
901
+ registerLocales(e) {
902
+ return this.locales = Object.keys(e).reduce(
903
+ (r, s) => (r[s] = { ...this.locales[s], ...e[s] }, r),
904
+ { ...this.locales }
905
+ ), this;
906
+ }
907
+ registerRules(e) {
908
+ return this.rules = { ...this.rules, ...e }, this;
909
+ }
910
+ registerPlugin(e) {
911
+ if (!e || !e.locales || !e.rules)
912
+ throw new Error('The plugin must have "locales" and "rules" properties.');
913
+ return this.registerLocales(e.locales).registerRules(e.rules);
914
+ }
915
+ }
916
+ export {
917
+ x as FieldValidator,
918
+ $e as FormValidator
919
+ };