@hy_ong/zod-kit 0.2.0 → 0.2.2
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 +64 -22
- package/dist/chunk-2SWEVDFZ.js +134 -0
- package/dist/chunk-32JI34CV.cjs +146 -0
- package/dist/chunk-42C5OHRK.js +71 -0
- package/dist/chunk-46VAH2BJ.js +160 -0
- package/dist/chunk-5JGTDL3Y.js +87 -0
- package/dist/chunk-5LEXCVLX.js +257 -0
- package/dist/chunk-6AAP4LPF.js +2606 -0
- package/dist/chunk-B4EZYZOK.cjs +215 -0
- package/dist/chunk-COYKBWTI.js +161 -0
- package/dist/chunk-DFJZ3NS2.cjs +151 -0
- package/dist/chunk-EDHT4LPO.js +118 -0
- package/dist/chunk-EGHL277K.cjs +165 -0
- package/dist/chunk-ERH4NIMU.cjs +69 -0
- package/dist/chunk-FM3EZ72O.js +165 -0
- package/dist/chunk-GJIRDBZJ.cjs +90 -0
- package/dist/chunk-H2XTEM4M.js +696 -0
- package/dist/chunk-HMSM6FFA.cjs +181 -0
- package/dist/chunk-HTEHINI7.cjs +177 -0
- package/dist/chunk-JOLSGZGN.cjs +696 -0
- package/dist/chunk-JXY7APBU.js +69 -0
- package/dist/chunk-K2UOY6TB.js +136 -0
- package/dist/chunk-KFOHKTFD.js +61 -0
- package/dist/chunk-L4HSIKTU.cjs +135 -0
- package/dist/chunk-LH7ZB4BK.js +124 -0
- package/dist/chunk-LL4ZWLGO.js +90 -0
- package/dist/chunk-M6MTP3NY.cjs +99 -0
- package/dist/chunk-MHJFYYGV.js +215 -0
- package/dist/chunk-MINMXGW3.js +135 -0
- package/dist/chunk-MM7IL2RG.js +181 -0
- package/dist/chunk-OPQJWHXN.cjs +301 -0
- package/dist/chunk-ORFHDJII.cjs +136 -0
- package/dist/chunk-ORVV4MCF.cjs +87 -0
- package/dist/chunk-QICQ6YEY.js +75 -0
- package/dist/chunk-RKUQREMW.js +127 -0
- package/dist/chunk-RO47DKQG.js +146 -0
- package/dist/chunk-RRPXIRTQ.cjs +257 -0
- package/dist/chunk-RYFG2GKM.cjs +118 -0
- package/dist/chunk-STNHTRG7.cjs +124 -0
- package/dist/chunk-TFGS34VD.cjs +71 -0
- package/dist/chunk-TQXDUMML.cjs +61 -0
- package/dist/chunk-UBK3VCVH.cjs +134 -0
- package/dist/chunk-UCOXAZJF.cjs +2606 -0
- package/dist/chunk-UQZKFAFX.js +130 -0
- package/dist/chunk-VB2KV2ZM.cjs +130 -0
- package/dist/chunk-WABKPFPK.js +151 -0
- package/dist/chunk-WDI4QJMQ.js +177 -0
- package/dist/chunk-YDH3L27K.cjs +127 -0
- package/dist/chunk-YIM3D2AD.js +99 -0
- package/dist/chunk-YPSEIDUR.cjs +160 -0
- package/dist/chunk-ZNJLWJX3.cjs +75 -0
- package/dist/chunk-ZTFCJCPO.cjs +161 -0
- package/dist/chunk-ZXUMK2RR.js +301 -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 +288 -0
- package/dist/common/id.d.ts +288 -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 -5255
- package/dist/index.d.cts +28 -3150
- package/dist/index.d.ts +28 -3150
- package/dist/index.js +135 -5131
- 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 +132 -6
- package/src/i18n/locales/en-GB.json +51 -0
- package/src/i18n/locales/en-US.json +52 -1
- package/src/i18n/locales/id-ID.json +51 -0
- package/src/i18n/locales/ja-JP.json +51 -0
- package/src/i18n/locales/ko-KR.json +51 -0
- package/src/i18n/locales/ms-MY.json +51 -0
- package/src/i18n/locales/th-TH.json +51 -0
- package/src/i18n/locales/vi-VN.json +51 -0
- package/src/i18n/locales/zh-CN.json +51 -0
- package/src/i18n/locales/zh-TW.json +51 -0
- package/src/index.ts +10 -2
- 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/id.ts +45 -3
- package/src/validators/common/ip.ts +210 -0
- package/src/validators/taiwan/bank-account.ts +176 -0
- package/src/validators/taiwan/invoice.ts +84 -0
- package/src/validators/taiwan/license-plate.ts +110 -0
- package/src/validators/taiwan/passport.ts +103 -0
- 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/id.test.ts +68 -3
- package/tests/common/ip.test.ts +419 -0
- package/tests/taiwan/bank-account.test.ts +286 -0
- package/tests/taiwan/invoice.test.ts +227 -0
- package/tests/taiwan/license-plate.test.ts +280 -0
- package/tests/taiwan/passport.test.ts +277 -0
- package/tsup.config.ts +36 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLocale,
|
|
3
|
+
t
|
|
4
|
+
} from "./chunk-6AAP4LPF.js";
|
|
5
|
+
|
|
6
|
+
// src/validators/taiwan/tel.ts
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
var validateTaiwanTel = (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 === "0800" || areaCode4 === "0809") {
|
|
15
|
+
return cleanValue.length === 10 && /^080[09]\d{6}$/.test(cleanValue);
|
|
16
|
+
}
|
|
17
|
+
if (areaCode4 === "0836") {
|
|
18
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^0836\d{5,6}$/.test(cleanValue);
|
|
19
|
+
}
|
|
20
|
+
const areaCode3 = cleanValue.substring(0, 3);
|
|
21
|
+
if (areaCode3 === "037") {
|
|
22
|
+
const firstDigit = cleanValue[3];
|
|
23
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^037[2-9]\d{5,6}$/.test(cleanValue);
|
|
27
|
+
}
|
|
28
|
+
if (areaCode3 === "049") {
|
|
29
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^049\d{6,7}$/.test(cleanValue);
|
|
30
|
+
}
|
|
31
|
+
if (areaCode3 === "082") {
|
|
32
|
+
return cleanValue.length === 9 && /^082\d{6}$/.test(cleanValue);
|
|
33
|
+
}
|
|
34
|
+
if (areaCode3 === "089") {
|
|
35
|
+
return cleanValue.length === 9 && /^089\d{6}$/.test(cleanValue);
|
|
36
|
+
}
|
|
37
|
+
const areaCode2 = cleanValue.substring(0, 2);
|
|
38
|
+
if (areaCode2 === "02") {
|
|
39
|
+
const firstDigit = cleanValue[2];
|
|
40
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return cleanValue.length === 10 && /^02[2-9]\d{7}$/.test(cleanValue);
|
|
44
|
+
}
|
|
45
|
+
if (areaCode2 === "03") {
|
|
46
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^03\d{7,8}$/.test(cleanValue);
|
|
47
|
+
}
|
|
48
|
+
if (areaCode2 === "04") {
|
|
49
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^04\d{7,8}$/.test(cleanValue);
|
|
50
|
+
}
|
|
51
|
+
if (areaCode2 === "05") {
|
|
52
|
+
return cleanValue.length === 9 && /^05\d{7}$/.test(cleanValue);
|
|
53
|
+
}
|
|
54
|
+
if (areaCode2 === "06") {
|
|
55
|
+
return cleanValue.length === 9 && /^06\d{7}$/.test(cleanValue);
|
|
56
|
+
}
|
|
57
|
+
if (areaCode2 === "07") {
|
|
58
|
+
const firstDigit = cleanValue[2];
|
|
59
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^07[2-9]\d{6,7}$/.test(cleanValue);
|
|
63
|
+
}
|
|
64
|
+
if (areaCode2 === "08") {
|
|
65
|
+
const firstDigit = cleanValue[2];
|
|
66
|
+
if (firstDigit !== "4" && firstDigit !== "7" && firstDigit !== "8") {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return cleanValue.length === 9 && /^08[478]\d{6}$/.test(cleanValue);
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
function twTel(required, options) {
|
|
74
|
+
const { whitelist, transform, defaultValue, i18n } = options ?? {};
|
|
75
|
+
const isRequired = required ?? false;
|
|
76
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
77
|
+
const getMessage = (key, params) => {
|
|
78
|
+
if (i18n) {
|
|
79
|
+
const currentLocale = getLocale();
|
|
80
|
+
const customMessages = i18n[currentLocale];
|
|
81
|
+
if (customMessages && customMessages[key]) {
|
|
82
|
+
const template = customMessages[key];
|
|
83
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return t(`taiwan.tel.${key}`, params);
|
|
87
|
+
};
|
|
88
|
+
const preprocessFn = (val) => {
|
|
89
|
+
if (val === null || val === void 0) {
|
|
90
|
+
return actualDefaultValue;
|
|
91
|
+
}
|
|
92
|
+
let processed = String(val).trim();
|
|
93
|
+
if (processed === "") {
|
|
94
|
+
if (whitelist && whitelist.includes("")) {
|
|
95
|
+
return "";
|
|
96
|
+
}
|
|
97
|
+
if (!isRequired) {
|
|
98
|
+
return actualDefaultValue;
|
|
99
|
+
}
|
|
100
|
+
return actualDefaultValue;
|
|
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 (val === null) return;
|
|
115
|
+
if (!isRequired && val === "") return;
|
|
116
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!validateTaiwanTel(val)) {
|
|
120
|
+
ctx.addIssue({ code: "custom", message: getMessage("invalid") });
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return schema;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export {
|
|
128
|
+
validateTaiwanTel,
|
|
129
|
+
twTel
|
|
130
|
+
};
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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 _chunkUCOXAZJFcjs = require('./chunk-UCOXAZJF.cjs');
|
|
5
|
+
|
|
6
|
+
// src/validators/taiwan/tel.ts
|
|
7
|
+
var _zod = require('zod');
|
|
8
|
+
var validateTaiwanTel = (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 === "0800" || areaCode4 === "0809") {
|
|
15
|
+
return cleanValue.length === 10 && /^080[09]\d{6}$/.test(cleanValue);
|
|
16
|
+
}
|
|
17
|
+
if (areaCode4 === "0836") {
|
|
18
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^0836\d{5,6}$/.test(cleanValue);
|
|
19
|
+
}
|
|
20
|
+
const areaCode3 = cleanValue.substring(0, 3);
|
|
21
|
+
if (areaCode3 === "037") {
|
|
22
|
+
const firstDigit = cleanValue[3];
|
|
23
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^037[2-9]\d{5,6}$/.test(cleanValue);
|
|
27
|
+
}
|
|
28
|
+
if (areaCode3 === "049") {
|
|
29
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^049\d{6,7}$/.test(cleanValue);
|
|
30
|
+
}
|
|
31
|
+
if (areaCode3 === "082") {
|
|
32
|
+
return cleanValue.length === 9 && /^082\d{6}$/.test(cleanValue);
|
|
33
|
+
}
|
|
34
|
+
if (areaCode3 === "089") {
|
|
35
|
+
return cleanValue.length === 9 && /^089\d{6}$/.test(cleanValue);
|
|
36
|
+
}
|
|
37
|
+
const areaCode2 = cleanValue.substring(0, 2);
|
|
38
|
+
if (areaCode2 === "02") {
|
|
39
|
+
const firstDigit = cleanValue[2];
|
|
40
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return cleanValue.length === 10 && /^02[2-9]\d{7}$/.test(cleanValue);
|
|
44
|
+
}
|
|
45
|
+
if (areaCode2 === "03") {
|
|
46
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^03\d{7,8}$/.test(cleanValue);
|
|
47
|
+
}
|
|
48
|
+
if (areaCode2 === "04") {
|
|
49
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^04\d{7,8}$/.test(cleanValue);
|
|
50
|
+
}
|
|
51
|
+
if (areaCode2 === "05") {
|
|
52
|
+
return cleanValue.length === 9 && /^05\d{7}$/.test(cleanValue);
|
|
53
|
+
}
|
|
54
|
+
if (areaCode2 === "06") {
|
|
55
|
+
return cleanValue.length === 9 && /^06\d{7}$/.test(cleanValue);
|
|
56
|
+
}
|
|
57
|
+
if (areaCode2 === "07") {
|
|
58
|
+
const firstDigit = cleanValue[2];
|
|
59
|
+
if (firstDigit === "0" || firstDigit === "1") {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return (cleanValue.length === 9 || cleanValue.length === 10) && /^07[2-9]\d{6,7}$/.test(cleanValue);
|
|
63
|
+
}
|
|
64
|
+
if (areaCode2 === "08") {
|
|
65
|
+
const firstDigit = cleanValue[2];
|
|
66
|
+
if (firstDigit !== "4" && firstDigit !== "7" && firstDigit !== "8") {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
return cleanValue.length === 9 && /^08[478]\d{6}$/.test(cleanValue);
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
function twTel(required, options) {
|
|
74
|
+
const { whitelist, transform, defaultValue, i18n } = _nullishCoalesce(options, () => ( {}));
|
|
75
|
+
const isRequired = _nullishCoalesce(required, () => ( false));
|
|
76
|
+
const actualDefaultValue = _nullishCoalesce(defaultValue, () => ( (isRequired ? "" : null)));
|
|
77
|
+
const getMessage = (key, params) => {
|
|
78
|
+
if (i18n) {
|
|
79
|
+
const currentLocale = _chunkUCOXAZJFcjs.getLocale.call(void 0, );
|
|
80
|
+
const customMessages = i18n[currentLocale];
|
|
81
|
+
if (customMessages && customMessages[key]) {
|
|
82
|
+
const template = customMessages[key];
|
|
83
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => _nullishCoalesce(_optionalChain([params, 'optionalAccess', _2 => _2[k]]), () => ( "")));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return _chunkUCOXAZJFcjs.t.call(void 0, `taiwan.tel.${key}`, params);
|
|
87
|
+
};
|
|
88
|
+
const preprocessFn = (val) => {
|
|
89
|
+
if (val === null || val === void 0) {
|
|
90
|
+
return actualDefaultValue;
|
|
91
|
+
}
|
|
92
|
+
let processed = String(val).trim();
|
|
93
|
+
if (processed === "") {
|
|
94
|
+
if (whitelist && whitelist.includes("")) {
|
|
95
|
+
return "";
|
|
96
|
+
}
|
|
97
|
+
if (!isRequired) {
|
|
98
|
+
return actualDefaultValue;
|
|
99
|
+
}
|
|
100
|
+
return actualDefaultValue;
|
|
101
|
+
}
|
|
102
|
+
if (transform) {
|
|
103
|
+
processed = transform(processed);
|
|
104
|
+
}
|
|
105
|
+
return processed;
|
|
106
|
+
};
|
|
107
|
+
const baseSchema = isRequired ? _zod.z.preprocess(preprocessFn, _zod.z.string()) : _zod.z.preprocess(preprocessFn, _zod.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 (val === null) return;
|
|
115
|
+
if (!isRequired && val === "") return;
|
|
116
|
+
if (whitelist && whitelist.length > 0 && whitelist.includes(val)) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!validateTaiwanTel(val)) {
|
|
120
|
+
ctx.addIssue({ code: "custom", message: getMessage("invalid") });
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return schema;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
exports.validateTaiwanTel = validateTaiwanTel; exports.twTel = twTel;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLocale,
|
|
3
|
+
t
|
|
4
|
+
} from "./chunk-6AAP4LPF.js";
|
|
5
|
+
|
|
6
|
+
// src/validators/common/url.ts
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
function url(required, options) {
|
|
9
|
+
const {
|
|
10
|
+
min,
|
|
11
|
+
max,
|
|
12
|
+
includes,
|
|
13
|
+
excludes,
|
|
14
|
+
protocols,
|
|
15
|
+
allowedDomains,
|
|
16
|
+
blockedDomains,
|
|
17
|
+
allowedPorts,
|
|
18
|
+
blockedPorts,
|
|
19
|
+
pathStartsWith,
|
|
20
|
+
pathEndsWith,
|
|
21
|
+
mustHaveQuery,
|
|
22
|
+
mustNotHaveQuery,
|
|
23
|
+
mustHaveFragment,
|
|
24
|
+
mustNotHaveFragment,
|
|
25
|
+
allowLocalhost = true,
|
|
26
|
+
blockLocalhost,
|
|
27
|
+
transform,
|
|
28
|
+
defaultValue = null,
|
|
29
|
+
i18n
|
|
30
|
+
} = options ?? {};
|
|
31
|
+
const isRequired = required ?? false;
|
|
32
|
+
const actualDefaultValue = defaultValue ?? (isRequired ? "" : null);
|
|
33
|
+
const getMessage = (key, params) => {
|
|
34
|
+
if (i18n) {
|
|
35
|
+
const currentLocale = getLocale();
|
|
36
|
+
const customMessages = i18n[currentLocale];
|
|
37
|
+
if (customMessages && customMessages[key]) {
|
|
38
|
+
const template = customMessages[key];
|
|
39
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return t(`common.url.${key}`, params);
|
|
43
|
+
};
|
|
44
|
+
const preprocessFn = (val) => {
|
|
45
|
+
if (val === "" || val === null || val === void 0) {
|
|
46
|
+
return actualDefaultValue;
|
|
47
|
+
}
|
|
48
|
+
let processed = String(val).trim();
|
|
49
|
+
if (transform) {
|
|
50
|
+
processed = transform(processed);
|
|
51
|
+
}
|
|
52
|
+
return processed;
|
|
53
|
+
};
|
|
54
|
+
const baseSchema = isRequired ? z.preprocess(preprocessFn, z.string()) : z.preprocess(preprocessFn, z.string().nullable());
|
|
55
|
+
const schema = baseSchema.superRefine((val, ctx) => {
|
|
56
|
+
if (val === null) return;
|
|
57
|
+
if (isRequired && (val === "" || val === "null" || val === "undefined")) {
|
|
58
|
+
ctx.addIssue({ code: "custom", message: getMessage("required") });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
let urlObj;
|
|
62
|
+
try {
|
|
63
|
+
urlObj = new URL(val);
|
|
64
|
+
} catch {
|
|
65
|
+
ctx.addIssue({ code: "custom", message: getMessage("invalid") });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (val !== null && min !== void 0 && val.length < min) {
|
|
69
|
+
ctx.addIssue({ code: "custom", message: getMessage("min", { min }) });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (val !== null && max !== void 0 && val.length > max) {
|
|
73
|
+
ctx.addIssue({ code: "custom", message: getMessage("max", { max }) });
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (val !== null && includes !== void 0 && !val.includes(includes)) {
|
|
77
|
+
ctx.addIssue({ code: "custom", message: getMessage("includes", { includes }) });
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (val !== null && excludes !== void 0) {
|
|
81
|
+
const excludeList = Array.isArray(excludes) ? excludes : [excludes];
|
|
82
|
+
for (const exclude of excludeList) {
|
|
83
|
+
if (val.includes(exclude)) {
|
|
84
|
+
ctx.addIssue({ code: "custom", message: getMessage("excludes", { excludes: exclude }) });
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (protocols && !protocols.includes(urlObj.protocol.slice(0, -1))) {
|
|
90
|
+
ctx.addIssue({ code: "custom", message: getMessage("protocol", { protocols: protocols.join(", ") }) });
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const hostname = urlObj.hostname.toLowerCase();
|
|
94
|
+
if (allowedDomains && !allowedDomains.some((domain) => hostname === domain || hostname.endsWith(`.${domain}`))) {
|
|
95
|
+
ctx.addIssue({ code: "custom", message: getMessage("domain", { domains: allowedDomains.join(", ") }) });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (blockedDomains && blockedDomains.some((domain) => hostname === domain || hostname.endsWith(`.${domain}`))) {
|
|
99
|
+
const blockedDomain = blockedDomains.find((domain) => hostname === domain || hostname.endsWith(`.${domain}`));
|
|
100
|
+
ctx.addIssue({ code: "custom", message: getMessage("domainBlacklist", { domain: blockedDomain }) });
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const port = urlObj.port ? parseInt(urlObj.port) : urlObj.protocol === "https:" ? 443 : 80;
|
|
104
|
+
if (allowedPorts && !allowedPorts.includes(port)) {
|
|
105
|
+
ctx.addIssue({ code: "custom", message: getMessage("port", { ports: allowedPorts.join(", ") }) });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (blockedPorts && blockedPorts.includes(port)) {
|
|
109
|
+
ctx.addIssue({ code: "custom", message: getMessage("port", { port }) });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (pathStartsWith && !urlObj.pathname.startsWith(pathStartsWith)) {
|
|
113
|
+
ctx.addIssue({ code: "custom", message: getMessage("pathStartsWith", { path: pathStartsWith }) });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (pathEndsWith && !urlObj.pathname.endsWith(pathEndsWith)) {
|
|
117
|
+
ctx.addIssue({ code: "custom", message: getMessage("pathEndsWith", { path: pathEndsWith }) });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (mustHaveQuery && !urlObj.search) {
|
|
121
|
+
ctx.addIssue({ code: "custom", message: getMessage("hasQuery") });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (mustNotHaveQuery && urlObj.search) {
|
|
125
|
+
ctx.addIssue({ code: "custom", message: getMessage("noQuery") });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (mustHaveFragment && !urlObj.hash) {
|
|
129
|
+
ctx.addIssue({ code: "custom", message: getMessage("hasFragment") });
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (mustNotHaveFragment && urlObj.hash) {
|
|
133
|
+
ctx.addIssue({ code: "custom", message: getMessage("noFragment") });
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const isLocalhost = hostname === "localhost" || hostname === "127.0.0.1" || hostname.startsWith("192.168.") || hostname.startsWith("10.") || hostname.match(/^172\.(1[6-9]|2[0-9]|3[0-1])\./);
|
|
137
|
+
if (blockLocalhost && isLocalhost) {
|
|
138
|
+
ctx.addIssue({ code: "custom", message: getMessage("noLocalhost") });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (!allowLocalhost && isLocalhost) {
|
|
142
|
+
ctx.addIssue({ code: "custom", message: getMessage("localhost") });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return schema;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export {
|
|
150
|
+
url
|
|
151
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLocale,
|
|
3
|
+
t
|
|
4
|
+
} from "./chunk-6AAP4LPF.js";
|
|
5
|
+
|
|
6
|
+
// src/validators/common/file.ts
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
function file(required, options) {
|
|
9
|
+
const {
|
|
10
|
+
maxSize,
|
|
11
|
+
minSize,
|
|
12
|
+
type,
|
|
13
|
+
typeBlacklist,
|
|
14
|
+
extension,
|
|
15
|
+
extensionBlacklist,
|
|
16
|
+
namePattern,
|
|
17
|
+
nameBlacklist,
|
|
18
|
+
imageOnly = false,
|
|
19
|
+
documentOnly = false,
|
|
20
|
+
videoOnly = false,
|
|
21
|
+
audioOnly = false,
|
|
22
|
+
archiveOnly = false,
|
|
23
|
+
caseSensitive = false,
|
|
24
|
+
transform,
|
|
25
|
+
defaultValue,
|
|
26
|
+
i18n
|
|
27
|
+
} = options ?? {};
|
|
28
|
+
const isRequired = required ?? false;
|
|
29
|
+
const getMessage = (key, params) => {
|
|
30
|
+
if (i18n) {
|
|
31
|
+
const currentLocale = getLocale();
|
|
32
|
+
const customMessages = i18n[currentLocale];
|
|
33
|
+
if (customMessages && customMessages[key]) {
|
|
34
|
+
const template = customMessages[key];
|
|
35
|
+
return template.replace(/\$\{(\w+)}/g, (_, k) => params?.[k] ?? "");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return t(`common.file.${key}`, params);
|
|
39
|
+
};
|
|
40
|
+
const imageTypes = ["image/jpeg", "image/jpg", "image/png", "image/gif", "image/webp", "image/svg+xml", "image/bmp", "image/tiff"];
|
|
41
|
+
const documentTypes = [
|
|
42
|
+
"application/pdf",
|
|
43
|
+
"application/msword",
|
|
44
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
45
|
+
"application/vnd.ms-excel",
|
|
46
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
47
|
+
"application/vnd.ms-powerpoint",
|
|
48
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
49
|
+
"text/plain",
|
|
50
|
+
"text/csv"
|
|
51
|
+
];
|
|
52
|
+
const videoTypes = ["video/mp4", "video/mpeg", "video/quicktime", "video/x-msvideo", "video/x-ms-wmv", "video/webm", "video/ogg"];
|
|
53
|
+
const audioTypes = ["audio/mpeg", "audio/wav", "audio/ogg", "audio/aac", "audio/webm", "audio/mp3", "audio/x-wav"];
|
|
54
|
+
const archiveTypes = ["application/zip", "application/x-rar-compressed", "application/x-7z-compressed", "application/x-tar", "application/gzip"];
|
|
55
|
+
const actualDefaultValue = defaultValue ?? null;
|
|
56
|
+
const fileOrNullSchema = z.union([z.instanceof(File), z.null()]);
|
|
57
|
+
const baseSchema = z.preprocess((val) => {
|
|
58
|
+
if (val === "" || val === null || val === void 0) {
|
|
59
|
+
return actualDefaultValue;
|
|
60
|
+
}
|
|
61
|
+
if (!(val instanceof File)) {
|
|
62
|
+
return val;
|
|
63
|
+
}
|
|
64
|
+
let processed = val;
|
|
65
|
+
if (transform) {
|
|
66
|
+
processed = transform(processed);
|
|
67
|
+
}
|
|
68
|
+
return processed;
|
|
69
|
+
}, fileOrNullSchema);
|
|
70
|
+
const schema = baseSchema.refine((val) => !isRequired || val !== null, {
|
|
71
|
+
message: getMessage("required")
|
|
72
|
+
}).refine((val) => val === null || val instanceof File, {
|
|
73
|
+
message: getMessage("invalid")
|
|
74
|
+
}).refine((val) => val === null || minSize === void 0 || val.size >= minSize, {
|
|
75
|
+
message: getMessage("minSize", { minSize: formatFileSize(minSize || 0) })
|
|
76
|
+
}).refine((val) => val === null || maxSize === void 0 || val.size <= maxSize, {
|
|
77
|
+
message: getMessage("maxSize", { maxSize: formatFileSize(maxSize || 0) })
|
|
78
|
+
}).refine((val) => val === null || !imageOnly || imageTypes.includes(val.type), {
|
|
79
|
+
message: getMessage("imageOnly")
|
|
80
|
+
}).refine((val) => val === null || !documentOnly || documentTypes.includes(val.type), {
|
|
81
|
+
message: getMessage("documentOnly")
|
|
82
|
+
}).refine((val) => val === null || !videoOnly || videoTypes.includes(val.type), {
|
|
83
|
+
message: getMessage("videoOnly")
|
|
84
|
+
}).refine((val) => val === null || !audioOnly || audioTypes.includes(val.type), {
|
|
85
|
+
message: getMessage("audioOnly")
|
|
86
|
+
}).refine((val) => val === null || !archiveOnly || archiveTypes.includes(val.type), {
|
|
87
|
+
message: getMessage("archiveOnly")
|
|
88
|
+
}).refine(
|
|
89
|
+
(val) => {
|
|
90
|
+
if (val === null || !typeBlacklist || typeBlacklist.length === 0) return true;
|
|
91
|
+
return !typeBlacklist.includes(val.type);
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
message: getMessage("type", { type: typeBlacklist?.join(", ") || "" })
|
|
95
|
+
}
|
|
96
|
+
).refine(
|
|
97
|
+
(val) => {
|
|
98
|
+
if (val === null || type === void 0) return true;
|
|
99
|
+
const allowedTypes = Array.isArray(type) ? type : [type];
|
|
100
|
+
return allowedTypes.includes(val.type);
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
message: getMessage("type", { type: Array.isArray(type) ? type.join(", ") : type || "" })
|
|
104
|
+
}
|
|
105
|
+
).refine(
|
|
106
|
+
(val) => {
|
|
107
|
+
if (val === null || extensionBlacklist === void 0 || extensionBlacklist.length === 0) return true;
|
|
108
|
+
const fileExtension = getFileExtension(val.name, caseSensitive);
|
|
109
|
+
const normalizedBlacklist = extensionBlacklist.map((ext) => normalizeExtension(ext, caseSensitive));
|
|
110
|
+
return !normalizedBlacklist.includes(fileExtension);
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
message: getMessage("extensionBlacklist", { extension: extensionBlacklist?.join(", ") || "" })
|
|
114
|
+
}
|
|
115
|
+
).refine(
|
|
116
|
+
(val) => {
|
|
117
|
+
if (val === null || extension === void 0) return true;
|
|
118
|
+
const fileName = val.name;
|
|
119
|
+
const fileExtension = getFileExtension(fileName, caseSensitive);
|
|
120
|
+
const allowedExtensions = Array.isArray(extension) ? extension : [extension];
|
|
121
|
+
const normalizedExtensions = allowedExtensions.map((ext) => normalizeExtension(ext, caseSensitive));
|
|
122
|
+
return normalizedExtensions.includes(fileExtension);
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
message: getMessage("extension", { extension: Array.isArray(extension) ? extension.join(", ") : extension || "" })
|
|
126
|
+
}
|
|
127
|
+
).refine(
|
|
128
|
+
(val) => {
|
|
129
|
+
if (val === null || namePattern === void 0) return true;
|
|
130
|
+
const pattern = typeof namePattern === "string" ? new RegExp(namePattern) : namePattern;
|
|
131
|
+
return pattern.test(val.name);
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
message: getMessage("name", { pattern: namePattern?.toString() || "" })
|
|
135
|
+
}
|
|
136
|
+
).refine(
|
|
137
|
+
(val) => {
|
|
138
|
+
if (val === null || nameBlacklist === void 0) return true;
|
|
139
|
+
const blacklistPatterns = Array.isArray(nameBlacklist) ? nameBlacklist : [nameBlacklist];
|
|
140
|
+
for (const blacklistPattern of blacklistPatterns) {
|
|
141
|
+
const pattern = typeof blacklistPattern === "string" ? new RegExp(blacklistPattern) : blacklistPattern;
|
|
142
|
+
if (pattern.test(val.name)) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
message: getMessage("nameBlacklist", { pattern: "" })
|
|
150
|
+
}
|
|
151
|
+
);
|
|
152
|
+
return schema;
|
|
153
|
+
}
|
|
154
|
+
function getFileExtension(fileName, caseSensitive) {
|
|
155
|
+
const lastDotIndex = fileName.lastIndexOf(".");
|
|
156
|
+
if (lastDotIndex === -1) return "";
|
|
157
|
+
const extension = fileName.substring(lastDotIndex);
|
|
158
|
+
return caseSensitive ? extension : extension.toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
function normalizeExtension(extension, caseSensitive) {
|
|
161
|
+
const normalized = extension.startsWith(".") ? extension : `.${extension}`;
|
|
162
|
+
return caseSensitive ? normalized : normalized.toLowerCase();
|
|
163
|
+
}
|
|
164
|
+
function formatFileSize(bytes) {
|
|
165
|
+
const units = ["B", "KB", "MB", "GB", "TB"];
|
|
166
|
+
let size = bytes;
|
|
167
|
+
let unitIndex = 0;
|
|
168
|
+
while (size >= 1024 && unitIndex < units.length - 1) {
|
|
169
|
+
size /= 1024;
|
|
170
|
+
unitIndex++;
|
|
171
|
+
}
|
|
172
|
+
return `${Math.round(size * 100) / 100} ${units[unitIndex]}`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export {
|
|
176
|
+
file
|
|
177
|
+
};
|