@hy_ong/zod-kit 0.0.6 → 0.1.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/.claude/settings.local.json +4 -1
- package/README.md +134 -68
- package/dist/index.cjs +93 -89
- package/dist/index.d.cts +235 -169
- package/dist/index.d.ts +235 -169
- package/dist/index.js +93 -89
- package/package.json +15 -5
- package/src/validators/common/boolean.ts +17 -14
- package/src/validators/common/date.ts +21 -14
- package/src/validators/common/datetime.ts +21 -14
- package/src/validators/common/email.ts +18 -15
- package/src/validators/common/file.ts +20 -13
- package/src/validators/common/id.ts +14 -14
- package/src/validators/common/number.ts +18 -15
- package/src/validators/common/password.ts +21 -14
- package/src/validators/common/text.ts +21 -17
- package/src/validators/common/time.ts +21 -14
- package/src/validators/common/url.ts +22 -15
- package/src/validators/taiwan/business-id.ts +18 -11
- package/src/validators/taiwan/fax.ts +23 -14
- package/src/validators/taiwan/mobile.ts +23 -14
- package/src/validators/taiwan/national-id.ts +11 -12
- package/src/validators/taiwan/postal-code.ts +16 -17
- package/src/validators/taiwan/tel.ts +23 -14
- package/tests/common/boolean.test.ts +38 -38
- package/tests/common/date.test.ts +65 -65
- package/tests/common/datetime.test.ts +100 -118
- package/tests/common/email.test.ts +24 -28
- package/tests/common/file.test.ts +47 -51
- package/tests/common/id.test.ts +80 -113
- package/tests/common/number.test.ts +24 -25
- package/tests/common/password.test.ts +28 -35
- package/tests/common/text.test.ts +36 -37
- package/tests/common/time.test.ts +64 -82
- package/tests/common/url.test.ts +67 -67
- package/tests/taiwan/business-id.test.ts +22 -22
- package/tests/taiwan/fax.test.ts +33 -42
- package/tests/taiwan/mobile.test.ts +32 -41
- package/tests/taiwan/national-id.test.ts +31 -31
- package/tests/taiwan/postal-code.test.ts +142 -96
- package/tests/taiwan/tel.test.ts +33 -42
- package/debug.js +0 -21
- package/debug.ts +0 -16
package/dist/index.js
CHANGED
|
@@ -437,9 +437,8 @@ function getNestedValue(obj, path) {
|
|
|
437
437
|
}
|
|
438
438
|
|
|
439
439
|
// src/validators/common/boolean.ts
|
|
440
|
-
function boolean(options) {
|
|
440
|
+
function boolean(required, options) {
|
|
441
441
|
const {
|
|
442
|
-
required = true,
|
|
443
442
|
defaultValue = null,
|
|
444
443
|
shouldBe,
|
|
445
444
|
truthyValues = [true, "true", 1, "1", "yes", "on"],
|
|
@@ -448,6 +447,7 @@ function boolean(options) {
|
|
|
448
447
|
transform,
|
|
449
448
|
i18n
|
|
450
449
|
} = options ?? {};
|
|
450
|
+
const isRequired = required ?? false;
|
|
451
451
|
const getMessage = (key, params) => {
|
|
452
452
|
if (i18n) {
|
|
453
453
|
const currentLocale2 = getLocale();
|
|
@@ -479,7 +479,7 @@ function boolean(options) {
|
|
|
479
479
|
},
|
|
480
480
|
z.union([z.literal(true), z.literal(false), z.literal(null)])
|
|
481
481
|
);
|
|
482
|
-
if (
|
|
482
|
+
if (isRequired && defaultValue === null) {
|
|
483
483
|
result = result.refine((val) => val !== null, { message: getMessage("required") });
|
|
484
484
|
}
|
|
485
485
|
if (shouldBe === true) {
|
|
@@ -511,9 +511,8 @@ dayjs.extend(isSameOrBefore);
|
|
|
511
511
|
dayjs.extend(customParseFormat);
|
|
512
512
|
dayjs.extend(isToday);
|
|
513
513
|
dayjs.extend(weekday);
|
|
514
|
-
function date(options) {
|
|
514
|
+
function date(required, options) {
|
|
515
515
|
const {
|
|
516
|
-
required = true,
|
|
517
516
|
min,
|
|
518
517
|
max,
|
|
519
518
|
format = "YYYY-MM-DD",
|
|
@@ -529,7 +528,8 @@ function date(options) {
|
|
|
529
528
|
defaultValue = null,
|
|
530
529
|
i18n
|
|
531
530
|
} = options ?? {};
|
|
532
|
-
const
|
|
531
|
+
const isRequired = required ?? false;
|
|
532
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
533
533
|
const getMessage = (key, params) => {
|
|
534
534
|
if (i18n) {
|
|
535
535
|
const currentLocale2 = getLocale();
|
|
@@ -551,10 +551,10 @@ function date(options) {
|
|
|
551
551
|
}
|
|
552
552
|
return processed;
|
|
553
553
|
};
|
|
554
|
-
const baseSchema =
|
|
554
|
+
const baseSchema = isRequired ? z2.preprocess(preprocessFn, z2.string()) : z2.preprocess(preprocessFn, z2.string().nullable());
|
|
555
555
|
const schema = baseSchema.refine((val) => {
|
|
556
556
|
if (val === null) return true;
|
|
557
|
-
if (
|
|
557
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
558
558
|
throw new z2.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
559
559
|
}
|
|
560
560
|
if (val !== null && !dayjs(val, format, true).isValid()) {
|
|
@@ -688,9 +688,8 @@ var normalizeDateTimeValue = (value, format, timezone2) => {
|
|
|
688
688
|
return parsed.format(format);
|
|
689
689
|
}
|
|
690
690
|
};
|
|
691
|
-
function datetime(options) {
|
|
691
|
+
function datetime(required, options) {
|
|
692
692
|
const {
|
|
693
|
-
required = true,
|
|
694
693
|
format = "YYYY-MM-DD HH:mm",
|
|
695
694
|
min,
|
|
696
695
|
max,
|
|
@@ -716,7 +715,8 @@ function datetime(options) {
|
|
|
716
715
|
defaultValue,
|
|
717
716
|
i18n
|
|
718
717
|
} = options ?? {};
|
|
719
|
-
const
|
|
718
|
+
const isRequired = required ?? false;
|
|
719
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
720
720
|
const getMessage = (key, params) => {
|
|
721
721
|
if (i18n) {
|
|
722
722
|
const currentLocale2 = getLocale();
|
|
@@ -750,7 +750,7 @@ function datetime(options) {
|
|
|
750
750
|
if (whitelist && whitelist.includes("")) {
|
|
751
751
|
return "";
|
|
752
752
|
}
|
|
753
|
-
if (!
|
|
753
|
+
if (!isRequired) {
|
|
754
754
|
return actualDefaultValue;
|
|
755
755
|
}
|
|
756
756
|
return actualDefaultValue;
|
|
@@ -770,14 +770,14 @@ function datetime(options) {
|
|
|
770
770
|
}
|
|
771
771
|
return processed;
|
|
772
772
|
};
|
|
773
|
-
const baseSchema =
|
|
773
|
+
const baseSchema = isRequired ? z3.preprocess(preprocessFn, z3.string()) : z3.preprocess(preprocessFn, z3.string().nullable());
|
|
774
774
|
const schema = baseSchema.refine((val) => {
|
|
775
775
|
if (val === null) return true;
|
|
776
|
-
if (
|
|
776
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
777
777
|
throw new z3.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
778
778
|
}
|
|
779
779
|
if (val === null) return true;
|
|
780
|
-
if (!
|
|
780
|
+
if (!isRequired && val === "") return true;
|
|
781
781
|
if (whitelist && whitelist.length > 0) {
|
|
782
782
|
if (whitelist.includes(val)) {
|
|
783
783
|
return true;
|
|
@@ -875,9 +875,8 @@ function datetime(options) {
|
|
|
875
875
|
|
|
876
876
|
// src/validators/common/email.ts
|
|
877
877
|
import { z as z4 } from "zod";
|
|
878
|
-
function email(options) {
|
|
878
|
+
function email(required, options) {
|
|
879
879
|
const {
|
|
880
|
-
required = true,
|
|
881
880
|
domain,
|
|
882
881
|
domainBlacklist,
|
|
883
882
|
minLength,
|
|
@@ -892,6 +891,7 @@ function email(options) {
|
|
|
892
891
|
defaultValue,
|
|
893
892
|
i18n
|
|
894
893
|
} = options ?? {};
|
|
894
|
+
const isRequired = required ?? false;
|
|
895
895
|
const getMessage = (key, params) => {
|
|
896
896
|
if (i18n) {
|
|
897
897
|
const currentLocale2 = getLocale();
|
|
@@ -923,7 +923,7 @@ function email(options) {
|
|
|
923
923
|
z4.union([z4.string().email(), z4.null()])
|
|
924
924
|
);
|
|
925
925
|
const schema = baseSchema.refine((val) => {
|
|
926
|
-
if (
|
|
926
|
+
if (isRequired && val === null) {
|
|
927
927
|
throw new z4.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
928
928
|
}
|
|
929
929
|
if (val === null) return true;
|
|
@@ -1005,9 +1005,8 @@ function email(options) {
|
|
|
1005
1005
|
|
|
1006
1006
|
// src/validators/common/file.ts
|
|
1007
1007
|
import { z as z5 } from "zod";
|
|
1008
|
-
function file(options) {
|
|
1008
|
+
function file(required, options) {
|
|
1009
1009
|
const {
|
|
1010
|
-
required = true,
|
|
1011
1010
|
maxSize,
|
|
1012
1011
|
minSize,
|
|
1013
1012
|
type,
|
|
@@ -1026,6 +1025,7 @@ function file(options) {
|
|
|
1026
1025
|
defaultValue,
|
|
1027
1026
|
i18n
|
|
1028
1027
|
} = options ?? {};
|
|
1028
|
+
const isRequired = required ?? false;
|
|
1029
1029
|
const getMessage = (key, params) => {
|
|
1030
1030
|
if (i18n) {
|
|
1031
1031
|
const currentLocale2 = getLocale();
|
|
@@ -1212,9 +1212,8 @@ var validateIdType = (value, type) => {
|
|
|
1212
1212
|
const pattern = ID_PATTERNS[type];
|
|
1213
1213
|
return pattern ? pattern.test(value) : false;
|
|
1214
1214
|
};
|
|
1215
|
-
function id(options) {
|
|
1215
|
+
function id(required, options) {
|
|
1216
1216
|
const {
|
|
1217
|
-
required = true,
|
|
1218
1217
|
type = "auto",
|
|
1219
1218
|
minLength,
|
|
1220
1219
|
maxLength,
|
|
@@ -1229,7 +1228,8 @@ function id(options) {
|
|
|
1229
1228
|
defaultValue,
|
|
1230
1229
|
i18n
|
|
1231
1230
|
} = options ?? {};
|
|
1232
|
-
const
|
|
1231
|
+
const isRequired = required ?? false;
|
|
1232
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
1233
1233
|
const getMessage = (key, params) => {
|
|
1234
1234
|
if (i18n) {
|
|
1235
1235
|
const currentLocale2 = getLocale();
|
|
@@ -1251,10 +1251,10 @@ function id(options) {
|
|
|
1251
1251
|
}
|
|
1252
1252
|
return processed;
|
|
1253
1253
|
};
|
|
1254
|
-
const baseSchema =
|
|
1254
|
+
const baseSchema = isRequired ? z6.preprocess(preprocessFn, z6.string()) : z6.preprocess(preprocessFn, z6.string().nullable());
|
|
1255
1255
|
const schema = baseSchema.refine((val) => {
|
|
1256
1256
|
if (val === null) return true;
|
|
1257
|
-
if (
|
|
1257
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
1258
1258
|
throw new z6.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
1259
1259
|
}
|
|
1260
1260
|
const comparisonVal = !caseSensitive ? val.toLowerCase() : val;
|
|
@@ -1336,9 +1336,8 @@ function id(options) {
|
|
|
1336
1336
|
|
|
1337
1337
|
// src/validators/common/number.ts
|
|
1338
1338
|
import { z as z7 } from "zod";
|
|
1339
|
-
function number(options) {
|
|
1339
|
+
function number(required, options) {
|
|
1340
1340
|
const {
|
|
1341
|
-
required = true,
|
|
1342
1341
|
min,
|
|
1343
1342
|
max,
|
|
1344
1343
|
defaultValue,
|
|
@@ -1354,6 +1353,7 @@ function number(options) {
|
|
|
1354
1353
|
parseCommas = false,
|
|
1355
1354
|
i18n
|
|
1356
1355
|
} = options ?? {};
|
|
1356
|
+
const isRequired = required ?? false;
|
|
1357
1357
|
const getMessage = (key, params) => {
|
|
1358
1358
|
if (i18n) {
|
|
1359
1359
|
const currentLocale2 = getLocale();
|
|
@@ -1395,7 +1395,7 @@ function number(options) {
|
|
|
1395
1395
|
},
|
|
1396
1396
|
z7.union([z7.number(), z7.null(), z7.nan(), z7.custom((val) => val === Infinity || val === -Infinity)])
|
|
1397
1397
|
).refine((val) => {
|
|
1398
|
-
if (
|
|
1398
|
+
if (isRequired && val === null) {
|
|
1399
1399
|
throw new z7.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
1400
1400
|
}
|
|
1401
1401
|
if (val === null) return true;
|
|
@@ -1488,9 +1488,8 @@ var calculatePasswordStrength = (password2) => {
|
|
|
1488
1488
|
if (score <= 6) return "strong";
|
|
1489
1489
|
return "very-strong";
|
|
1490
1490
|
};
|
|
1491
|
-
function password(options) {
|
|
1491
|
+
function password(required, options) {
|
|
1492
1492
|
const {
|
|
1493
|
-
required = true,
|
|
1494
1493
|
min,
|
|
1495
1494
|
max,
|
|
1496
1495
|
uppercase,
|
|
@@ -1508,7 +1507,8 @@ function password(options) {
|
|
|
1508
1507
|
defaultValue,
|
|
1509
1508
|
i18n
|
|
1510
1509
|
} = options ?? {};
|
|
1511
|
-
const
|
|
1510
|
+
const isRequired = required ?? false;
|
|
1511
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
1512
1512
|
const getMessage = (key, params) => {
|
|
1513
1513
|
if (i18n) {
|
|
1514
1514
|
const currentLocale2 = getLocale();
|
|
@@ -1530,10 +1530,10 @@ function password(options) {
|
|
|
1530
1530
|
}
|
|
1531
1531
|
return processed;
|
|
1532
1532
|
};
|
|
1533
|
-
const baseSchema =
|
|
1533
|
+
const baseSchema = isRequired ? z8.preprocess(preprocessFn, z8.string()) : z8.preprocess(preprocessFn, z8.string().nullable());
|
|
1534
1534
|
const schema = baseSchema.refine((val) => {
|
|
1535
1535
|
if (val === null) return true;
|
|
1536
|
-
if (
|
|
1536
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
1537
1537
|
throw new z8.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
1538
1538
|
}
|
|
1539
1539
|
if (val !== null && min !== void 0 && val.length < min) {
|
|
@@ -1593,9 +1593,10 @@ function password(options) {
|
|
|
1593
1593
|
|
|
1594
1594
|
// src/validators/common/text.ts
|
|
1595
1595
|
import { z as z9 } from "zod";
|
|
1596
|
-
function text(options) {
|
|
1597
|
-
const {
|
|
1598
|
-
const
|
|
1596
|
+
function text(required, options) {
|
|
1597
|
+
const { minLength, maxLength, startsWith, endsWith, includes, excludes, regex, trimMode = "trim", casing = "none", transform, notEmpty, defaultValue, i18n } = options ?? {};
|
|
1598
|
+
const isRequired = required ?? false;
|
|
1599
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
1599
1600
|
const getMessage = (key, params) => {
|
|
1600
1601
|
if (i18n) {
|
|
1601
1602
|
const currentLocale2 = getLocale();
|
|
@@ -1643,10 +1644,10 @@ function text(options) {
|
|
|
1643
1644
|
}
|
|
1644
1645
|
return processed;
|
|
1645
1646
|
};
|
|
1646
|
-
const baseSchema =
|
|
1647
|
+
const baseSchema = isRequired ? z9.preprocess(preprocessFn, z9.string()) : z9.preprocess(preprocessFn, z9.string().nullable());
|
|
1647
1648
|
const schema = baseSchema.refine((val) => {
|
|
1648
1649
|
if (val === null) return true;
|
|
1649
|
-
if (
|
|
1650
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
1650
1651
|
throw new z9.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
1651
1652
|
}
|
|
1652
1653
|
if (notEmpty && val !== null && val.trim() === "") {
|
|
@@ -1754,9 +1755,8 @@ var normalizeTime = (timeStr, format) => {
|
|
|
1754
1755
|
}
|
|
1755
1756
|
return cleanTime;
|
|
1756
1757
|
};
|
|
1757
|
-
function time(options) {
|
|
1758
|
+
function time(required, options) {
|
|
1758
1759
|
const {
|
|
1759
|
-
required = true,
|
|
1760
1760
|
format = "HH:mm",
|
|
1761
1761
|
min,
|
|
1762
1762
|
max,
|
|
@@ -1776,7 +1776,8 @@ function time(options) {
|
|
|
1776
1776
|
defaultValue,
|
|
1777
1777
|
i18n
|
|
1778
1778
|
} = options ?? {};
|
|
1779
|
-
const
|
|
1779
|
+
const isRequired = required ?? false;
|
|
1780
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
1780
1781
|
const getMessage = (key, params) => {
|
|
1781
1782
|
if (i18n) {
|
|
1782
1783
|
const currentLocale2 = getLocale();
|
|
@@ -1810,7 +1811,7 @@ function time(options) {
|
|
|
1810
1811
|
if (whitelist && whitelist.includes("")) {
|
|
1811
1812
|
return "";
|
|
1812
1813
|
}
|
|
1813
|
-
if (!
|
|
1814
|
+
if (!isRequired) {
|
|
1814
1815
|
return actualDefaultValue;
|
|
1815
1816
|
}
|
|
1816
1817
|
return actualDefaultValue;
|
|
@@ -1830,14 +1831,14 @@ function time(options) {
|
|
|
1830
1831
|
}
|
|
1831
1832
|
return processed;
|
|
1832
1833
|
};
|
|
1833
|
-
const baseSchema =
|
|
1834
|
+
const baseSchema = isRequired ? z10.preprocess(preprocessFn, z10.string()) : z10.preprocess(preprocessFn, z10.string().nullable());
|
|
1834
1835
|
const schema = baseSchema.refine((val) => {
|
|
1835
1836
|
if (val === null) return true;
|
|
1836
|
-
if (
|
|
1837
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
1837
1838
|
throw new z10.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
1838
1839
|
}
|
|
1839
1840
|
if (val === null) return true;
|
|
1840
|
-
if (!
|
|
1841
|
+
if (!isRequired && val === "") return true;
|
|
1841
1842
|
if (whitelist && whitelist.length > 0) {
|
|
1842
1843
|
if (whitelist.includes(val)) {
|
|
1843
1844
|
return true;
|
|
@@ -1917,9 +1918,8 @@ function time(options) {
|
|
|
1917
1918
|
|
|
1918
1919
|
// src/validators/common/url.ts
|
|
1919
1920
|
import { z as z11 } from "zod";
|
|
1920
|
-
function url(options) {
|
|
1921
|
+
function url(required, options) {
|
|
1921
1922
|
const {
|
|
1922
|
-
required = true,
|
|
1923
1923
|
min,
|
|
1924
1924
|
max,
|
|
1925
1925
|
includes,
|
|
@@ -1941,7 +1941,8 @@ function url(options) {
|
|
|
1941
1941
|
defaultValue = null,
|
|
1942
1942
|
i18n
|
|
1943
1943
|
} = options ?? {};
|
|
1944
|
-
const
|
|
1944
|
+
const isRequired = required ?? false;
|
|
1945
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
1945
1946
|
const getMessage = (key, params) => {
|
|
1946
1947
|
if (i18n) {
|
|
1947
1948
|
const currentLocale2 = getLocale();
|
|
@@ -1963,10 +1964,10 @@ function url(options) {
|
|
|
1963
1964
|
}
|
|
1964
1965
|
return processed;
|
|
1965
1966
|
};
|
|
1966
|
-
const baseSchema =
|
|
1967
|
+
const baseSchema = isRequired ? z11.preprocess(preprocessFn, z11.string()) : z11.preprocess(preprocessFn, z11.string().nullable());
|
|
1967
1968
|
const schema = baseSchema.refine((val) => {
|
|
1968
1969
|
if (val === null) return true;
|
|
1969
|
-
if (
|
|
1970
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
1970
1971
|
throw new z11.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
1971
1972
|
}
|
|
1972
1973
|
let urlObj;
|
|
@@ -2073,14 +2074,14 @@ var validateTaiwanBusinessId = (value) => {
|
|
|
2073
2074
|
}
|
|
2074
2075
|
return false;
|
|
2075
2076
|
};
|
|
2076
|
-
function businessId(options) {
|
|
2077
|
+
function businessId(required, options) {
|
|
2077
2078
|
const {
|
|
2078
|
-
required = true,
|
|
2079
2079
|
transform,
|
|
2080
2080
|
defaultValue,
|
|
2081
2081
|
i18n
|
|
2082
2082
|
} = options ?? {};
|
|
2083
|
-
const
|
|
2083
|
+
const isRequired = required ?? false;
|
|
2084
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
2084
2085
|
const getMessage = (key, params) => {
|
|
2085
2086
|
if (i18n) {
|
|
2086
2087
|
const currentLocale2 = getLocale();
|
|
@@ -2105,14 +2106,14 @@ function businessId(options) {
|
|
|
2105
2106
|
}
|
|
2106
2107
|
return processed;
|
|
2107
2108
|
};
|
|
2108
|
-
const baseSchema =
|
|
2109
|
+
const baseSchema = isRequired ? z12.preprocess(preprocessFn, z12.string()) : z12.preprocess(preprocessFn, z12.string().nullable());
|
|
2109
2110
|
const schema = baseSchema.refine((val) => {
|
|
2110
2111
|
if (val === null) return true;
|
|
2111
|
-
if (
|
|
2112
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
2112
2113
|
throw new z12.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
2113
2114
|
}
|
|
2114
2115
|
if (val === null) return true;
|
|
2115
|
-
if (!
|
|
2116
|
+
if (!isRequired && val === "") return true;
|
|
2116
2117
|
if (!validateTaiwanBusinessId(val)) {
|
|
2117
2118
|
throw new z12.ZodError([{ code: "custom", message: getMessage("invalid"), path: [] }]);
|
|
2118
2119
|
}
|
|
@@ -2219,16 +2220,16 @@ var validateTaiwanNationalId = (value, type = "both", allowOldResident = true) =
|
|
|
2219
2220
|
return false;
|
|
2220
2221
|
}
|
|
2221
2222
|
};
|
|
2222
|
-
function nationalId(options) {
|
|
2223
|
+
function nationalId(required, options) {
|
|
2223
2224
|
const {
|
|
2224
|
-
required = true,
|
|
2225
2225
|
type = "both",
|
|
2226
2226
|
allowOldResident = true,
|
|
2227
2227
|
transform,
|
|
2228
2228
|
defaultValue,
|
|
2229
2229
|
i18n
|
|
2230
2230
|
} = options ?? {};
|
|
2231
|
-
const
|
|
2231
|
+
const isRequired = required ?? false;
|
|
2232
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
2232
2233
|
const getMessage = (key, params) => {
|
|
2233
2234
|
if (i18n) {
|
|
2234
2235
|
const currentLocale2 = getLocale();
|
|
@@ -2253,14 +2254,14 @@ function nationalId(options) {
|
|
|
2253
2254
|
}
|
|
2254
2255
|
return processed;
|
|
2255
2256
|
};
|
|
2256
|
-
const baseSchema =
|
|
2257
|
+
const baseSchema = isRequired ? z13.preprocess(preprocessFn, z13.string()) : z13.preprocess(preprocessFn, z13.string().nullable());
|
|
2257
2258
|
const schema = baseSchema.refine((val) => {
|
|
2258
2259
|
if (val === null) return true;
|
|
2259
|
-
if (
|
|
2260
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
2260
2261
|
throw new z13.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
2261
2262
|
}
|
|
2262
2263
|
if (val === null) return true;
|
|
2263
|
-
if (!
|
|
2264
|
+
if (!isRequired && val === "") return true;
|
|
2264
2265
|
if (!validateTaiwanNationalId(val, type, allowOldResident)) {
|
|
2265
2266
|
throw new z13.ZodError([{ code: "custom", message: getMessage("invalid"), path: [] }]);
|
|
2266
2267
|
}
|
|
@@ -2274,9 +2275,10 @@ import { z as z14 } from "zod";
|
|
|
2274
2275
|
var validateTaiwanMobile = (value) => {
|
|
2275
2276
|
return /^09[0-9]\d{7}$/.test(value);
|
|
2276
2277
|
};
|
|
2277
|
-
function mobile(options) {
|
|
2278
|
-
const {
|
|
2279
|
-
const
|
|
2278
|
+
function mobile(required, options) {
|
|
2279
|
+
const { whitelist, transform, defaultValue, i18n } = options ?? {};
|
|
2280
|
+
const isRequired = required ?? false;
|
|
2281
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
2280
2282
|
const getMessage = (key, params) => {
|
|
2281
2283
|
if (i18n) {
|
|
2282
2284
|
const currentLocale2 = getLocale();
|
|
@@ -2297,7 +2299,7 @@ function mobile(options) {
|
|
|
2297
2299
|
if (whitelist && whitelist.includes("")) {
|
|
2298
2300
|
return "";
|
|
2299
2301
|
}
|
|
2300
|
-
if (!
|
|
2302
|
+
if (!isRequired) {
|
|
2301
2303
|
return actualDefaultValue;
|
|
2302
2304
|
}
|
|
2303
2305
|
return actualDefaultValue;
|
|
@@ -2307,14 +2309,14 @@ function mobile(options) {
|
|
|
2307
2309
|
}
|
|
2308
2310
|
return processed;
|
|
2309
2311
|
};
|
|
2310
|
-
const baseSchema =
|
|
2312
|
+
const baseSchema = isRequired ? z14.preprocess(preprocessFn, z14.string()) : z14.preprocess(preprocessFn, z14.string().nullable());
|
|
2311
2313
|
const schema = baseSchema.refine((val) => {
|
|
2312
2314
|
if (val === null) return true;
|
|
2313
|
-
if (
|
|
2315
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
2314
2316
|
throw new z14.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
2315
2317
|
}
|
|
2316
2318
|
if (val === null) return true;
|
|
2317
|
-
if (!
|
|
2319
|
+
if (!isRequired && val === "") return true;
|
|
2318
2320
|
if (whitelist && whitelist.length > 0) {
|
|
2319
2321
|
if (whitelist.includes(val)) {
|
|
2320
2322
|
return true;
|
|
@@ -2911,9 +2913,8 @@ var validateTaiwanPostalCode = (value, format = "3+6", strictValidation = true,
|
|
|
2911
2913
|
return false;
|
|
2912
2914
|
}
|
|
2913
2915
|
};
|
|
2914
|
-
function postalCode(options) {
|
|
2916
|
+
function postalCode(required, options) {
|
|
2915
2917
|
const {
|
|
2916
|
-
required = true,
|
|
2917
2918
|
format = "3+6",
|
|
2918
2919
|
strictValidation = true,
|
|
2919
2920
|
allowDashes = true,
|
|
@@ -2926,7 +2927,8 @@ function postalCode(options) {
|
|
|
2926
2927
|
strictSuffixValidation = false,
|
|
2927
2928
|
deprecate5Digit = false
|
|
2928
2929
|
} = options ?? {};
|
|
2929
|
-
const
|
|
2930
|
+
const isRequired = required ?? false;
|
|
2931
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
2930
2932
|
const getMessage = (key, params) => {
|
|
2931
2933
|
if (i18n) {
|
|
2932
2934
|
const currentLocale2 = getLocale();
|
|
@@ -2954,14 +2956,14 @@ function postalCode(options) {
|
|
|
2954
2956
|
}
|
|
2955
2957
|
return processed;
|
|
2956
2958
|
};
|
|
2957
|
-
const baseSchema =
|
|
2959
|
+
const baseSchema = isRequired ? z15.preprocess(preprocessFn, z15.string()) : z15.preprocess(preprocessFn, z15.string().nullable());
|
|
2958
2960
|
const schema = baseSchema.refine((val) => {
|
|
2959
2961
|
if (val === null) return true;
|
|
2960
|
-
if (
|
|
2962
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
2961
2963
|
throw new z15.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
2962
2964
|
}
|
|
2963
2965
|
if (val === null) return true;
|
|
2964
|
-
if (!
|
|
2966
|
+
if (!isRequired && val === "") return true;
|
|
2965
2967
|
const cleanValue = val.replace(/[-\s]/g, "");
|
|
2966
2968
|
if (format === "3" && cleanValue.length !== 3) {
|
|
2967
2969
|
throw new z15.ZodError([{ code: "custom", message: getMessage("format3Only"), path: [] }]);
|
|
@@ -3047,9 +3049,10 @@ var validateTaiwanTel = (value) => {
|
|
|
3047
3049
|
}
|
|
3048
3050
|
return false;
|
|
3049
3051
|
};
|
|
3050
|
-
function tel(options) {
|
|
3051
|
-
const {
|
|
3052
|
-
const
|
|
3052
|
+
function tel(required, options) {
|
|
3053
|
+
const { whitelist, transform, defaultValue, i18n } = options ?? {};
|
|
3054
|
+
const isRequired = required ?? false;
|
|
3055
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
3053
3056
|
const getMessage = (key, params) => {
|
|
3054
3057
|
if (i18n) {
|
|
3055
3058
|
const currentLocale2 = getLocale();
|
|
@@ -3070,7 +3073,7 @@ function tel(options) {
|
|
|
3070
3073
|
if (whitelist && whitelist.includes("")) {
|
|
3071
3074
|
return "";
|
|
3072
3075
|
}
|
|
3073
|
-
if (!
|
|
3076
|
+
if (!isRequired) {
|
|
3074
3077
|
return actualDefaultValue;
|
|
3075
3078
|
}
|
|
3076
3079
|
return actualDefaultValue;
|
|
@@ -3080,14 +3083,14 @@ function tel(options) {
|
|
|
3080
3083
|
}
|
|
3081
3084
|
return processed;
|
|
3082
3085
|
};
|
|
3083
|
-
const baseSchema =
|
|
3086
|
+
const baseSchema = isRequired ? z16.preprocess(preprocessFn, z16.string()) : z16.preprocess(preprocessFn, z16.string().nullable());
|
|
3084
3087
|
const schema = baseSchema.refine((val) => {
|
|
3085
3088
|
if (val === null) return true;
|
|
3086
|
-
if (
|
|
3089
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
3087
3090
|
throw new z16.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
3088
3091
|
}
|
|
3089
3092
|
if (val === null) return true;
|
|
3090
|
-
if (!
|
|
3093
|
+
if (!isRequired && val === "") return true;
|
|
3091
3094
|
if (whitelist && whitelist.length > 0) {
|
|
3092
3095
|
if (whitelist.includes(val)) {
|
|
3093
3096
|
return true;
|
|
@@ -3144,9 +3147,10 @@ var validateTaiwanFax = (value) => {
|
|
|
3144
3147
|
}
|
|
3145
3148
|
return false;
|
|
3146
3149
|
};
|
|
3147
|
-
function fax(options) {
|
|
3148
|
-
const {
|
|
3149
|
-
const
|
|
3150
|
+
function fax(required, options) {
|
|
3151
|
+
const { whitelist, transform, defaultValue, i18n } = options ?? {};
|
|
3152
|
+
const isRequired = required ?? false;
|
|
3153
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
3150
3154
|
const getMessage = (key, params) => {
|
|
3151
3155
|
if (i18n) {
|
|
3152
3156
|
const currentLocale2 = getLocale();
|
|
@@ -3167,7 +3171,7 @@ function fax(options) {
|
|
|
3167
3171
|
if (whitelist && whitelist.includes("")) {
|
|
3168
3172
|
return "";
|
|
3169
3173
|
}
|
|
3170
|
-
if (!
|
|
3174
|
+
if (!isRequired) {
|
|
3171
3175
|
return actualDefaultValue;
|
|
3172
3176
|
}
|
|
3173
3177
|
return actualDefaultValue;
|
|
@@ -3177,14 +3181,14 @@ function fax(options) {
|
|
|
3177
3181
|
}
|
|
3178
3182
|
return processed;
|
|
3179
3183
|
};
|
|
3180
|
-
const baseSchema =
|
|
3184
|
+
const baseSchema = isRequired ? z17.preprocess(preprocessFn, z17.string()) : z17.preprocess(preprocessFn, z17.string().nullable());
|
|
3181
3185
|
const schema = baseSchema.refine((val) => {
|
|
3182
3186
|
if (val === null) return true;
|
|
3183
|
-
if (
|
|
3187
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
3184
3188
|
throw new z17.ZodError([{ code: "custom", message: getMessage("required"), path: [] }]);
|
|
3185
3189
|
}
|
|
3186
3190
|
if (val === null) return true;
|
|
3187
|
-
if (!
|
|
3191
|
+
if (!isRequired && val === "") return true;
|
|
3188
3192
|
if (whitelist && whitelist.length > 0) {
|
|
3189
3193
|
if (whitelist.includes(val)) {
|
|
3190
3194
|
return true;
|
package/package.json
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hy_ong/zod-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Zod
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A comprehensive TypeScript library providing pre-built Zod validation schemas with full internationalization support for common data types and Taiwan-specific formats",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"hy_ong",
|
|
7
6
|
"zod",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
7
|
+
"validation",
|
|
8
|
+
"schema",
|
|
9
|
+
"typescript",
|
|
10
|
+
"taiwan",
|
|
11
|
+
"i18n",
|
|
12
|
+
"form-validation",
|
|
13
|
+
"email",
|
|
14
|
+
"password",
|
|
15
|
+
"phone",
|
|
16
|
+
"postal-code",
|
|
17
|
+
"national-id",
|
|
18
|
+
"business-id"
|
|
10
19
|
],
|
|
11
20
|
"homepage": "https://github.com/hy-ong/zod-kit#readme",
|
|
12
21
|
"bugs": {
|
|
@@ -38,6 +47,7 @@
|
|
|
38
47
|
},
|
|
39
48
|
"dependencies": {
|
|
40
49
|
"dayjs": "^1.11.14",
|
|
50
|
+
"glob": "^11.0.3",
|
|
41
51
|
"zod": "^4.1.4"
|
|
42
52
|
},
|
|
43
53
|
"devDependencies": {
|