@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,167 @@
|
|
|
1
|
+
import { ZodNumber, ZodNullable } from 'zod';
|
|
2
|
+
import { L as Locale } from '../config-CABSSvAp.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Number validator for Zod Kit
|
|
6
|
+
*
|
|
7
|
+
* Provides comprehensive number validation with type constraints, range validation,
|
|
8
|
+
* precision control, and advanced parsing features including comma-separated numbers.
|
|
9
|
+
*
|
|
10
|
+
* @author Ong Hoe Yuan
|
|
11
|
+
* @version 0.0.5
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type definition for number validation error messages
|
|
16
|
+
*
|
|
17
|
+
* @interface NumberMessages
|
|
18
|
+
* @property {string} [required] - Message when field is required but empty
|
|
19
|
+
* @property {string} [invalid] - Message when value is not a valid number
|
|
20
|
+
* @property {string} [integer] - Message when integer is required but float provided
|
|
21
|
+
* @property {string} [float] - Message when float is required but integer provided
|
|
22
|
+
* @property {string} [min] - Message when number is below minimum value
|
|
23
|
+
* @property {string} [max] - Message when number exceeds maximum value
|
|
24
|
+
* @property {string} [positive] - Message when positive number is required
|
|
25
|
+
* @property {string} [negative] - Message when negative number is required
|
|
26
|
+
* @property {string} [nonNegative] - Message when non-negative number is required
|
|
27
|
+
* @property {string} [nonPositive] - Message when non-positive number is required
|
|
28
|
+
* @property {string} [multipleOf] - Message when number is not a multiple of specified value
|
|
29
|
+
* @property {string} [finite] - Message when finite number is required
|
|
30
|
+
* @property {string} [precision] - Message when number has too many decimal places
|
|
31
|
+
*/
|
|
32
|
+
type NumberMessages = {
|
|
33
|
+
required?: string;
|
|
34
|
+
invalid?: string;
|
|
35
|
+
integer?: string;
|
|
36
|
+
float?: string;
|
|
37
|
+
min?: string;
|
|
38
|
+
max?: string;
|
|
39
|
+
positive?: string;
|
|
40
|
+
negative?: string;
|
|
41
|
+
nonNegative?: string;
|
|
42
|
+
nonPositive?: string;
|
|
43
|
+
multipleOf?: string;
|
|
44
|
+
finite?: string;
|
|
45
|
+
precision?: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Configuration options for number validation
|
|
49
|
+
*
|
|
50
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
51
|
+
*
|
|
52
|
+
* @interface NumberOptions
|
|
53
|
+
* @property {IsRequired} [required=true] - Whether the field is required
|
|
54
|
+
* @property {number} [min] - Minimum allowed value
|
|
55
|
+
* @property {number} [max] - Maximum allowed value
|
|
56
|
+
* @property {number | null} [defaultValue] - Default value when input is empty
|
|
57
|
+
* @property {"integer" | "float" | "both"} [type="both"] - Type constraint for the number
|
|
58
|
+
* @property {boolean} [positive] - Whether number must be positive (> 0)
|
|
59
|
+
* @property {boolean} [negative] - Whether number must be negative (< 0)
|
|
60
|
+
* @property {boolean} [nonNegative] - Whether number must be non-negative (>= 0)
|
|
61
|
+
* @property {boolean} [nonPositive] - Whether number must be non-positive (<= 0)
|
|
62
|
+
* @property {number} [multipleOf] - Number must be a multiple of this value
|
|
63
|
+
* @property {number} [precision] - Maximum number of decimal places allowed
|
|
64
|
+
* @property {boolean} [finite=true] - Whether to reject Infinity and -Infinity
|
|
65
|
+
* @property {Function} [transform] - Custom transformation function for number values
|
|
66
|
+
* @property {boolean} [parseCommas=false] - Whether to parse comma-separated numbers (e.g., "1,234")
|
|
67
|
+
* @property {Record<Locale, NumberMessages>} [i18n] - Custom error messages for different locales
|
|
68
|
+
*/
|
|
69
|
+
type NumberOptions<IsRequired extends boolean = true> = {
|
|
70
|
+
min?: number;
|
|
71
|
+
max?: number;
|
|
72
|
+
defaultValue?: IsRequired extends true ? number : number | null;
|
|
73
|
+
type?: "integer" | "float" | "both";
|
|
74
|
+
positive?: boolean;
|
|
75
|
+
negative?: boolean;
|
|
76
|
+
nonNegative?: boolean;
|
|
77
|
+
nonPositive?: boolean;
|
|
78
|
+
multipleOf?: number;
|
|
79
|
+
precision?: number;
|
|
80
|
+
finite?: boolean;
|
|
81
|
+
transform?: (value: number) => number;
|
|
82
|
+
parseCommas?: boolean;
|
|
83
|
+
i18n?: Partial<Record<Locale, Partial<NumberMessages>>>;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Type alias for number validation schema based on required flag
|
|
87
|
+
*
|
|
88
|
+
* @template IsRequired - Whether the field is required
|
|
89
|
+
* @typedef NumberSchema
|
|
90
|
+
* @description Returns ZodNumber if required, ZodNullable<ZodNumber> if optional
|
|
91
|
+
*/
|
|
92
|
+
type NumberSchema<IsRequired extends boolean> = IsRequired extends true ? ZodNumber : ZodNullable<ZodNumber>;
|
|
93
|
+
/**
|
|
94
|
+
* Creates a Zod schema for number validation with comprehensive constraints
|
|
95
|
+
*
|
|
96
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
97
|
+
* @param required
|
|
98
|
+
* @param {NumberOptions<IsRequired>} [options] - Configuration options for number validation
|
|
99
|
+
* @returns {NumberSchema<IsRequired>} Zod schema for number validation
|
|
100
|
+
*
|
|
101
|
+
* @description
|
|
102
|
+
* Creates a comprehensive number validator with type constraints, range validation,
|
|
103
|
+
* precision control, and advanced parsing features including comma-separated numbers.
|
|
104
|
+
*
|
|
105
|
+
* Features:
|
|
106
|
+
* - Type constraints (integer, float, or both)
|
|
107
|
+
* - Range validation (min/max)
|
|
108
|
+
* - Sign constraints (positive, negative, non-negative, non-positive)
|
|
109
|
+
* - Multiple-of validation
|
|
110
|
+
* - Precision control (decimal places)
|
|
111
|
+
* - Finite number validation
|
|
112
|
+
* - Comma-separated number parsing
|
|
113
|
+
* - Custom transformation functions
|
|
114
|
+
* - Comprehensive internationalization
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* // Basic number validation (optional by default)
|
|
119
|
+
* const basicSchema = number()
|
|
120
|
+
* basicSchema.parse(42) // ✓ Valid
|
|
121
|
+
* basicSchema.parse("42") // ✓ Valid (converted to number)
|
|
122
|
+
* basicSchema.parse(null) // ✓ Valid (optional)
|
|
123
|
+
*
|
|
124
|
+
* // Required number
|
|
125
|
+
* const requiredSchema = number(true)
|
|
126
|
+
* requiredSchema.parse(42) // ✓ Valid
|
|
127
|
+
* requiredSchema.parse(null) // ✗ Invalid (required)
|
|
128
|
+
*
|
|
129
|
+
* // Integer only
|
|
130
|
+
* const integerSchema = number(false, { type: "integer" })
|
|
131
|
+
* integerSchema.parse(42) // ✓ Valid
|
|
132
|
+
* integerSchema.parse(42.5) // ✗ Invalid
|
|
133
|
+
*
|
|
134
|
+
* // Range validation
|
|
135
|
+
* const rangeSchema = number(true, { min: 0, max: 100 })
|
|
136
|
+
* rangeSchema.parse(50) // ✓ Valid
|
|
137
|
+
* rangeSchema.parse(150) // ✗ Invalid
|
|
138
|
+
*
|
|
139
|
+
* // Positive numbers only
|
|
140
|
+
* const positiveSchema = number(true, { positive: true })
|
|
141
|
+
* positiveSchema.parse(5) // ✓ Valid
|
|
142
|
+
* positiveSchema.parse(-5) // ✗ Invalid
|
|
143
|
+
*
|
|
144
|
+
* // Multiple of constraint
|
|
145
|
+
* const multipleSchema = number(true, { multipleOf: 5 })
|
|
146
|
+
* multipleSchema.parse(10) // ✓ Valid
|
|
147
|
+
* multipleSchema.parse(7) // ✗ Invalid
|
|
148
|
+
*
|
|
149
|
+
* // Precision control
|
|
150
|
+
* const precisionSchema = number(true, { precision: 2 })
|
|
151
|
+
* precisionSchema.parse(3.14) // ✓ Valid
|
|
152
|
+
* precisionSchema.parse(3.14159) // ✗ Invalid
|
|
153
|
+
*
|
|
154
|
+
* // Comma-separated parsing
|
|
155
|
+
* const commaSchema = number(false, { parseCommas: true })
|
|
156
|
+
* commaSchema.parse("1,234.56") // ✓ Valid (parsed as 1234.56)
|
|
157
|
+
*
|
|
158
|
+
* // Optional with default
|
|
159
|
+
* const optionalSchema = number(false, { defaultValue: 0 })
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @throws {z.ZodError} When validation fails with specific error messages
|
|
163
|
+
* @see {@link NumberOptions} for all available configuration options
|
|
164
|
+
*/
|
|
165
|
+
declare function number<IsRequired extends boolean = false>(required?: IsRequired, options?: Omit<NumberOptions<IsRequired>, "required">): NumberSchema<IsRequired>;
|
|
166
|
+
|
|
167
|
+
export { type NumberMessages, type NumberOptions, type NumberSchema, number };
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { ZodNumber, ZodNullable } from 'zod';
|
|
2
|
+
import { L as Locale } from '../config-CABSSvAp.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Number validator for Zod Kit
|
|
6
|
+
*
|
|
7
|
+
* Provides comprehensive number validation with type constraints, range validation,
|
|
8
|
+
* precision control, and advanced parsing features including comma-separated numbers.
|
|
9
|
+
*
|
|
10
|
+
* @author Ong Hoe Yuan
|
|
11
|
+
* @version 0.0.5
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type definition for number validation error messages
|
|
16
|
+
*
|
|
17
|
+
* @interface NumberMessages
|
|
18
|
+
* @property {string} [required] - Message when field is required but empty
|
|
19
|
+
* @property {string} [invalid] - Message when value is not a valid number
|
|
20
|
+
* @property {string} [integer] - Message when integer is required but float provided
|
|
21
|
+
* @property {string} [float] - Message when float is required but integer provided
|
|
22
|
+
* @property {string} [min] - Message when number is below minimum value
|
|
23
|
+
* @property {string} [max] - Message when number exceeds maximum value
|
|
24
|
+
* @property {string} [positive] - Message when positive number is required
|
|
25
|
+
* @property {string} [negative] - Message when negative number is required
|
|
26
|
+
* @property {string} [nonNegative] - Message when non-negative number is required
|
|
27
|
+
* @property {string} [nonPositive] - Message when non-positive number is required
|
|
28
|
+
* @property {string} [multipleOf] - Message when number is not a multiple of specified value
|
|
29
|
+
* @property {string} [finite] - Message when finite number is required
|
|
30
|
+
* @property {string} [precision] - Message when number has too many decimal places
|
|
31
|
+
*/
|
|
32
|
+
type NumberMessages = {
|
|
33
|
+
required?: string;
|
|
34
|
+
invalid?: string;
|
|
35
|
+
integer?: string;
|
|
36
|
+
float?: string;
|
|
37
|
+
min?: string;
|
|
38
|
+
max?: string;
|
|
39
|
+
positive?: string;
|
|
40
|
+
negative?: string;
|
|
41
|
+
nonNegative?: string;
|
|
42
|
+
nonPositive?: string;
|
|
43
|
+
multipleOf?: string;
|
|
44
|
+
finite?: string;
|
|
45
|
+
precision?: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Configuration options for number validation
|
|
49
|
+
*
|
|
50
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
51
|
+
*
|
|
52
|
+
* @interface NumberOptions
|
|
53
|
+
* @property {IsRequired} [required=true] - Whether the field is required
|
|
54
|
+
* @property {number} [min] - Minimum allowed value
|
|
55
|
+
* @property {number} [max] - Maximum allowed value
|
|
56
|
+
* @property {number | null} [defaultValue] - Default value when input is empty
|
|
57
|
+
* @property {"integer" | "float" | "both"} [type="both"] - Type constraint for the number
|
|
58
|
+
* @property {boolean} [positive] - Whether number must be positive (> 0)
|
|
59
|
+
* @property {boolean} [negative] - Whether number must be negative (< 0)
|
|
60
|
+
* @property {boolean} [nonNegative] - Whether number must be non-negative (>= 0)
|
|
61
|
+
* @property {boolean} [nonPositive] - Whether number must be non-positive (<= 0)
|
|
62
|
+
* @property {number} [multipleOf] - Number must be a multiple of this value
|
|
63
|
+
* @property {number} [precision] - Maximum number of decimal places allowed
|
|
64
|
+
* @property {boolean} [finite=true] - Whether to reject Infinity and -Infinity
|
|
65
|
+
* @property {Function} [transform] - Custom transformation function for number values
|
|
66
|
+
* @property {boolean} [parseCommas=false] - Whether to parse comma-separated numbers (e.g., "1,234")
|
|
67
|
+
* @property {Record<Locale, NumberMessages>} [i18n] - Custom error messages for different locales
|
|
68
|
+
*/
|
|
69
|
+
type NumberOptions<IsRequired extends boolean = true> = {
|
|
70
|
+
min?: number;
|
|
71
|
+
max?: number;
|
|
72
|
+
defaultValue?: IsRequired extends true ? number : number | null;
|
|
73
|
+
type?: "integer" | "float" | "both";
|
|
74
|
+
positive?: boolean;
|
|
75
|
+
negative?: boolean;
|
|
76
|
+
nonNegative?: boolean;
|
|
77
|
+
nonPositive?: boolean;
|
|
78
|
+
multipleOf?: number;
|
|
79
|
+
precision?: number;
|
|
80
|
+
finite?: boolean;
|
|
81
|
+
transform?: (value: number) => number;
|
|
82
|
+
parseCommas?: boolean;
|
|
83
|
+
i18n?: Partial<Record<Locale, Partial<NumberMessages>>>;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Type alias for number validation schema based on required flag
|
|
87
|
+
*
|
|
88
|
+
* @template IsRequired - Whether the field is required
|
|
89
|
+
* @typedef NumberSchema
|
|
90
|
+
* @description Returns ZodNumber if required, ZodNullable<ZodNumber> if optional
|
|
91
|
+
*/
|
|
92
|
+
type NumberSchema<IsRequired extends boolean> = IsRequired extends true ? ZodNumber : ZodNullable<ZodNumber>;
|
|
93
|
+
/**
|
|
94
|
+
* Creates a Zod schema for number validation with comprehensive constraints
|
|
95
|
+
*
|
|
96
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
97
|
+
* @param required
|
|
98
|
+
* @param {NumberOptions<IsRequired>} [options] - Configuration options for number validation
|
|
99
|
+
* @returns {NumberSchema<IsRequired>} Zod schema for number validation
|
|
100
|
+
*
|
|
101
|
+
* @description
|
|
102
|
+
* Creates a comprehensive number validator with type constraints, range validation,
|
|
103
|
+
* precision control, and advanced parsing features including comma-separated numbers.
|
|
104
|
+
*
|
|
105
|
+
* Features:
|
|
106
|
+
* - Type constraints (integer, float, or both)
|
|
107
|
+
* - Range validation (min/max)
|
|
108
|
+
* - Sign constraints (positive, negative, non-negative, non-positive)
|
|
109
|
+
* - Multiple-of validation
|
|
110
|
+
* - Precision control (decimal places)
|
|
111
|
+
* - Finite number validation
|
|
112
|
+
* - Comma-separated number parsing
|
|
113
|
+
* - Custom transformation functions
|
|
114
|
+
* - Comprehensive internationalization
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* // Basic number validation (optional by default)
|
|
119
|
+
* const basicSchema = number()
|
|
120
|
+
* basicSchema.parse(42) // ✓ Valid
|
|
121
|
+
* basicSchema.parse("42") // ✓ Valid (converted to number)
|
|
122
|
+
* basicSchema.parse(null) // ✓ Valid (optional)
|
|
123
|
+
*
|
|
124
|
+
* // Required number
|
|
125
|
+
* const requiredSchema = number(true)
|
|
126
|
+
* requiredSchema.parse(42) // ✓ Valid
|
|
127
|
+
* requiredSchema.parse(null) // ✗ Invalid (required)
|
|
128
|
+
*
|
|
129
|
+
* // Integer only
|
|
130
|
+
* const integerSchema = number(false, { type: "integer" })
|
|
131
|
+
* integerSchema.parse(42) // ✓ Valid
|
|
132
|
+
* integerSchema.parse(42.5) // ✗ Invalid
|
|
133
|
+
*
|
|
134
|
+
* // Range validation
|
|
135
|
+
* const rangeSchema = number(true, { min: 0, max: 100 })
|
|
136
|
+
* rangeSchema.parse(50) // ✓ Valid
|
|
137
|
+
* rangeSchema.parse(150) // ✗ Invalid
|
|
138
|
+
*
|
|
139
|
+
* // Positive numbers only
|
|
140
|
+
* const positiveSchema = number(true, { positive: true })
|
|
141
|
+
* positiveSchema.parse(5) // ✓ Valid
|
|
142
|
+
* positiveSchema.parse(-5) // ✗ Invalid
|
|
143
|
+
*
|
|
144
|
+
* // Multiple of constraint
|
|
145
|
+
* const multipleSchema = number(true, { multipleOf: 5 })
|
|
146
|
+
* multipleSchema.parse(10) // ✓ Valid
|
|
147
|
+
* multipleSchema.parse(7) // ✗ Invalid
|
|
148
|
+
*
|
|
149
|
+
* // Precision control
|
|
150
|
+
* const precisionSchema = number(true, { precision: 2 })
|
|
151
|
+
* precisionSchema.parse(3.14) // ✓ Valid
|
|
152
|
+
* precisionSchema.parse(3.14159) // ✗ Invalid
|
|
153
|
+
*
|
|
154
|
+
* // Comma-separated parsing
|
|
155
|
+
* const commaSchema = number(false, { parseCommas: true })
|
|
156
|
+
* commaSchema.parse("1,234.56") // ✓ Valid (parsed as 1234.56)
|
|
157
|
+
*
|
|
158
|
+
* // Optional with default
|
|
159
|
+
* const optionalSchema = number(false, { defaultValue: 0 })
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @throws {z.ZodError} When validation fails with specific error messages
|
|
163
|
+
* @see {@link NumberOptions} for all available configuration options
|
|
164
|
+
*/
|
|
165
|
+
declare function number<IsRequired extends boolean = false>(required?: IsRequired, options?: Omit<NumberOptions<IsRequired>, "required">): NumberSchema<IsRequired>;
|
|
166
|
+
|
|
167
|
+
export { type NumberMessages, type NumberOptions, type NumberSchema, number };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { ZodString, ZodNullable } from 'zod';
|
|
2
|
+
import { L as Locale } from '../config-CABSSvAp.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview Password validator for Zod Kit
|
|
6
|
+
*
|
|
7
|
+
* Provides comprehensive password validation with strength analysis, character requirements,
|
|
8
|
+
* security checks, and protection against common weak passwords.
|
|
9
|
+
*
|
|
10
|
+
* @author Ong Hoe Yuan
|
|
11
|
+
* @version 0.0.5
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type definition for password validation error messages
|
|
16
|
+
*
|
|
17
|
+
* @interface PasswordMessages
|
|
18
|
+
* @property {string} [required] - Message when field is required but empty
|
|
19
|
+
* @property {string} [min] - Message when password is too short
|
|
20
|
+
* @property {string} [max] - Message when password is too long
|
|
21
|
+
* @property {string} [uppercase] - Message when uppercase letters are required
|
|
22
|
+
* @property {string} [lowercase] - Message when lowercase letters are required
|
|
23
|
+
* @property {string} [digits] - Message when digits are required
|
|
24
|
+
* @property {string} [special] - Message when special characters are required
|
|
25
|
+
* @property {string} [noRepeating] - Message when repeating characters are forbidden
|
|
26
|
+
* @property {string} [noSequential] - Message when sequential characters are forbidden
|
|
27
|
+
* @property {string} [noCommonWords] - Message when common passwords are forbidden
|
|
28
|
+
* @property {string} [minStrength] - Message when password strength is insufficient
|
|
29
|
+
* @property {string} [excludes] - Message when password contains forbidden strings
|
|
30
|
+
* @property {string} [includes] - Message when password doesn't contain required string
|
|
31
|
+
* @property {string} [invalid] - Message when password doesn't match custom regex
|
|
32
|
+
*/
|
|
33
|
+
type PasswordMessages = {
|
|
34
|
+
required?: string;
|
|
35
|
+
min?: string;
|
|
36
|
+
max?: string;
|
|
37
|
+
uppercase?: string;
|
|
38
|
+
lowercase?: string;
|
|
39
|
+
digits?: string;
|
|
40
|
+
special?: string;
|
|
41
|
+
noRepeating?: string;
|
|
42
|
+
noSequential?: string;
|
|
43
|
+
noCommonWords?: string;
|
|
44
|
+
minStrength?: string;
|
|
45
|
+
excludes?: string;
|
|
46
|
+
includes?: string;
|
|
47
|
+
invalid?: string;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Password strength levels used for validation
|
|
51
|
+
*
|
|
52
|
+
* @typedef {"weak" | "medium" | "strong" | "very-strong"} PasswordStrength
|
|
53
|
+
* @description
|
|
54
|
+
* - weak: Basic passwords with minimal requirements
|
|
55
|
+
* - medium: Passwords with some character variety
|
|
56
|
+
* - strong: Passwords with good character variety and length
|
|
57
|
+
* - very-strong: Passwords with excellent character variety, length, and complexity
|
|
58
|
+
*/
|
|
59
|
+
type PasswordStrength = "weak" | "medium" | "strong" | "very-strong";
|
|
60
|
+
/**
|
|
61
|
+
* Configuration options for password validation
|
|
62
|
+
*
|
|
63
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
64
|
+
*
|
|
65
|
+
* @interface PasswordOptions
|
|
66
|
+
* @property {IsRequired} [required=true] - Whether the field is required
|
|
67
|
+
* @property {number} [min] - Minimum length of password
|
|
68
|
+
* @property {number} [max] - Maximum length of password
|
|
69
|
+
* @property {boolean} [uppercase] - Whether uppercase letters are required
|
|
70
|
+
* @property {boolean} [lowercase] - Whether lowercase letters are required
|
|
71
|
+
* @property {boolean} [digits] - Whether digits are required
|
|
72
|
+
* @property {boolean} [special] - Whether special characters are required
|
|
73
|
+
* @property {boolean} [noRepeating] - Whether to forbid repeating characters (3+ in a row)
|
|
74
|
+
* @property {boolean} [noSequential] - Whether to forbid sequential characters (abc, 123)
|
|
75
|
+
* @property {boolean} [noCommonWords] - Whether to forbid common weak passwords
|
|
76
|
+
* @property {PasswordStrength} [minStrength] - Minimum required password strength
|
|
77
|
+
* @property {string | string[]} [excludes] - String(s) that must not be included
|
|
78
|
+
* @property {string} [includes] - String that must be included in password
|
|
79
|
+
* @property {RegExp} [regex] - Custom regex pattern for validation
|
|
80
|
+
* @property {Function} [transform] - Custom transformation function for password
|
|
81
|
+
* @property {string | null} [defaultValue] - Default value when input is empty
|
|
82
|
+
* @property {Record<Locale, PasswordMessages>} [i18n] - Custom error messages for different locales
|
|
83
|
+
*/
|
|
84
|
+
type PasswordOptions<IsRequired extends boolean = true> = {
|
|
85
|
+
min?: number;
|
|
86
|
+
max?: number;
|
|
87
|
+
uppercase?: boolean;
|
|
88
|
+
lowercase?: boolean;
|
|
89
|
+
digits?: boolean;
|
|
90
|
+
special?: boolean;
|
|
91
|
+
noRepeating?: boolean;
|
|
92
|
+
noSequential?: boolean;
|
|
93
|
+
noCommonWords?: boolean;
|
|
94
|
+
minStrength?: PasswordStrength;
|
|
95
|
+
excludes?: string | string[];
|
|
96
|
+
includes?: string;
|
|
97
|
+
regex?: RegExp;
|
|
98
|
+
transform?: (value: string) => string;
|
|
99
|
+
defaultValue?: IsRequired extends true ? string : string | null;
|
|
100
|
+
i18n?: Partial<Record<Locale, Partial<PasswordMessages>>>;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Type alias for password validation schema based on required flag
|
|
104
|
+
*
|
|
105
|
+
* @template IsRequired - Whether the field is required
|
|
106
|
+
* @typedef PasswordSchema
|
|
107
|
+
* @description Returns ZodString if required, ZodNullable<ZodString> if optional
|
|
108
|
+
*/
|
|
109
|
+
type PasswordSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
|
|
110
|
+
/**
|
|
111
|
+
* Creates a Zod schema for password validation with comprehensive security checks
|
|
112
|
+
*
|
|
113
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
114
|
+
* @param {IsRequired} [required=false] - Whether the field is required
|
|
115
|
+
* @param {Omit<ValidatorOptions<IsRequired>, 'required'>} [options] - Configuration options for validation
|
|
116
|
+
* @returns {PasswordSchema<IsRequired>} Zod schema for password validation
|
|
117
|
+
*
|
|
118
|
+
* @description
|
|
119
|
+
* Creates a comprehensive password validator with strength analysis, character requirements,
|
|
120
|
+
* security checks, and protection against common weak passwords.
|
|
121
|
+
*
|
|
122
|
+
* Features:
|
|
123
|
+
* - Length validation (min/max)
|
|
124
|
+
* - Character requirements (uppercase, lowercase, digits, special)
|
|
125
|
+
* - Security checks (no repeating, no sequential patterns)
|
|
126
|
+
* - Common password detection
|
|
127
|
+
* - Strength analysis with configurable minimum levels
|
|
128
|
+
* - Content inclusion/exclusion
|
|
129
|
+
* - Custom regex patterns
|
|
130
|
+
* - Custom transformation functions
|
|
131
|
+
* - Comprehensive internationalization
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* // Basic password validation
|
|
136
|
+
* const basicSchema = password() // optional by default
|
|
137
|
+
* basicSchema.parse("MyPassword123!") // ✓ Valid
|
|
138
|
+
* basicSchema.parse(null) // ✓ Valid (optional)
|
|
139
|
+
*
|
|
140
|
+
* // Required validation
|
|
141
|
+
* const requiredSchema = parse("MyPassword123!") // ✓ Valid
|
|
142
|
+
(true)
|
|
143
|
+
* requiredSchema.parse(null) // ✗ Invalid (required)
|
|
144
|
+
*
|
|
145
|
+
*
|
|
146
|
+
* // Strong password requirements
|
|
147
|
+
* const strongSchema = password(false, {
|
|
148
|
+
* min: 12,
|
|
149
|
+
* uppercase: true,
|
|
150
|
+
* lowercase: true,
|
|
151
|
+
* digits: true,
|
|
152
|
+
* special: true,
|
|
153
|
+
* minStrength: "strong"
|
|
154
|
+
* })
|
|
155
|
+
*
|
|
156
|
+
* // No common passwords
|
|
157
|
+
* const secureSchema = password(false, {
|
|
158
|
+
* noCommonWords: true,
|
|
159
|
+
* noRepeating: true,
|
|
160
|
+
* noSequential: true
|
|
161
|
+
* })
|
|
162
|
+
* secureSchema.parse("password123") // ✗ Invalid (common password)
|
|
163
|
+
* secureSchema.parse("aaa123") // ✗ Invalid (repeating characters)
|
|
164
|
+
* secureSchema.parse("abc123") // ✗ Invalid (sequential characters)
|
|
165
|
+
*
|
|
166
|
+
* // Custom requirements
|
|
167
|
+
* const customSchema = password(false, {
|
|
168
|
+
* min: 8,
|
|
169
|
+
* includes: "@", // Must contain @
|
|
170
|
+
* excludes: ["admin", "user"], // Cannot contain these words
|
|
171
|
+
* regex: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)/ // Custom pattern
|
|
172
|
+
* })
|
|
173
|
+
*
|
|
174
|
+
* // Minimum strength requirement
|
|
175
|
+
* const strengthSchema = password(false, { minStrength: "very-strong" })
|
|
176
|
+
* strengthSchema.parse("weak") // ✗ Invalid (insufficient strength)
|
|
177
|
+
* strengthSchema.parse("MyVeryStr0ng!P@ssw0rd2024") // ✓ Valid
|
|
178
|
+
*
|
|
179
|
+
* // Optional with default
|
|
180
|
+
* const optionalSchema = password(false, {
|
|
181
|
+
* defaultValue: null
|
|
182
|
+
* })
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* @throws {z.ZodError} When validation fails with specific error messages
|
|
186
|
+
* @see {@link PasswordOptions} for all available configuration options
|
|
187
|
+
* @see {@link PasswordStrength} for strength level definitions
|
|
188
|
+
* @see {@link calculatePasswordStrength} for strength calculation logic
|
|
189
|
+
*/
|
|
190
|
+
declare function password<IsRequired extends boolean = false>(required?: IsRequired, options?: Omit<PasswordOptions<IsRequired>, 'required'>): PasswordSchema<IsRequired>;
|
|
191
|
+
|
|
192
|
+
export { type PasswordMessages, type PasswordOptions, type PasswordSchema, type PasswordStrength, password };
|