@hy_ong/zod-kit 0.1.16 → 0.2.0
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.md +142 -0
- package/dist/index.cjs +1660 -4
- package/dist/index.d.cts +18 -18
- package/dist/index.d.ts +18 -18
- package/dist/index.js +1660 -4
- package/package.json +1 -1
- package/src/config.ts +2 -2
- package/src/i18n/index.ts +18 -2
- package/src/i18n/locales/en-GB.json +204 -0
- package/src/i18n/locales/id-ID.json +204 -0
- package/src/i18n/locales/ja-JP.json +204 -0
- package/src/i18n/locales/ko-KR.json +204 -0
- package/src/i18n/locales/ms-MY.json +204 -0
- package/src/i18n/locales/th-TH.json +204 -0
- package/src/i18n/locales/vi-VN.json +204 -0
- package/src/i18n/locales/zh-CN.json +204 -0
- package/src/validators/common/boolean.ts +1 -1
- 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/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/business-id.ts +1 -1
- package/src/validators/taiwan/fax.ts +1 -1
- package/src/validators/taiwan/mobile.ts +1 -1
- package/src/validators/taiwan/national-id.ts +1 -1
- 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/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/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/business-id.test.ts +4 -4
- package/tests/taiwan/fax.test.ts +7 -7
- package/tests/taiwan/mobile.test.ts +7 -7
- package/tests/taiwan/national-id.test.ts +4 -4
- package/tests/taiwan/postal-code.test.ts +6 -6
- package/tests/taiwan/tel.test.ts +7 -7
- /package/src/i18n/locales/{en.json → en-US.json} +0 -0
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# CLAUDE.md — zod-kit
|
|
2
|
+
|
|
3
|
+
## Project Overview
|
|
4
|
+
|
|
5
|
+
`@hy_ong/zod-kit` is a TypeScript library providing pre-built Zod validation schemas with full i18n support (English, Traditional Chinese). It includes common validators (email, password, text, number, etc.) and Taiwan-specific validators (National ID, Business ID, mobile, tel, fax, postal code).
|
|
6
|
+
|
|
7
|
+
- **Version:** 0.1.16
|
|
8
|
+
- **License:** MIT
|
|
9
|
+
- **NPM:** `@hy_ong/zod-kit`
|
|
10
|
+
- **Peer dependency:** Zod ^4.3.6
|
|
11
|
+
- **Production dependency:** dayjs ^1.11.19
|
|
12
|
+
|
|
13
|
+
## Commands
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# Run all tests (743 test cases across 17 files)
|
|
17
|
+
npm test
|
|
18
|
+
|
|
19
|
+
# Run tests once (no watch mode)
|
|
20
|
+
npm test -- --run
|
|
21
|
+
|
|
22
|
+
# Run specific test file
|
|
23
|
+
npx vitest run tests/common/email.test.ts
|
|
24
|
+
|
|
25
|
+
# Build (ESM + CJS + .d.ts)
|
|
26
|
+
npm run build
|
|
27
|
+
# Equivalent to: tsup src/index.ts --format esm,cjs --dts
|
|
28
|
+
|
|
29
|
+
# Lint
|
|
30
|
+
npx eslint src/
|
|
31
|
+
|
|
32
|
+
# Publish (runs tests + build automatically via prepublishOnly)
|
|
33
|
+
npm publish --access public
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Project Structure
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
src/
|
|
40
|
+
├── index.ts # Re-exports all validators + config
|
|
41
|
+
├── config.ts # Locale state (setLocale, getLocale)
|
|
42
|
+
├── i18n/
|
|
43
|
+
│ ├── index.ts # Translation function t(key, params)
|
|
44
|
+
│ └── locales/
|
|
45
|
+
│ ├── en.json # English messages
|
|
46
|
+
│ └── zh-TW.json # Traditional Chinese messages
|
|
47
|
+
└── validators/
|
|
48
|
+
├── common/ # 11 universal validators
|
|
49
|
+
│ ├── boolean.ts
|
|
50
|
+
│ ├── date.ts
|
|
51
|
+
│ ├── datetime.ts # Uses dayjs for parsing
|
|
52
|
+
│ ├── email.ts
|
|
53
|
+
│ ├── file.ts
|
|
54
|
+
│ ├── id.ts # UUID, nanoid, ObjectId
|
|
55
|
+
│ ├── number.ts
|
|
56
|
+
│ ├── password.ts
|
|
57
|
+
│ ├── text.ts
|
|
58
|
+
│ ├── time.ts
|
|
59
|
+
│ └── url.ts
|
|
60
|
+
└── taiwan/ # 6 Taiwan-specific validators
|
|
61
|
+
├── business-id.ts # 統一編號 (checksum)
|
|
62
|
+
├── fax.ts
|
|
63
|
+
├── mobile.ts # 09X-XXXX-XXXX
|
|
64
|
+
├── national-id.ts # 身分證/居留證 (checksum)
|
|
65
|
+
├── postal-code.ts # 3/5/6-digit with official data
|
|
66
|
+
└── tel.ts # Landline + toll-free (0800/0809)
|
|
67
|
+
|
|
68
|
+
tests/ # Mirrors src/validators/ structure
|
|
69
|
+
├── common/ # 11 test files
|
|
70
|
+
└── taiwan/ # 6 test files
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Architecture & Patterns
|
|
74
|
+
|
|
75
|
+
### Validator Function Signature
|
|
76
|
+
|
|
77
|
+
Every validator follows this consistent pattern:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
function validatorName<IsRequired extends boolean = false>(
|
|
81
|
+
required?: IsRequired,
|
|
82
|
+
options?: Omit<ValidatorOptions<IsRequired>, "required">
|
|
83
|
+
): ValidatorSchema<IsRequired>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- First param: `required` boolean (defaults to `false`)
|
|
87
|
+
- Second param: options object (varies per validator)
|
|
88
|
+
- Returns: `ZodType` — required returns non-nullable, optional returns nullable
|
|
89
|
+
|
|
90
|
+
### Validation Pipeline
|
|
91
|
+
|
|
92
|
+
All validators use a two-step approach:
|
|
93
|
+
1. `z.preprocess()` — input transformation (trim, coerce, normalize)
|
|
94
|
+
2. `.superRefine()` — complex business logic and custom error messages
|
|
95
|
+
|
|
96
|
+
### i18n System
|
|
97
|
+
|
|
98
|
+
- Locale state is global: `setLocale("en")` / `setLocale("zh-TW")`
|
|
99
|
+
- Translation keys are dot-separated: `common.email.invalid`
|
|
100
|
+
- Parameter substitution: `${paramName}` in message templates
|
|
101
|
+
- Each validator supports custom `i18n` overrides in options
|
|
102
|
+
|
|
103
|
+
### Taiwan Validators
|
|
104
|
+
|
|
105
|
+
Taiwan validators implement domain-specific algorithms:
|
|
106
|
+
- **National ID / Business ID**: Checksum verification with weighted sums
|
|
107
|
+
- **Postal Code**: Validates against official Chunghwa Post data (28KB+ of mappings)
|
|
108
|
+
- **Tel/Fax**: Area code + digit length rules per region
|
|
109
|
+
- **Mobile**: Operator prefix validation (090-099)
|
|
110
|
+
|
|
111
|
+
## Code Style
|
|
112
|
+
|
|
113
|
+
- **Formatter:** Prettier — no semicolons, double quotes, 200 char line width, 2-space indent
|
|
114
|
+
- **Linter:** ESLint 9 + typescript-eslint (recommended rules, `no-explicit-any` disabled)
|
|
115
|
+
- **TypeScript:** Strict mode, ESNext target, declaration files generated
|
|
116
|
+
- **Module:** ES Module (`"type": "module"` in package.json)
|
|
117
|
+
- **Tests:** Vitest with globals enabled, node environment
|
|
118
|
+
|
|
119
|
+
## Conventions
|
|
120
|
+
|
|
121
|
+
- Commit messages use **Conventional Commits**: `feat(scope):`, `fix(scope):`, `chore:`, `docs:`
|
|
122
|
+
- Every validator exports both a factory function and relevant type definitions
|
|
123
|
+
- Test files use `beforeEach(() => setLocale("en"))` for consistent locale
|
|
124
|
+
- Test structure: `describe("validatorName(required) features")` → nested `describe` per feature → `it` cases
|
|
125
|
+
- Source files are organized by domain: `common/` for universal, `taiwan/` for locale-specific
|
|
126
|
+
- No CI/CD pipelines — tests and builds are run manually or via `prepublishOnly` hook
|
|
127
|
+
|
|
128
|
+
## Build Output
|
|
129
|
+
|
|
130
|
+
`dist/` contains four files:
|
|
131
|
+
- `index.js` — ESM bundle
|
|
132
|
+
- `index.cjs` — CommonJS bundle
|
|
133
|
+
- `index.d.ts` — TypeScript declarations (ESM)
|
|
134
|
+
- `index.d.cts` — TypeScript declarations (CJS)
|
|
135
|
+
|
|
136
|
+
## Adding a New Validator
|
|
137
|
+
|
|
138
|
+
1. Create `src/validators/{domain}/{name}.ts` following the generic pattern above
|
|
139
|
+
2. Add i18n keys to both `src/i18n/locales/en.json` and `zh-TW.json`
|
|
140
|
+
3. Export from `src/index.ts`
|
|
141
|
+
4. Create `tests/{domain}/{name}.test.ts` with comprehensive coverage
|
|
142
|
+
5. Test with `npx vitest run tests/{domain}/{name}.test.ts`
|