@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,196 @@
|
|
|
1
|
+
import { ZodString, ZodNullable } from 'zod';
|
|
2
|
+
import { L as Locale } from '../config-CABSSvAp.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview URL validator for Zod Kit
|
|
6
|
+
*
|
|
7
|
+
* Provides comprehensive URL validation with protocol filtering, domain control,
|
|
8
|
+
* port validation, path constraints, and localhost handling.
|
|
9
|
+
*
|
|
10
|
+
* @author Ong Hoe Yuan
|
|
11
|
+
* @version 0.0.5
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type definition for URL validation error messages
|
|
16
|
+
*
|
|
17
|
+
* @interface UrlMessages
|
|
18
|
+
* @property {string} [required] - Message when field is required but empty
|
|
19
|
+
* @property {string} [invalid] - Message when URL format is invalid
|
|
20
|
+
* @property {string} [min] - Message when URL is too short
|
|
21
|
+
* @property {string} [max] - Message when URL is too long
|
|
22
|
+
* @property {string} [includes] - Message when URL doesn't contain required string
|
|
23
|
+
* @property {string} [excludes] - Message when URL contains forbidden string
|
|
24
|
+
* @property {string} [protocol] - Message when protocol is not allowed
|
|
25
|
+
* @property {string} [domain] - Message when domain is not allowed
|
|
26
|
+
* @property {string} [domainBlacklist] - Message when domain is blacklisted
|
|
27
|
+
* @property {string} [port] - Message when port is not allowed
|
|
28
|
+
* @property {string} [pathStartsWith] - Message when path doesn't start with required string
|
|
29
|
+
* @property {string} [pathEndsWith] - Message when path doesn't end with required string
|
|
30
|
+
* @property {string} [hasQuery] - Message when query parameters are required
|
|
31
|
+
* @property {string} [noQuery] - Message when query parameters are forbidden
|
|
32
|
+
* @property {string} [hasFragment] - Message when fragment is required
|
|
33
|
+
* @property {string} [noFragment] - Message when fragment is forbidden
|
|
34
|
+
* @property {string} [localhost] - Message when localhost is forbidden
|
|
35
|
+
* @property {string} [noLocalhost] - Message when localhost is required
|
|
36
|
+
*/
|
|
37
|
+
type UrlMessages = {
|
|
38
|
+
required?: string;
|
|
39
|
+
invalid?: string;
|
|
40
|
+
min?: string;
|
|
41
|
+
max?: string;
|
|
42
|
+
includes?: string;
|
|
43
|
+
excludes?: string;
|
|
44
|
+
protocol?: string;
|
|
45
|
+
domain?: string;
|
|
46
|
+
domainBlacklist?: string;
|
|
47
|
+
port?: string;
|
|
48
|
+
pathStartsWith?: string;
|
|
49
|
+
pathEndsWith?: string;
|
|
50
|
+
hasQuery?: string;
|
|
51
|
+
noQuery?: string;
|
|
52
|
+
hasFragment?: string;
|
|
53
|
+
noFragment?: string;
|
|
54
|
+
localhost?: string;
|
|
55
|
+
noLocalhost?: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Configuration options for URL validation
|
|
59
|
+
*
|
|
60
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
61
|
+
*
|
|
62
|
+
* @interface UrlOptions
|
|
63
|
+
* @property {IsRequired} [required=true] - Whether the field is required
|
|
64
|
+
* @property {number} [min] - Minimum length of URL
|
|
65
|
+
* @property {number} [max] - Maximum length of URL
|
|
66
|
+
* @property {string} [includes] - String that must be included in URL
|
|
67
|
+
* @property {string | string[]} [excludes] - String(s) that must not be included
|
|
68
|
+
* @property {string[]} [protocols] - Allowed protocols (e.g., ["https", "http"])
|
|
69
|
+
* @property {string[]} [allowedDomains] - Domains that are allowed
|
|
70
|
+
* @property {string[]} [blockedDomains] - Domains that are blocked
|
|
71
|
+
* @property {number[]} [allowedPorts] - Ports that are allowed
|
|
72
|
+
* @property {number[]} [blockedPorts] - Ports that are blocked
|
|
73
|
+
* @property {string} [pathStartsWith] - Path must start with this string
|
|
74
|
+
* @property {string} [pathEndsWith] - Path must end with this string
|
|
75
|
+
* @property {boolean} [mustHaveQuery] - Whether URL must have query parameters
|
|
76
|
+
* @property {boolean} [mustNotHaveQuery] - Whether URL must not have query parameters
|
|
77
|
+
* @property {boolean} [mustHaveFragment] - Whether URL must have fragment
|
|
78
|
+
* @property {boolean} [mustNotHaveFragment] - Whether URL must not have fragment
|
|
79
|
+
* @property {boolean} [allowLocalhost=true] - Whether to allow localhost URLs
|
|
80
|
+
* @property {boolean} [blockLocalhost] - Whether to explicitly block localhost URLs
|
|
81
|
+
* @property {Function} [transform] - Custom transformation function for URL strings
|
|
82
|
+
* @property {string | null} [defaultValue] - Default value when input is empty
|
|
83
|
+
* @property {Record<Locale, UrlMessages>} [i18n] - Custom error messages for different locales
|
|
84
|
+
*/
|
|
85
|
+
type UrlOptions<IsRequired extends boolean = true> = {
|
|
86
|
+
min?: number;
|
|
87
|
+
max?: number;
|
|
88
|
+
includes?: string;
|
|
89
|
+
excludes?: string | string[];
|
|
90
|
+
protocols?: string[];
|
|
91
|
+
allowedDomains?: string[];
|
|
92
|
+
blockedDomains?: string[];
|
|
93
|
+
allowedPorts?: number[];
|
|
94
|
+
blockedPorts?: number[];
|
|
95
|
+
pathStartsWith?: string;
|
|
96
|
+
pathEndsWith?: string;
|
|
97
|
+
mustHaveQuery?: boolean;
|
|
98
|
+
mustNotHaveQuery?: boolean;
|
|
99
|
+
mustHaveFragment?: boolean;
|
|
100
|
+
mustNotHaveFragment?: boolean;
|
|
101
|
+
allowLocalhost?: boolean;
|
|
102
|
+
blockLocalhost?: boolean;
|
|
103
|
+
transform?: (value: string) => string;
|
|
104
|
+
defaultValue?: IsRequired extends true ? string : string | null;
|
|
105
|
+
i18n?: Partial<Record<Locale, Partial<UrlMessages>>>;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Type alias for URL validation schema based on required flag
|
|
109
|
+
*
|
|
110
|
+
* @template IsRequired - Whether the field is required
|
|
111
|
+
* @typedef UrlSchema
|
|
112
|
+
* @description Returns ZodString if required, ZodNullable<ZodString> if optional
|
|
113
|
+
*/
|
|
114
|
+
type UrlSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
|
|
115
|
+
/**
|
|
116
|
+
* Creates a Zod schema for URL validation with comprehensive constraints
|
|
117
|
+
*
|
|
118
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
119
|
+
* @param {IsRequired} [required=false] - Whether the field is required
|
|
120
|
+
* @param {Omit<ValidatorOptions<IsRequired>, 'required'>} [options] - Configuration options for validation
|
|
121
|
+
* @returns {UrlSchema<IsRequired>} Zod schema for URL validation
|
|
122
|
+
*
|
|
123
|
+
* @description
|
|
124
|
+
* Creates a comprehensive URL validator with protocol filtering, domain control,
|
|
125
|
+
* port validation, path constraints, and localhost handling.
|
|
126
|
+
*
|
|
127
|
+
* Features:
|
|
128
|
+
* - RFC-compliant URL format validation
|
|
129
|
+
* - Protocol whitelist/blacklist (http, https, ftp, etc.)
|
|
130
|
+
* - Domain whitelist/blacklist with subdomain support
|
|
131
|
+
* - Port validation and filtering
|
|
132
|
+
* - Path prefix/suffix validation
|
|
133
|
+
* - Query parameter requirements
|
|
134
|
+
* - Fragment requirements
|
|
135
|
+
* - Localhost detection and control
|
|
136
|
+
* - Length validation
|
|
137
|
+
* - Content inclusion/exclusion
|
|
138
|
+
* - Custom transformation functions
|
|
139
|
+
* - Comprehensive internationalization
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* // Basic URL validation
|
|
144
|
+
* const basicSchema = url() // optional by default
|
|
145
|
+
* basicSchema.parse("https://example.com") // ✓ Valid
|
|
146
|
+
* basicSchema.parse(null) // ✓ Valid (optional)
|
|
147
|
+
*
|
|
148
|
+
* // Required validation
|
|
149
|
+
* const requiredSchema = parse("https://example.com") // ✓ Valid
|
|
150
|
+
(true)
|
|
151
|
+
* requiredSchema.parse(null) // ✗ Invalid (required)
|
|
152
|
+
*
|
|
153
|
+
*
|
|
154
|
+
* // HTTPS only
|
|
155
|
+
* const httpsSchema = url(false, { protocols: ["https"] })
|
|
156
|
+
* httpsSchema.parse("https://example.com") // ✓ Valid
|
|
157
|
+
* httpsSchema.parse("http://example.com") // ✗ Invalid
|
|
158
|
+
*
|
|
159
|
+
* // Domain restriction
|
|
160
|
+
* const domainSchema = url(false, {
|
|
161
|
+
* allowedDomains: ["company.com", "trusted.org"]
|
|
162
|
+
* })
|
|
163
|
+
* domainSchema.parse("https://app.company.com") // ✓ Valid (subdomain)
|
|
164
|
+
* domainSchema.parse("https://example.com") // ✗ Invalid
|
|
165
|
+
*
|
|
166
|
+
* // Block localhost
|
|
167
|
+
* const noLocalhostSchema = url(false, { blockLocalhost: true })
|
|
168
|
+
* noLocalhostSchema.parse("https://example.com") // ✓ Valid
|
|
169
|
+
* noLocalhostSchema.parse("http://localhost:3000") // ✗ Invalid
|
|
170
|
+
*
|
|
171
|
+
* // API endpoints with path requirements
|
|
172
|
+
* const apiSchema = url(false, {
|
|
173
|
+
* pathStartsWith: "/api/",
|
|
174
|
+
* mustHaveQuery: true
|
|
175
|
+
* })
|
|
176
|
+
* apiSchema.parse("https://api.com/api/users?page=1") // ✓ Valid
|
|
177
|
+
*
|
|
178
|
+
* // Port restrictions
|
|
179
|
+
* const portSchema = url(false, {
|
|
180
|
+
* allowedPorts: [80, 443, 8080]
|
|
181
|
+
* })
|
|
182
|
+
* portSchema.parse("https://example.com:443") // ✓ Valid
|
|
183
|
+
* portSchema.parse("https://example.com:3000") // ✗ Invalid
|
|
184
|
+
*
|
|
185
|
+
* // Optional with default
|
|
186
|
+
* const optionalSchema = url(false, {
|
|
187
|
+
* defaultValue: null
|
|
188
|
+
* })
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @throws {z.ZodError} When validation fails with specific error messages
|
|
192
|
+
* @see {@link UrlOptions} for all available configuration options
|
|
193
|
+
*/
|
|
194
|
+
declare function url<IsRequired extends boolean = false>(required?: IsRequired, options?: Omit<UrlOptions<IsRequired>, 'required'>): UrlSchema<IsRequired>;
|
|
195
|
+
|
|
196
|
+
export { type UrlMessages, type UrlOptions, type UrlSchema, url };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { ZodString, ZodNullable } from 'zod';
|
|
2
|
+
import { L as Locale } from '../config-CABSSvAp.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @fileoverview URL validator for Zod Kit
|
|
6
|
+
*
|
|
7
|
+
* Provides comprehensive URL validation with protocol filtering, domain control,
|
|
8
|
+
* port validation, path constraints, and localhost handling.
|
|
9
|
+
*
|
|
10
|
+
* @author Ong Hoe Yuan
|
|
11
|
+
* @version 0.0.5
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type definition for URL validation error messages
|
|
16
|
+
*
|
|
17
|
+
* @interface UrlMessages
|
|
18
|
+
* @property {string} [required] - Message when field is required but empty
|
|
19
|
+
* @property {string} [invalid] - Message when URL format is invalid
|
|
20
|
+
* @property {string} [min] - Message when URL is too short
|
|
21
|
+
* @property {string} [max] - Message when URL is too long
|
|
22
|
+
* @property {string} [includes] - Message when URL doesn't contain required string
|
|
23
|
+
* @property {string} [excludes] - Message when URL contains forbidden string
|
|
24
|
+
* @property {string} [protocol] - Message when protocol is not allowed
|
|
25
|
+
* @property {string} [domain] - Message when domain is not allowed
|
|
26
|
+
* @property {string} [domainBlacklist] - Message when domain is blacklisted
|
|
27
|
+
* @property {string} [port] - Message when port is not allowed
|
|
28
|
+
* @property {string} [pathStartsWith] - Message when path doesn't start with required string
|
|
29
|
+
* @property {string} [pathEndsWith] - Message when path doesn't end with required string
|
|
30
|
+
* @property {string} [hasQuery] - Message when query parameters are required
|
|
31
|
+
* @property {string} [noQuery] - Message when query parameters are forbidden
|
|
32
|
+
* @property {string} [hasFragment] - Message when fragment is required
|
|
33
|
+
* @property {string} [noFragment] - Message when fragment is forbidden
|
|
34
|
+
* @property {string} [localhost] - Message when localhost is forbidden
|
|
35
|
+
* @property {string} [noLocalhost] - Message when localhost is required
|
|
36
|
+
*/
|
|
37
|
+
type UrlMessages = {
|
|
38
|
+
required?: string;
|
|
39
|
+
invalid?: string;
|
|
40
|
+
min?: string;
|
|
41
|
+
max?: string;
|
|
42
|
+
includes?: string;
|
|
43
|
+
excludes?: string;
|
|
44
|
+
protocol?: string;
|
|
45
|
+
domain?: string;
|
|
46
|
+
domainBlacklist?: string;
|
|
47
|
+
port?: string;
|
|
48
|
+
pathStartsWith?: string;
|
|
49
|
+
pathEndsWith?: string;
|
|
50
|
+
hasQuery?: string;
|
|
51
|
+
noQuery?: string;
|
|
52
|
+
hasFragment?: string;
|
|
53
|
+
noFragment?: string;
|
|
54
|
+
localhost?: string;
|
|
55
|
+
noLocalhost?: string;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Configuration options for URL validation
|
|
59
|
+
*
|
|
60
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
61
|
+
*
|
|
62
|
+
* @interface UrlOptions
|
|
63
|
+
* @property {IsRequired} [required=true] - Whether the field is required
|
|
64
|
+
* @property {number} [min] - Minimum length of URL
|
|
65
|
+
* @property {number} [max] - Maximum length of URL
|
|
66
|
+
* @property {string} [includes] - String that must be included in URL
|
|
67
|
+
* @property {string | string[]} [excludes] - String(s) that must not be included
|
|
68
|
+
* @property {string[]} [protocols] - Allowed protocols (e.g., ["https", "http"])
|
|
69
|
+
* @property {string[]} [allowedDomains] - Domains that are allowed
|
|
70
|
+
* @property {string[]} [blockedDomains] - Domains that are blocked
|
|
71
|
+
* @property {number[]} [allowedPorts] - Ports that are allowed
|
|
72
|
+
* @property {number[]} [blockedPorts] - Ports that are blocked
|
|
73
|
+
* @property {string} [pathStartsWith] - Path must start with this string
|
|
74
|
+
* @property {string} [pathEndsWith] - Path must end with this string
|
|
75
|
+
* @property {boolean} [mustHaveQuery] - Whether URL must have query parameters
|
|
76
|
+
* @property {boolean} [mustNotHaveQuery] - Whether URL must not have query parameters
|
|
77
|
+
* @property {boolean} [mustHaveFragment] - Whether URL must have fragment
|
|
78
|
+
* @property {boolean} [mustNotHaveFragment] - Whether URL must not have fragment
|
|
79
|
+
* @property {boolean} [allowLocalhost=true] - Whether to allow localhost URLs
|
|
80
|
+
* @property {boolean} [blockLocalhost] - Whether to explicitly block localhost URLs
|
|
81
|
+
* @property {Function} [transform] - Custom transformation function for URL strings
|
|
82
|
+
* @property {string | null} [defaultValue] - Default value when input is empty
|
|
83
|
+
* @property {Record<Locale, UrlMessages>} [i18n] - Custom error messages for different locales
|
|
84
|
+
*/
|
|
85
|
+
type UrlOptions<IsRequired extends boolean = true> = {
|
|
86
|
+
min?: number;
|
|
87
|
+
max?: number;
|
|
88
|
+
includes?: string;
|
|
89
|
+
excludes?: string | string[];
|
|
90
|
+
protocols?: string[];
|
|
91
|
+
allowedDomains?: string[];
|
|
92
|
+
blockedDomains?: string[];
|
|
93
|
+
allowedPorts?: number[];
|
|
94
|
+
blockedPorts?: number[];
|
|
95
|
+
pathStartsWith?: string;
|
|
96
|
+
pathEndsWith?: string;
|
|
97
|
+
mustHaveQuery?: boolean;
|
|
98
|
+
mustNotHaveQuery?: boolean;
|
|
99
|
+
mustHaveFragment?: boolean;
|
|
100
|
+
mustNotHaveFragment?: boolean;
|
|
101
|
+
allowLocalhost?: boolean;
|
|
102
|
+
blockLocalhost?: boolean;
|
|
103
|
+
transform?: (value: string) => string;
|
|
104
|
+
defaultValue?: IsRequired extends true ? string : string | null;
|
|
105
|
+
i18n?: Partial<Record<Locale, Partial<UrlMessages>>>;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Type alias for URL validation schema based on required flag
|
|
109
|
+
*
|
|
110
|
+
* @template IsRequired - Whether the field is required
|
|
111
|
+
* @typedef UrlSchema
|
|
112
|
+
* @description Returns ZodString if required, ZodNullable<ZodString> if optional
|
|
113
|
+
*/
|
|
114
|
+
type UrlSchema<IsRequired extends boolean> = IsRequired extends true ? ZodString : ZodNullable<ZodString>;
|
|
115
|
+
/**
|
|
116
|
+
* Creates a Zod schema for URL validation with comprehensive constraints
|
|
117
|
+
*
|
|
118
|
+
* @template IsRequired - Whether the field is required (affects return type)
|
|
119
|
+
* @param {IsRequired} [required=false] - Whether the field is required
|
|
120
|
+
* @param {Omit<ValidatorOptions<IsRequired>, 'required'>} [options] - Configuration options for validation
|
|
121
|
+
* @returns {UrlSchema<IsRequired>} Zod schema for URL validation
|
|
122
|
+
*
|
|
123
|
+
* @description
|
|
124
|
+
* Creates a comprehensive URL validator with protocol filtering, domain control,
|
|
125
|
+
* port validation, path constraints, and localhost handling.
|
|
126
|
+
*
|
|
127
|
+
* Features:
|
|
128
|
+
* - RFC-compliant URL format validation
|
|
129
|
+
* - Protocol whitelist/blacklist (http, https, ftp, etc.)
|
|
130
|
+
* - Domain whitelist/blacklist with subdomain support
|
|
131
|
+
* - Port validation and filtering
|
|
132
|
+
* - Path prefix/suffix validation
|
|
133
|
+
* - Query parameter requirements
|
|
134
|
+
* - Fragment requirements
|
|
135
|
+
* - Localhost detection and control
|
|
136
|
+
* - Length validation
|
|
137
|
+
* - Content inclusion/exclusion
|
|
138
|
+
* - Custom transformation functions
|
|
139
|
+
* - Comprehensive internationalization
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* // Basic URL validation
|
|
144
|
+
* const basicSchema = url() // optional by default
|
|
145
|
+
* basicSchema.parse("https://example.com") // ✓ Valid
|
|
146
|
+
* basicSchema.parse(null) // ✓ Valid (optional)
|
|
147
|
+
*
|
|
148
|
+
* // Required validation
|
|
149
|
+
* const requiredSchema = parse("https://example.com") // ✓ Valid
|
|
150
|
+
(true)
|
|
151
|
+
* requiredSchema.parse(null) // ✗ Invalid (required)
|
|
152
|
+
*
|
|
153
|
+
*
|
|
154
|
+
* // HTTPS only
|
|
155
|
+
* const httpsSchema = url(false, { protocols: ["https"] })
|
|
156
|
+
* httpsSchema.parse("https://example.com") // ✓ Valid
|
|
157
|
+
* httpsSchema.parse("http://example.com") // ✗ Invalid
|
|
158
|
+
*
|
|
159
|
+
* // Domain restriction
|
|
160
|
+
* const domainSchema = url(false, {
|
|
161
|
+
* allowedDomains: ["company.com", "trusted.org"]
|
|
162
|
+
* })
|
|
163
|
+
* domainSchema.parse("https://app.company.com") // ✓ Valid (subdomain)
|
|
164
|
+
* domainSchema.parse("https://example.com") // ✗ Invalid
|
|
165
|
+
*
|
|
166
|
+
* // Block localhost
|
|
167
|
+
* const noLocalhostSchema = url(false, { blockLocalhost: true })
|
|
168
|
+
* noLocalhostSchema.parse("https://example.com") // ✓ Valid
|
|
169
|
+
* noLocalhostSchema.parse("http://localhost:3000") // ✗ Invalid
|
|
170
|
+
*
|
|
171
|
+
* // API endpoints with path requirements
|
|
172
|
+
* const apiSchema = url(false, {
|
|
173
|
+
* pathStartsWith: "/api/",
|
|
174
|
+
* mustHaveQuery: true
|
|
175
|
+
* })
|
|
176
|
+
* apiSchema.parse("https://api.com/api/users?page=1") // ✓ Valid
|
|
177
|
+
*
|
|
178
|
+
* // Port restrictions
|
|
179
|
+
* const portSchema = url(false, {
|
|
180
|
+
* allowedPorts: [80, 443, 8080]
|
|
181
|
+
* })
|
|
182
|
+
* portSchema.parse("https://example.com:443") // ✓ Valid
|
|
183
|
+
* portSchema.parse("https://example.com:3000") // ✗ Invalid
|
|
184
|
+
*
|
|
185
|
+
* // Optional with default
|
|
186
|
+
* const optionalSchema = url(false, {
|
|
187
|
+
* defaultValue: null
|
|
188
|
+
* })
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @throws {z.ZodError} When validation fails with specific error messages
|
|
192
|
+
* @see {@link UrlOptions} for all available configuration options
|
|
193
|
+
*/
|
|
194
|
+
declare function url<IsRequired extends boolean = false>(required?: IsRequired, options?: Omit<UrlOptions<IsRequired>, 'required'>): UrlSchema<IsRequired>;
|
|
195
|
+
|
|
196
|
+
export { type UrlMessages, type UrlOptions, type UrlSchema, url };
|