@hy_ong/zod-kit 0.1.15 → 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.
Files changed (52) hide show
  1. package/CLAUDE.md +142 -0
  2. package/README.md +5 -5
  3. package/dist/index.cjs +1660 -4
  4. package/dist/index.d.cts +18 -18
  5. package/dist/index.d.ts +18 -18
  6. package/dist/index.js +1660 -4
  7. package/package.json +4 -3
  8. package/src/config.ts +2 -2
  9. package/src/i18n/index.ts +18 -2
  10. package/src/i18n/locales/en-GB.json +204 -0
  11. package/src/i18n/locales/id-ID.json +204 -0
  12. package/src/i18n/locales/ja-JP.json +204 -0
  13. package/src/i18n/locales/ko-KR.json +204 -0
  14. package/src/i18n/locales/ms-MY.json +204 -0
  15. package/src/i18n/locales/th-TH.json +204 -0
  16. package/src/i18n/locales/vi-VN.json +204 -0
  17. package/src/i18n/locales/zh-CN.json +204 -0
  18. package/src/validators/common/boolean.ts +1 -1
  19. package/src/validators/common/date.ts +1 -1
  20. package/src/validators/common/datetime.ts +1 -1
  21. package/src/validators/common/email.ts +1 -1
  22. package/src/validators/common/file.ts +1 -1
  23. package/src/validators/common/id.ts +1 -1
  24. package/src/validators/common/number.ts +1 -1
  25. package/src/validators/common/password.ts +1 -1
  26. package/src/validators/common/text.ts +1 -1
  27. package/src/validators/common/time.ts +1 -1
  28. package/src/validators/common/url.ts +1 -1
  29. package/src/validators/taiwan/business-id.ts +1 -1
  30. package/src/validators/taiwan/fax.ts +1 -1
  31. package/src/validators/taiwan/mobile.ts +1 -1
  32. package/src/validators/taiwan/national-id.ts +1 -1
  33. package/src/validators/taiwan/postal-code.ts +1 -1
  34. package/src/validators/taiwan/tel.ts +1 -1
  35. package/tests/common/boolean.test.ts +6 -6
  36. package/tests/common/date.test.ts +8 -8
  37. package/tests/common/datetime.test.ts +7 -7
  38. package/tests/common/email.test.ts +6 -6
  39. package/tests/common/file.test.ts +6 -6
  40. package/tests/common/id.test.ts +5 -5
  41. package/tests/common/number.test.ts +6 -6
  42. package/tests/common/password.test.ts +5 -5
  43. package/tests/common/text.test.ts +7 -7
  44. package/tests/common/time.test.ts +7 -7
  45. package/tests/common/url.test.ts +10 -10
  46. package/tests/taiwan/business-id.test.ts +4 -4
  47. package/tests/taiwan/fax.test.ts +7 -7
  48. package/tests/taiwan/mobile.test.ts +7 -7
  49. package/tests/taiwan/national-id.test.ts +4 -4
  50. package/tests/taiwan/postal-code.test.ts +6 -6
  51. package/tests/taiwan/tel.test.ts +7 -7
  52. /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`
package/README.md CHANGED
@@ -11,12 +11,12 @@ A comprehensive TypeScript library that provides pre-built validation schemas on
11
11
  - 🔍 **Pre-built schemas** - Common validation patterns ready to use
12
12
  - 🌐 **i18n support** - English and Traditional Chinese localization
13
13
  - 📝 **TypeScript-first** - Full type safety and IntelliSense support
14
- - ⚡ **Zero dependencies** - Built on top of Zod (peer dependency)
14
+ - ⚡ **Lightweight** - Minimal dependencies with Zod as peer dependency
15
15
  - 🎯 **Highly configurable** - Flexible options for every use case
16
16
  - 🇹🇼 **Taiwan-specific** - National ID, business ID, phone numbers, postal codes, etc.
17
17
  - ⏰ **Date/Time support** - Comprehensive datetime, time, and date validation
18
18
  - 📎 **File validation** - MIME type filtering, size constraints, and file type checking
19
- - 🧪 **Battle-tested** - Comprehensive test suite with 500+ tests
19
+ - 🧪 **Battle-tested** - Comprehensive test suite with 700+ tests
20
20
 
21
21
  ## 📦 Installation
22
22
 
@@ -32,7 +32,7 @@ yarn add @hy_ong/zod-kit zod
32
32
  pnpm add @hy_ong/zod-kit zod
33
33
  ```
34
34
 
35
- > **Note**: Zod is a peer dependency and must be installed separately.
35
+ > **Note**: Zod is a peer dependency (`^4.3.6`) and must be installed separately.
36
36
 
37
37
  ## 🚀 Quick Start
38
38
 
@@ -566,7 +566,7 @@ npm test
566
566
  npm run build
567
567
 
568
568
  # Run tests in watch mode
569
- npm run test:watch
569
+ npx vitest --watch
570
570
  ```
571
571
 
572
572
  ## 🧪 Testing
@@ -581,7 +581,7 @@ The library includes comprehensive tests covering:
581
581
 
582
582
  ```bash
583
583
  npm test # Run all tests
584
- npm run test:coverage # Run with coverage report
584
+ npx vitest --coverage # Run with coverage report
585
585
  ```
586
586
 
587
587
  ## 📄 License