@hy_ong/zod-kit 0.1.16 → 0.2.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/.github/workflows/ci.yml +24 -0
- package/CLAUDE.md +184 -0
- package/dist/chunk-36NWHESN.js +124 -0
- package/dist/chunk-4LYZAO3P.js +165 -0
- package/dist/chunk-5GAZQDVS.cjs +206 -0
- package/dist/chunk-5LS4DSRQ.cjs +127 -0
- package/dist/chunk-5OGW2ERW.js +181 -0
- package/dist/chunk-5ZEKWPSE.cjs +69 -0
- package/dist/chunk-6OGDPSWT.js +135 -0
- package/dist/chunk-6X22I6NQ.cjs +136 -0
- package/dist/chunk-77KZUPPN.cjs +177 -0
- package/dist/chunk-AANSHH2O.cjs +165 -0
- package/dist/chunk-AI72FMOF.cjs +130 -0
- package/dist/chunk-AWV2IT66.js +146 -0
- package/dist/chunk-B3U5G3AA.js +160 -0
- package/dist/chunk-CFFCBWYL.cjs +99 -0
- package/dist/chunk-DPXRMSB2.js +130 -0
- package/dist/chunk-DRXPGQM6.cjs +135 -0
- package/dist/chunk-EAU42EVH.js +161 -0
- package/dist/chunk-FC6VDOC7.js +206 -0
- package/dist/chunk-FVO4743A.cjs +134 -0
- package/dist/chunk-G6DV7LX7.cjs +161 -0
- package/dist/chunk-I2RJMDXN.js +90 -0
- package/dist/chunk-IJEEM3DI.js +136 -0
- package/dist/chunk-JBNCMS42.cjs +151 -0
- package/dist/chunk-JZ2SHRGZ.js +87 -0
- package/dist/chunk-KARFFIMP.js +696 -0
- package/dist/chunk-LIQSVJLS.js +177 -0
- package/dist/chunk-LKPXHW5N.cjs +181 -0
- package/dist/chunk-MAQRXYE6.js +118 -0
- package/dist/chunk-MCDESS3T.js +69 -0
- package/dist/chunk-MG25BEV4.cjs +160 -0
- package/dist/chunk-NKCYXBGX.js +99 -0
- package/dist/chunk-OEK7QSQP.js +75 -0
- package/dist/chunk-OMFQ7Z63.cjs +696 -0
- package/dist/chunk-OP4KV3BY.cjs +124 -0
- package/dist/chunk-P2NONIMS.js +257 -0
- package/dist/chunk-P364KRO5.js +61 -0
- package/dist/chunk-PGSDXR2I.js +71 -0
- package/dist/chunk-PL2GERLG.cjs +61 -0
- package/dist/chunk-R5G4V7C6.cjs +75 -0
- package/dist/chunk-RKHX3DGH.js +127 -0
- package/dist/chunk-TSHL7ZO2.js +134 -0
- package/dist/chunk-UFNVCUPQ.cjs +301 -0
- package/dist/chunk-VCRKYMJM.js +301 -0
- package/dist/chunk-VDOAPLA6.cjs +257 -0
- package/dist/chunk-VP5CCP5F.cjs +90 -0
- package/dist/chunk-W2EWMV3A.cjs +87 -0
- package/dist/chunk-WWRFBLCR.cjs +146 -0
- package/dist/chunk-YALLOVNO.cjs +118 -0
- package/dist/chunk-YAU6JCYL.cjs +71 -0
- package/dist/chunk-YWV2BBXN.cjs +2526 -0
- package/dist/chunk-ZBOQCXD4.js +2526 -0
- package/dist/chunk-ZFQQXWNB.js +151 -0
- package/dist/common/boolean.cjs +7 -0
- package/dist/common/boolean.d.cts +119 -0
- package/dist/common/boolean.d.ts +119 -0
- package/dist/common/boolean.js +7 -0
- package/dist/common/color.cjs +9 -0
- package/dist/common/color.d.cts +26 -0
- package/dist/common/color.d.ts +26 -0
- package/dist/common/color.js +9 -0
- package/dist/common/coordinate.cjs +11 -0
- package/dist/common/coordinate.d.cts +23 -0
- package/dist/common/coordinate.d.ts +23 -0
- package/dist/common/coordinate.js +11 -0
- package/dist/common/credit-card.cjs +11 -0
- package/dist/common/credit-card.d.cts +22 -0
- package/dist/common/credit-card.d.ts +22 -0
- package/dist/common/credit-card.js +11 -0
- package/dist/common/date.cjs +7 -0
- package/dist/common/date.d.cts +174 -0
- package/dist/common/date.d.ts +174 -0
- package/dist/common/date.js +7 -0
- package/dist/common/datetime.cjs +15 -0
- package/dist/common/datetime.d.cts +301 -0
- package/dist/common/datetime.d.ts +301 -0
- package/dist/common/datetime.js +15 -0
- package/dist/common/email.cjs +7 -0
- package/dist/common/email.d.cts +149 -0
- package/dist/common/email.d.ts +149 -0
- package/dist/common/email.js +7 -0
- package/dist/common/file.cjs +7 -0
- package/dist/common/file.d.cts +178 -0
- package/dist/common/file.d.ts +178 -0
- package/dist/common/file.js +7 -0
- package/dist/common/id.cjs +13 -0
- package/dist/common/id.d.cts +262 -0
- package/dist/common/id.d.ts +262 -0
- package/dist/common/id.js +13 -0
- package/dist/common/ip.cjs +11 -0
- package/dist/common/ip.d.cts +25 -0
- package/dist/common/ip.d.ts +25 -0
- package/dist/common/ip.js +11 -0
- package/dist/common/number.cjs +7 -0
- package/dist/common/number.d.cts +167 -0
- package/dist/common/number.d.ts +167 -0
- package/dist/common/number.js +7 -0
- package/dist/common/password.cjs +7 -0
- package/dist/common/password.d.cts +192 -0
- package/dist/common/password.d.ts +192 -0
- package/dist/common/password.js +7 -0
- package/dist/common/text.cjs +7 -0
- package/dist/common/text.d.cts +156 -0
- package/dist/common/text.d.ts +156 -0
- package/dist/common/text.js +7 -0
- package/dist/common/time.cjs +15 -0
- package/dist/common/time.d.cts +268 -0
- package/dist/common/time.d.ts +268 -0
- package/dist/common/time.js +15 -0
- package/dist/common/url.cjs +7 -0
- package/dist/common/url.d.cts +196 -0
- package/dist/common/url.d.ts +196 -0
- package/dist/common/url.js +7 -0
- package/dist/config-CABSSvAp.d.cts +5 -0
- package/dist/config-CABSSvAp.d.ts +5 -0
- package/dist/index.cjs +180 -3599
- package/dist/index.d.cts +28 -3150
- package/dist/index.d.ts +28 -3150
- package/dist/index.js +135 -3475
- package/dist/taiwan/bank-account.cjs +11 -0
- package/dist/taiwan/bank-account.d.cts +22 -0
- package/dist/taiwan/bank-account.d.ts +22 -0
- package/dist/taiwan/bank-account.js +11 -0
- package/dist/taiwan/business-id.cjs +9 -0
- package/dist/taiwan/business-id.d.cts +133 -0
- package/dist/taiwan/business-id.d.ts +133 -0
- package/dist/taiwan/business-id.js +9 -0
- package/dist/taiwan/fax.cjs +9 -0
- package/dist/taiwan/fax.d.cts +157 -0
- package/dist/taiwan/fax.d.ts +157 -0
- package/dist/taiwan/fax.js +9 -0
- package/dist/taiwan/invoice.cjs +9 -0
- package/dist/taiwan/invoice.d.cts +17 -0
- package/dist/taiwan/invoice.d.ts +17 -0
- package/dist/taiwan/invoice.js +9 -0
- package/dist/taiwan/license-plate.cjs +9 -0
- package/dist/taiwan/license-plate.d.cts +19 -0
- package/dist/taiwan/license-plate.d.ts +19 -0
- package/dist/taiwan/license-plate.js +9 -0
- package/dist/taiwan/mobile.cjs +9 -0
- package/dist/taiwan/mobile.d.cts +146 -0
- package/dist/taiwan/mobile.d.ts +146 -0
- package/dist/taiwan/mobile.js +9 -0
- package/dist/taiwan/national-id.cjs +15 -0
- package/dist/taiwan/national-id.d.cts +214 -0
- package/dist/taiwan/national-id.d.ts +214 -0
- package/dist/taiwan/national-id.js +15 -0
- package/dist/taiwan/passport.cjs +9 -0
- package/dist/taiwan/passport.d.cts +19 -0
- package/dist/taiwan/passport.d.ts +19 -0
- package/dist/taiwan/passport.js +9 -0
- package/dist/taiwan/postal-code.cjs +17 -0
- package/dist/taiwan/postal-code.d.cts +237 -0
- package/dist/taiwan/postal-code.d.ts +237 -0
- package/dist/taiwan/postal-code.js +17 -0
- package/dist/taiwan/tel.cjs +9 -0
- package/dist/taiwan/tel.d.cts +162 -0
- package/dist/taiwan/tel.d.ts +162 -0
- package/dist/taiwan/tel.js +9 -0
- package/package.json +128 -2
- package/src/config.ts +2 -2
- package/src/i18n/index.ts +18 -2
- package/src/i18n/locales/en-GB.json +247 -0
- package/src/i18n/locales/{en.json → en-US.json} +43 -0
- package/src/i18n/locales/id-ID.json +247 -0
- package/src/i18n/locales/ja-JP.json +247 -0
- package/src/i18n/locales/ko-KR.json +247 -0
- package/src/i18n/locales/ms-MY.json +247 -0
- package/src/i18n/locales/th-TH.json +247 -0
- package/src/i18n/locales/vi-VN.json +247 -0
- package/src/i18n/locales/zh-CN.json +247 -0
- package/src/i18n/locales/zh-TW.json +43 -0
- package/src/index.ts +10 -2
- package/src/validators/common/boolean.ts +1 -1
- package/src/validators/common/color.ts +192 -0
- package/src/validators/common/coordinate.ts +159 -0
- package/src/validators/common/credit-card.ts +134 -0
- package/src/validators/common/date.ts +1 -1
- package/src/validators/common/datetime.ts +1 -1
- package/src/validators/common/email.ts +1 -1
- package/src/validators/common/file.ts +1 -1
- package/src/validators/common/id.ts +1 -1
- package/src/validators/common/ip.ts +210 -0
- package/src/validators/common/number.ts +1 -1
- package/src/validators/common/password.ts +1 -1
- package/src/validators/common/text.ts +1 -1
- package/src/validators/common/time.ts +1 -1
- package/src/validators/common/url.ts +1 -1
- package/src/validators/taiwan/bank-account.ts +176 -0
- package/src/validators/taiwan/business-id.ts +1 -1
- package/src/validators/taiwan/fax.ts +1 -1
- package/src/validators/taiwan/invoice.ts +84 -0
- package/src/validators/taiwan/license-plate.ts +110 -0
- package/src/validators/taiwan/mobile.ts +1 -1
- package/src/validators/taiwan/national-id.ts +1 -1
- package/src/validators/taiwan/passport.ts +103 -0
- package/src/validators/taiwan/postal-code.ts +1 -1
- package/src/validators/taiwan/tel.ts +1 -1
- package/tests/common/boolean.test.ts +6 -6
- package/tests/common/color.test.ts +587 -0
- package/tests/common/coordinate.test.ts +345 -0
- package/tests/common/credit-card.test.ts +378 -0
- package/tests/common/date.test.ts +8 -8
- package/tests/common/datetime.test.ts +7 -7
- package/tests/common/email.test.ts +6 -6
- package/tests/common/file.test.ts +6 -6
- package/tests/common/id.test.ts +5 -5
- package/tests/common/ip.test.ts +419 -0
- package/tests/common/number.test.ts +6 -6
- package/tests/common/password.test.ts +5 -5
- package/tests/common/text.test.ts +7 -7
- package/tests/common/time.test.ts +7 -7
- package/tests/common/url.test.ts +10 -10
- package/tests/taiwan/bank-account.test.ts +286 -0
- package/tests/taiwan/business-id.test.ts +4 -4
- package/tests/taiwan/fax.test.ts +7 -7
- package/tests/taiwan/invoice.test.ts +227 -0
- package/tests/taiwan/license-plate.test.ts +280 -0
- package/tests/taiwan/mobile.test.ts +7 -7
- package/tests/taiwan/national-id.test.ts +4 -4
- package/tests/taiwan/passport.test.ts +277 -0
- package/tests/taiwan/postal-code.test.ts +6 -6
- package/tests/taiwan/tel.test.ts +7 -7
- package/tsup.config.ts +36 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLocale,
|
|
3
|
+
t
|
|
4
|
+
} from "./chunk-ZBOQCXD4.js";
|
|
5
|
+
|
|
6
|
+
// src/validators/taiwan/fax.ts
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
var validateTaiwanFax = (value) => {
|
|
9
|
+
const cleanValue = value.replace(/[-\s]/g, "");
|
|
10
|
+
if (!/^0\d{7,10}$/.test(cleanValue)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const areaCode4 = cleanValue.substring(0, 4);
|
|
14
|
+
if (areaCode4 === "0836") {
|
|
15
|
+
return cleanValue.length === 9 && /^0836\d{5}$/.test(cleanValue);
|
|
16
|
+
}
|
|
17
|
+
const areaCode3 = cleanValue.substring(0, 3);
|
|
18
|
+
if (areaCode3 === "037") {
|
|
19
|
+
const firstDigit = cleanValue[3];
|
|
20
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^037[2-9]\d{5,6}$/.test(cleanValue);
|
|
24
|
+
}
|
|
25
|
+
if (areaCode3 === "049") {
|
|
26
|
+
return cleanValue.length === 10 && /^049\d{7}$/.test(cleanValue);
|
|
27
|
+
}
|
|
28
|
+
if (areaCode3 === "082") {
|
|
29
|
+
return cleanValue.length === 9 && /^082\d{6}$/.test(cleanValue);
|
|
30
|
+
}
|
|
31
|
+
if (areaCode3 === "089") {
|
|
32
|
+
return cleanValue.length === 9 && /^089\d{6}$/.test(cleanValue);
|
|
33
|
+
}
|
|
34
|
+
const areaCode2 = cleanValue.substring(0, 2);
|
|
35
|
+
if (areaCode2 === "02") {
|
|
36
|
+
const firstDigit = cleanValue[2];
|
|
37
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return cleanValue.length === 10 && /^02[2-9]\d{7}$/.test(cleanValue);
|
|
41
|
+
}
|
|
42
|
+
if (areaCode2 === "03") {
|
|
43
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^03\d{7,8}$/.test(cleanValue);
|
|
44
|
+
}
|
|
45
|
+
if (areaCode2 === "04") {
|
|
46
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^04\d{7,8}$/.test(cleanValue);
|
|
47
|
+
}
|
|
48
|
+
if (areaCode2 === "05") {
|
|
49
|
+
return cleanValue.length === 9 && /^05\d{7}$/.test(cleanValue);
|
|
50
|
+
}
|
|
51
|
+
if (areaCode2 === "06") {
|
|
52
|
+
return cleanValue.length === 9 && /^06\d{7}$/.test(cleanValue);
|
|
53
|
+
}
|
|
54
|
+
if (areaCode2 === "07") {
|
|
55
|
+
const firstDigit = cleanValue[2];
|
|
56
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return cleanValue.length === 9 && /^07[2-9]\d{6}$/.test(cleanValue);
|
|
60
|
+
}
|
|
61
|
+
if (areaCode2 === "08") {
|
|
62
|
+
const firstDigit = cleanValue[2];
|
|
63
|
+
if (firstDigit !== "4" && firstDigit !== "7" && firstDigit !== "8") {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return cleanValue.length === 9 && /^08[478]\d{6}$/.test(cleanValue);
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
};
|
|
70
|
+
function twFax(required, options) {
|
|
71
|
+
const { whitelist, transform, defaultValue, i18n } = options ?? {};
|
|
72
|
+
const isRequired = required ?? false;
|
|
73
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
74
|
+
const getMessage = (key, params) => {
|
|
75
|
+
if (i18n) {
|
|
76
|
+
const currentLocale = getLocale();
|
|
77
|
+
const customMessages = i18n[currentLocale];
|
|
78
|
+
if (customMessages && customMessages[key]) {
|
|
79
|
+
const template = customMessages[key];
|
|
80
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return t(`taiwan.fax.${key}`, params);
|
|
84
|
+
};
|
|
85
|
+
const preprocessFn = (val) => {
|
|
86
|
+
if (val === null || val === void 0) {
|
|
87
|
+
return actualDefaultValue;
|
|
88
|
+
}
|
|
89
|
+
let processed = String(val).trim();
|
|
90
|
+
if (processed === "") {
|
|
91
|
+
if (whitelist && whitelist.includes("")) {
|
|
92
|
+
return "";
|
|
93
|
+
}
|
|
94
|
+
if (!isRequired) {
|
|
95
|
+
return actualDefaultValue;
|
|
96
|
+
}
|
|
97
|
+
return actualDefaultValue;
|
|
98
|
+
}
|
|
99
|
+
if (transform) {
|
|
100
|
+
processed = transform(processed);
|
|
101
|
+
}
|
|
102
|
+
return processed;
|
|
103
|
+
};
|
|
104
|
+
const baseSchema = isRequired ? z.preprocess(preprocessFn, z.string()) : z.preprocess(preprocessFn, z.string().nullable());
|
|
105
|
+
const schema = baseSchema.superRefine((val, ctx) => {
|
|
106
|
+
if (val === null) return;
|
|
107
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
108
|
+
ctx.addIssue({ code: "custom", message: getMessage("required") });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (val === null) return;
|
|
112
|
+
if (!isRequired && val === "") return;
|
|
113
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (!validateTaiwanFax(val)) {
|
|
117
|
+
ctx.addIssue({ code: "custom", message: getMessage("invalid") });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return schema;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export {
|
|
125
|
+
validateTaiwanFax,
|
|
126
|
+
twFax
|
|
127
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLocale,
|
|
3
|
+
t
|
|
4
|
+
} from "./chunk-ZBOQCXD4.js";
|
|
5
|
+
|
|
6
|
+
// src/validators/common/color.ts
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
var HEX_RGB = /^#[0-9a-f]{3}$/i;
|
|
9
|
+
var HEX_RRGGBB = /^#[0-9a-f]{6}$/i;
|
|
10
|
+
var HEX_RRGGBBAA = /^#[0-9a-f]{8}$/i;
|
|
11
|
+
function isValidByte(n) {
|
|
12
|
+
return Number.isInteger(n) && n >= 0 && n <= 255;
|
|
13
|
+
}
|
|
14
|
+
function isValidAlpha(n) {
|
|
15
|
+
return n >= 0 && n <= 1;
|
|
16
|
+
}
|
|
17
|
+
function isValidHue(n) {
|
|
18
|
+
return n >= 0 && n <= 360;
|
|
19
|
+
}
|
|
20
|
+
function isValidPercentage(n) {
|
|
21
|
+
return n >= 0 && n <= 100;
|
|
22
|
+
}
|
|
23
|
+
function validateHex(value, allowAlpha) {
|
|
24
|
+
if (HEX_RGB.test(value) || HEX_RRGGBB.test(value)) return true;
|
|
25
|
+
if (allowAlpha && HEX_RRGGBBAA.test(value)) return true;
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
function validateRgb(value, allowAlpha) {
|
|
29
|
+
const rgbaMatch = value.match(/^rgba?\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)\s*(?:,\s*(-?\d+(?:\.\d+)?))?\s*\)$/);
|
|
30
|
+
if (!rgbaMatch) return false;
|
|
31
|
+
const r = Number(rgbaMatch[1]);
|
|
32
|
+
const g = Number(rgbaMatch[2]);
|
|
33
|
+
const b = Number(rgbaMatch[3]);
|
|
34
|
+
const a = rgbaMatch[4] !== void 0 ? Number(rgbaMatch[4]) : void 0;
|
|
35
|
+
if (!isValidByte(r) || !isValidByte(g) || !isValidByte(b)) return false;
|
|
36
|
+
if (a !== void 0) {
|
|
37
|
+
if (!allowAlpha) return false;
|
|
38
|
+
if (!isValidAlpha(a)) return false;
|
|
39
|
+
}
|
|
40
|
+
if (value.startsWith("rgba(") && a === void 0) return false;
|
|
41
|
+
if (value.startsWith("rgb(") && a !== void 0) return false;
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
function validateHsl(value, allowAlpha) {
|
|
45
|
+
const hslaMatch = value.match(/^hsla?\(\s*(-?\d+(?:\.\d+)?)\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*(?:,\s*(-?\d+(?:\.\d+)?))?\s*\)$/);
|
|
46
|
+
if (!hslaMatch) return false;
|
|
47
|
+
const h = Number(hslaMatch[1]);
|
|
48
|
+
const s = Number(hslaMatch[2]);
|
|
49
|
+
const l = Number(hslaMatch[3]);
|
|
50
|
+
const a = hslaMatch[4] !== void 0 ? Number(hslaMatch[4]) : void 0;
|
|
51
|
+
if (!isValidHue(h) || !isValidPercentage(s) || !isValidPercentage(l)) return false;
|
|
52
|
+
if (a !== void 0) {
|
|
53
|
+
if (!allowAlpha) return false;
|
|
54
|
+
if (!isValidAlpha(a)) return false;
|
|
55
|
+
}
|
|
56
|
+
if (value.startsWith("hsla(") && a === void 0) return false;
|
|
57
|
+
if (value.startsWith("hsl(") && a !== void 0) return false;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
function validateColor(value, formats, allowAlpha) {
|
|
61
|
+
const hasAny = formats.includes("any");
|
|
62
|
+
if (hasAny) {
|
|
63
|
+
if (validateHex(value, allowAlpha)) return { valid: true };
|
|
64
|
+
if (validateRgb(value, allowAlpha)) return { valid: true };
|
|
65
|
+
if (validateHsl(value, allowAlpha)) return { valid: true };
|
|
66
|
+
return { valid: false };
|
|
67
|
+
}
|
|
68
|
+
for (const format of formats) {
|
|
69
|
+
if (format === "hex" && validateHex(value, allowAlpha)) return { valid: true };
|
|
70
|
+
if (format === "rgb" && validateRgb(value, allowAlpha)) return { valid: true };
|
|
71
|
+
if (format === "hsl" && validateHsl(value, allowAlpha)) return { valid: true };
|
|
72
|
+
}
|
|
73
|
+
if (formats.length === 1) {
|
|
74
|
+
return { valid: false, failedFormat: formats[0] };
|
|
75
|
+
}
|
|
76
|
+
return { valid: false };
|
|
77
|
+
}
|
|
78
|
+
function color(required, options) {
|
|
79
|
+
const { format = "any", allowAlpha = true, transform, defaultValue, i18n } = options ?? {};
|
|
80
|
+
const isRequired = required ?? false;
|
|
81
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
82
|
+
const formats = Array.isArray(format) ? format : [format];
|
|
83
|
+
const getMessage = (key, params) => {
|
|
84
|
+
if (i18n) {
|
|
85
|
+
const currentLocale = getLocale();
|
|
86
|
+
const customMessages = i18n[currentLocale];
|
|
87
|
+
if (customMessages && customMessages[key]) {
|
|
88
|
+
const template = customMessages[key];
|
|
89
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return t(`common.color.${key}`, params);
|
|
93
|
+
};
|
|
94
|
+
const preprocessFn = (val) => {
|
|
95
|
+
if (val === "" || val === null || val === void 0) {
|
|
96
|
+
return actualDefaultValue;
|
|
97
|
+
}
|
|
98
|
+
let processed = String(val).trim();
|
|
99
|
+
if (processed === "" && !required) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
if (transform) {
|
|
103
|
+
processed = transform(processed);
|
|
104
|
+
}
|
|
105
|
+
return processed;
|
|
106
|
+
};
|
|
107
|
+
const baseSchema = isRequired ? z.preprocess(preprocessFn, z.string()) : z.preprocess(preprocessFn, z.string().nullable());
|
|
108
|
+
const schema = baseSchema.superRefine((val, ctx) => {
|
|
109
|
+
if (val === null) return;
|
|
110
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
111
|
+
ctx.addIssue({ code: "custom", message: getMessage("required") });
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!isRequired && val === "") return;
|
|
115
|
+
const result = validateColor(val, formats, allowAlpha);
|
|
116
|
+
if (!result.valid) {
|
|
117
|
+
const formatMessageMap = {
|
|
118
|
+
hex: "notHex",
|
|
119
|
+
rgb: "notRgb",
|
|
120
|
+
hsl: "notHsl",
|
|
121
|
+
any: "invalid"
|
|
122
|
+
};
|
|
123
|
+
const messageKey = result.failedFormat ? formatMessageMap[result.failedFormat] : "invalid";
|
|
124
|
+
ctx.addIssue({ code: "custom", message: getMessage(messageKey) });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return schema;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export {
|
|
132
|
+
validateColor,
|
|
133
|
+
color
|
|
134
|
+
};
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkYWV2BBXNcjs = require('./chunk-YWV2BBXN.cjs');
|
|
5
|
+
|
|
6
|
+
// src/validators/common/datetime.ts
|
|
7
|
+
var _zod = require('zod');
|
|
8
|
+
var _dayjs = require('dayjs'); var _dayjs2 = _interopRequireDefault(_dayjs);
|
|
9
|
+
var _customParseFormat = require('dayjs/plugin/customParseFormat'); var _customParseFormat2 = _interopRequireDefault(_customParseFormat);
|
|
10
|
+
var _isSameOrAfter = require('dayjs/plugin/isSameOrAfter'); var _isSameOrAfter2 = _interopRequireDefault(_isSameOrAfter);
|
|
11
|
+
var _isSameOrBefore = require('dayjs/plugin/isSameOrBefore'); var _isSameOrBefore2 = _interopRequireDefault(_isSameOrBefore);
|
|
12
|
+
var _isToday = require('dayjs/plugin/isToday'); var _isToday2 = _interopRequireDefault(_isToday);
|
|
13
|
+
var _weekday = require('dayjs/plugin/weekday'); var _weekday2 = _interopRequireDefault(_weekday);
|
|
14
|
+
var _timezone = require('dayjs/plugin/timezone'); var _timezone2 = _interopRequireDefault(_timezone);
|
|
15
|
+
var _utc = require('dayjs/plugin/utc'); var _utc2 = _interopRequireDefault(_utc);
|
|
16
|
+
_dayjs2.default.extend(_isSameOrAfter2.default);
|
|
17
|
+
_dayjs2.default.extend(_isSameOrBefore2.default);
|
|
18
|
+
_dayjs2.default.extend(_customParseFormat2.default);
|
|
19
|
+
_dayjs2.default.extend(_isToday2.default);
|
|
20
|
+
_dayjs2.default.extend(_weekday2.default);
|
|
21
|
+
_dayjs2.default.extend(_timezone2.default);
|
|
22
|
+
_dayjs2.default.extend(_utc2.default);
|
|
23
|
+
var DATETIME_PATTERNS = {
|
|
24
|
+
"YYYY-MM-DD HH:mm": /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/,
|
|
25
|
+
"YYYY-MM-DD HH:mm:ss": /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/,
|
|
26
|
+
"YYYY-MM-DD hh:mm A": /^\d{4}-\d{2}-\d{2} \d{1,2}:\d{2} (AM|PM)$/i,
|
|
27
|
+
"YYYY-MM-DD hh:mm:ss A": /^\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2} (AM|PM)$/i,
|
|
28
|
+
"DD/MM/YYYY HH:mm": /^\d{1,2}\/\d{1,2}\/\d{4} \d{2}:\d{2}$/,
|
|
29
|
+
"DD/MM/YYYY HH:mm:ss": /^\d{1,2}\/\d{1,2}\/\d{4} \d{2}:\d{2}:\d{2}$/,
|
|
30
|
+
"DD/MM/YYYY hh:mm A": /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2} (AM|PM)$/i,
|
|
31
|
+
"MM/DD/YYYY HH:mm": /^\d{1,2}\/\d{1,2}\/\d{4} \d{2}:\d{2}$/,
|
|
32
|
+
"MM/DD/YYYY hh:mm A": /^\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2} (AM|PM)$/i,
|
|
33
|
+
"YYYY/MM/DD HH:mm": /^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}$/,
|
|
34
|
+
"DD-MM-YYYY HH:mm": /^\d{1,2}-\d{1,2}-\d{4} \d{2}:\d{2}$/,
|
|
35
|
+
"MM-DD-YYYY HH:mm": /^\d{1,2}-\d{1,2}-\d{4} \d{2}:\d{2}$/,
|
|
36
|
+
ISO: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/,
|
|
37
|
+
RFC: /^[A-Za-z]{3}, \d{1,2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} [A-Z]{3}$/,
|
|
38
|
+
UNIX: /^\d{10}$/
|
|
39
|
+
};
|
|
40
|
+
var validateDateTimeFormat = (value, format) => {
|
|
41
|
+
const pattern = DATETIME_PATTERNS[format];
|
|
42
|
+
if (!pattern.test(value.trim())) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const parsed = parseDateTimeValue(value, format);
|
|
46
|
+
return parsed !== null;
|
|
47
|
+
};
|
|
48
|
+
var parseDateTimeValue = (value, format, timezone2) => {
|
|
49
|
+
try {
|
|
50
|
+
const cleanValue = value.trim();
|
|
51
|
+
let parsed;
|
|
52
|
+
switch (format) {
|
|
53
|
+
case "ISO":
|
|
54
|
+
parsed = _dayjs2.default.call(void 0, cleanValue);
|
|
55
|
+
break;
|
|
56
|
+
case "RFC":
|
|
57
|
+
parsed = _dayjs2.default.call(void 0, cleanValue);
|
|
58
|
+
break;
|
|
59
|
+
case "UNIX":
|
|
60
|
+
parsed = _dayjs2.default.unix(parseInt(cleanValue, 10));
|
|
61
|
+
break;
|
|
62
|
+
default:
|
|
63
|
+
parsed = _dayjs2.default.call(void 0, cleanValue, format, true);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
if (!parsed.isValid()) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (timezone2) {
|
|
70
|
+
parsed = parsed.tz(timezone2);
|
|
71
|
+
}
|
|
72
|
+
return parsed;
|
|
73
|
+
} catch (e) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
var normalizeDateTimeValue = (value, format, timezone2) => {
|
|
78
|
+
const parsed = parseDateTimeValue(value, format, timezone2);
|
|
79
|
+
if (!parsed) return null;
|
|
80
|
+
switch (format) {
|
|
81
|
+
case "ISO":
|
|
82
|
+
return parsed.toISOString();
|
|
83
|
+
case "RFC":
|
|
84
|
+
return parsed.format("ddd, DD MMM YYYY HH:mm:ss [GMT]");
|
|
85
|
+
case "UNIX":
|
|
86
|
+
return parsed.unix().toString();
|
|
87
|
+
default:
|
|
88
|
+
return parsed.format(format);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
function datetime(required, options) {
|
|
92
|
+
const {
|
|
93
|
+
format = "YYYY-MM-DD HH:mm",
|
|
94
|
+
min,
|
|
95
|
+
max,
|
|
96
|
+
minHour,
|
|
97
|
+
maxHour,
|
|
98
|
+
allowedHours,
|
|
99
|
+
minuteStep,
|
|
100
|
+
timezone: timezone2,
|
|
101
|
+
includes,
|
|
102
|
+
excludes,
|
|
103
|
+
regex,
|
|
104
|
+
trimMode = "trim",
|
|
105
|
+
casing = "none",
|
|
106
|
+
mustBePast,
|
|
107
|
+
mustBeFuture,
|
|
108
|
+
mustBeToday,
|
|
109
|
+
mustNotBeToday,
|
|
110
|
+
weekdaysOnly,
|
|
111
|
+
weekendsOnly,
|
|
112
|
+
whitelist,
|
|
113
|
+
whitelistOnly = false,
|
|
114
|
+
transform,
|
|
115
|
+
defaultValue,
|
|
116
|
+
i18n
|
|
117
|
+
} = _nullishCoalesce(options, () => ( {}));
|
|
118
|
+
const isRequired = _nullishCoalesce(required, () => ( false));
|
|
119
|
+
const actualDefaultValue = _nullishCoalesce(defaultValue, () => ( (isRequired ? "" : null)));
|
|
120
|
+
const getMessage = (key, params) => {
|
|
121
|
+
if (i18n) {
|
|
122
|
+
const currentLocale = _chunkYWV2BBXNcjs.getLocale.call(void 0, );
|
|
123
|
+
const customMessages = i18n[currentLocale];
|
|
124
|
+
if (customMessages && customMessages[key]) {
|
|
125
|
+
const template = customMessages[key];
|
|
126
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => _nullishCoalesce(_optionalChain([params, 'optionalAccess', _2 => _2[k]]), () => ( "")));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return _chunkYWV2BBXNcjs.t.call(void 0, `common.datetime.${key}`, params);
|
|
130
|
+
};
|
|
131
|
+
const preprocessFn = (val) => {
|
|
132
|
+
if (val === null || val === void 0) {
|
|
133
|
+
return actualDefaultValue;
|
|
134
|
+
}
|
|
135
|
+
let processed = String(val);
|
|
136
|
+
switch (trimMode) {
|
|
137
|
+
case "trim":
|
|
138
|
+
processed = processed.trim();
|
|
139
|
+
break;
|
|
140
|
+
case "trimStart":
|
|
141
|
+
processed = processed.trimStart();
|
|
142
|
+
break;
|
|
143
|
+
case "trimEnd":
|
|
144
|
+
processed = processed.trimEnd();
|
|
145
|
+
break;
|
|
146
|
+
case "none":
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
if (processed === "") {
|
|
150
|
+
if (whitelist && whitelist.includes("")) {
|
|
151
|
+
return "";
|
|
152
|
+
}
|
|
153
|
+
if (!isRequired) {
|
|
154
|
+
return actualDefaultValue;
|
|
155
|
+
}
|
|
156
|
+
return actualDefaultValue;
|
|
157
|
+
}
|
|
158
|
+
switch (casing) {
|
|
159
|
+
case "upper":
|
|
160
|
+
processed = processed.toUpperCase();
|
|
161
|
+
break;
|
|
162
|
+
case "lower":
|
|
163
|
+
processed = processed.toLowerCase();
|
|
164
|
+
break;
|
|
165
|
+
case "none":
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
if (transform) {
|
|
169
|
+
processed = transform(processed);
|
|
170
|
+
}
|
|
171
|
+
return processed;
|
|
172
|
+
};
|
|
173
|
+
const baseSchema = isRequired ? _zod.z.preprocess(preprocessFn, _zod.z.string()) : _zod.z.preprocess(preprocessFn, _zod.z.string().nullable());
|
|
174
|
+
const schema = baseSchema.superRefine((val, ctx) => {
|
|
175
|
+
if (val === null) return;
|
|
176
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
177
|
+
ctx.addIssue({ code: "custom", message: getMessage("required") });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (val === null) return;
|
|
181
|
+
if (!isRequired && val === "") return;
|
|
182
|
+
if (whitelist && whitelist.length > 0) {
|
|
183
|
+
if (whitelist.includes(val)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (whitelistOnly) {
|
|
187
|
+
ctx.addIssue({ code: "custom", message: getMessage("notInWhitelist") });
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (regex) {
|
|
192
|
+
if (!regex.test(val)) {
|
|
193
|
+
ctx.addIssue({ code: "custom", message: getMessage("customRegex") });
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
if (!validateDateTimeFormat(val, format)) {
|
|
198
|
+
ctx.addIssue({ code: "custom", message: getMessage("format", { format }) });
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (includes && !val.includes(includes)) {
|
|
203
|
+
ctx.addIssue({ code: "custom", message: getMessage("includes", { includes }) });
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (excludes) {
|
|
207
|
+
const excludeList = Array.isArray(excludes) ? excludes : [excludes];
|
|
208
|
+
for (const exclude of excludeList) {
|
|
209
|
+
if (val.includes(exclude)) {
|
|
210
|
+
ctx.addIssue({ code: "custom", message: getMessage("excludes", { excludes: exclude }) });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (regex) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const parsed = parseDateTimeValue(val, format, timezone2);
|
|
219
|
+
if (!parsed) {
|
|
220
|
+
const pattern = DATETIME_PATTERNS[format];
|
|
221
|
+
if (!pattern.test(val.trim())) {
|
|
222
|
+
ctx.addIssue({ code: "custom", message: getMessage("format", { format }) });
|
|
223
|
+
return;
|
|
224
|
+
} else {
|
|
225
|
+
ctx.addIssue({ code: "custom", message: getMessage("invalid") });
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
const hour = parsed.hour();
|
|
230
|
+
if (minHour !== void 0 && hour < minHour) {
|
|
231
|
+
ctx.addIssue({ code: "custom", message: getMessage("hour", { minHour, maxHour: _nullishCoalesce(maxHour, () => ( 23)) }) });
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (maxHour !== void 0 && hour > maxHour) {
|
|
235
|
+
ctx.addIssue({ code: "custom", message: getMessage("hour", { minHour: _nullishCoalesce(minHour, () => ( 0)), maxHour }) });
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (allowedHours && allowedHours.length > 0) {
|
|
239
|
+
if (!allowedHours.includes(hour)) {
|
|
240
|
+
ctx.addIssue({ code: "custom", message: getMessage("hour", { allowedHours: allowedHours.join(", ") }) });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const minute = parsed.minute();
|
|
245
|
+
if (minuteStep !== void 0 && minute % minuteStep !== 0) {
|
|
246
|
+
ctx.addIssue({ code: "custom", message: getMessage("minute", { minuteStep }) });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (min) {
|
|
250
|
+
const minParsed = typeof min === "string" ? parseDateTimeValue(min, format, timezone2) : _dayjs2.default.call(void 0, min);
|
|
251
|
+
if (minParsed && parsed.isBefore(minParsed)) {
|
|
252
|
+
const minFormatted = typeof min === "string" ? min : minParsed.format(format);
|
|
253
|
+
ctx.addIssue({ code: "custom", message: getMessage("min", { min: minFormatted }) });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (max) {
|
|
258
|
+
const maxParsed = typeof max === "string" ? parseDateTimeValue(max, format, timezone2) : _dayjs2.default.call(void 0, max);
|
|
259
|
+
if (maxParsed && parsed.isAfter(maxParsed)) {
|
|
260
|
+
const maxFormatted = typeof max === "string" ? max : maxParsed.format(format);
|
|
261
|
+
ctx.addIssue({ code: "custom", message: getMessage("max", { max: maxFormatted }) });
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const now = timezone2 ? _dayjs2.default.call(void 0, ).tz(timezone2) : _dayjs2.default.call(void 0, );
|
|
266
|
+
if (mustBePast && !parsed.isBefore(now)) {
|
|
267
|
+
ctx.addIssue({ code: "custom", message: getMessage("past") });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (mustBeFuture && !parsed.isAfter(now)) {
|
|
271
|
+
ctx.addIssue({ code: "custom", message: getMessage("future") });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (mustBeToday && !parsed.isSame(now, "day")) {
|
|
275
|
+
ctx.addIssue({ code: "custom", message: getMessage("today") });
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (mustNotBeToday && parsed.isSame(now, "day")) {
|
|
279
|
+
ctx.addIssue({ code: "custom", message: getMessage("notToday") });
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const dayOfWeek = parsed.day();
|
|
283
|
+
if (weekdaysOnly && (dayOfWeek === 0 || dayOfWeek === 6)) {
|
|
284
|
+
ctx.addIssue({ code: "custom", message: getMessage("weekday") });
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (weekendsOnly && dayOfWeek !== 0 && dayOfWeek !== 6) {
|
|
288
|
+
ctx.addIssue({ code: "custom", message: getMessage("weekend") });
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
return schema;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
exports.DATETIME_PATTERNS = DATETIME_PATTERNS; exports.validateDateTimeFormat = validateDateTimeFormat; exports.parseDateTimeValue = parseDateTimeValue; exports.normalizeDateTimeValue = normalizeDateTimeValue; exports.datetime = datetime;
|