@jarenjs/formats 0.8.3 → 0.9.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/LICENSE +21 -0
- package/README.md +128 -2
- package/dist/types/datetime.d.ts +109 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/json.d.ts +71 -0
- package/dist/types/number.d.ts +144 -0
- package/dist/types/string.d.ts +379 -0
- package/dist/types/testers.d.ts +31 -0
- package/package.json +28 -8
- package/src/datetime.js +373 -0
- package/src/index.js +31 -0
- package/src/json.js +90 -0
- package/src/number.js +219 -0
- package/src/string.js +548 -0
- package/src/testers.js +216 -0
- package/dist/index.js +0 -679
- package/dist/index.js.map +0 -7
- package/dist/index.min.js +0 -2
- package/dist/index.min.js.map +0 -7
package/src/string.js
ADDED
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
// eslint-disable no-useless-escape
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
isStringType,
|
|
6
|
+
} from '@jarenjs/core';
|
|
7
|
+
|
|
8
|
+
// The name -> predicate bindings live in ONE place: testers.js. This
|
|
9
|
+
// module only wraps them in the validator's compiler contract.
|
|
10
|
+
import { stringFormatTesters } from './testers.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{format?: string, formatMinimum?: string, formatExclusiveMinimum?: string, formatMaximum?: string, formatExclusiveMaximum?: string}} JSONSchema
|
|
14
|
+
* @typedef {{
|
|
15
|
+
* options: {skipErrors: boolean},
|
|
16
|
+
* createErrorHandler: (expected: any, key: string, ...details: any[]) => (data: any, dataPath?: string) => boolean
|
|
17
|
+
* }} ValidationObject
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a string format compiler function.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} formatName - The name of the format (e.g., 'email', 'uri')
|
|
24
|
+
* @param {(value: string) => boolean} isFormatTest - The function to test if a string matches the format
|
|
25
|
+
* @returns {(schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean} A compiler function that creates format validators
|
|
26
|
+
* @example
|
|
27
|
+
* const compiler = createStringFormatCompiler('email', isValidEmail);
|
|
28
|
+
* const validator = compiler(schemaObj, { format: 'email' });
|
|
29
|
+
* validator('user@example.com'); // true
|
|
30
|
+
*/
|
|
31
|
+
export function createStringFormatCompiler(formatName, isFormatTest) {
|
|
32
|
+
return function compileStringFormat(schemaObj, jsonSchema) {
|
|
33
|
+
if (jsonSchema.format !== formatName)
|
|
34
|
+
throw new Error('Format is not equal to jsonSchema (should not happen!)');
|
|
35
|
+
|
|
36
|
+
// when skipErrors is true, we don't need to create error objects
|
|
37
|
+
if (schemaObj.options.skipErrors) {
|
|
38
|
+
return function validateStringFormatFast(data, dataPath) {
|
|
39
|
+
return isStringType(data)
|
|
40
|
+
? isFormatTest(data)
|
|
41
|
+
: true;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const addError = schemaObj.createErrorHandler(formatName, 'format', isFormatTest.constructor.name);
|
|
46
|
+
|
|
47
|
+
return function validateStringFormat(data, dataPath) {
|
|
48
|
+
return isStringType(data)
|
|
49
|
+
? isFormatTest(data) || addError(data, dataPath)
|
|
50
|
+
: true;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// =============================================================================
|
|
56
|
+
// Alphabetic & Case Format Compilers
|
|
57
|
+
// =============================================================================
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Compiles a validator for the 'alpha' format.
|
|
61
|
+
* Validates strings containing only alphabetic characters (a-z, A-Z).
|
|
62
|
+
*
|
|
63
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
64
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
65
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
66
|
+
* @example
|
|
67
|
+
* compileAlphaFormat(schemaObj, { format: 'alpha' })('HelloWorld'); // true
|
|
68
|
+
* compileAlphaFormat(schemaObj, { format: 'alpha' })('Hello123'); // false (with error)
|
|
69
|
+
*/
|
|
70
|
+
export const compileAlphaFormat = createStringFormatCompiler('alpha', stringFormatTesters['alpha']);
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Compiles a validator for the 'alphanumeric' format.
|
|
74
|
+
* Validates strings containing only alphabetic characters and digits (a-z, A-Z, 0-9).
|
|
75
|
+
*
|
|
76
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
77
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
78
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
79
|
+
* @example
|
|
80
|
+
* compileAlphaNumericFormat(schemaObj, { format: 'alphanumeric' })('Hello123'); // true
|
|
81
|
+
*/
|
|
82
|
+
export const compileAlphaNumericFormat = createStringFormatCompiler('alphanumeric', stringFormatTesters['alphanumeric']);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Compiles a validator for the 'uppercase' format.
|
|
86
|
+
* Validates strings containing only uppercase characters.
|
|
87
|
+
*
|
|
88
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
89
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
90
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
91
|
+
*/
|
|
92
|
+
export const compileUppercaseFormat = createStringFormatCompiler('uppercase', stringFormatTesters['uppercase']);
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Compiles a validator for the 'lowercase' format.
|
|
96
|
+
* Validates strings containing only lowercase characters.
|
|
97
|
+
*
|
|
98
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
99
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
100
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
101
|
+
*/
|
|
102
|
+
export const compileLowercaseFormat = createStringFormatCompiler('lowercase', stringFormatTesters['lowercase']);
|
|
103
|
+
|
|
104
|
+
// =============================================================================
|
|
105
|
+
// Identifier Format Compilers
|
|
106
|
+
// =============================================================================
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Compiles a validator for the 'identifier' format.
|
|
110
|
+
* Validates general identifier strings (variable names, etc.).
|
|
111
|
+
*
|
|
112
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
113
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
114
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
115
|
+
*/
|
|
116
|
+
export const compileIdentifierFormat = createStringFormatCompiler('identifier', stringFormatTesters['identifier']);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Compiles a validator for the 'html-identifier' format.
|
|
120
|
+
* Validates HTML element and attribute identifiers.
|
|
121
|
+
*
|
|
122
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
123
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
124
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
125
|
+
*/
|
|
126
|
+
export const compileHtmlIdentifierFormat = createStringFormatCompiler('html-identifier', stringFormatTesters['html-identifier']);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Compiles a validator for the 'css-identifier' format.
|
|
130
|
+
* Validates CSS class and ID selectors.
|
|
131
|
+
*
|
|
132
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
133
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
134
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
135
|
+
*/
|
|
136
|
+
export const compileCssIdentifierFormat = createStringFormatCompiler('css-identifier', stringFormatTesters['css-identifier']);
|
|
137
|
+
|
|
138
|
+
// =============================================================================
|
|
139
|
+
// Numeric & Color Format Compilers
|
|
140
|
+
// =============================================================================
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Compiles a validator for the 'hexadecimal' format.
|
|
144
|
+
* Validates hexadecimal number strings (0-9, a-f, A-F).
|
|
145
|
+
*
|
|
146
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
147
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
148
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
149
|
+
*/
|
|
150
|
+
export const compileHexadecimalFormat = createStringFormatCompiler('hexadecimal', stringFormatTesters['hexadecimal']);
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Compiles a validator for the 'numeric' format.
|
|
154
|
+
* Validates numeric strings containing only digits.
|
|
155
|
+
*
|
|
156
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
157
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
158
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
159
|
+
*/
|
|
160
|
+
export const compileNumericFormat = createStringFormatCompiler('numeric', stringFormatTesters['numeric']);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Compiles a validator for the 'color' format.
|
|
164
|
+
* Validates hexadecimal color codes (e.g., #FFF, #FFFFFF).
|
|
165
|
+
*
|
|
166
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
167
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
168
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
169
|
+
*/
|
|
170
|
+
export const compileColorFormat = createStringFormatCompiler('color', stringFormatTesters['color']);
|
|
171
|
+
|
|
172
|
+
// =============================================================================
|
|
173
|
+
// Regex Format Compiler
|
|
174
|
+
// =============================================================================
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Compiles a validator for the 'regex' format.
|
|
178
|
+
* Validates that a string is a valid regular expression pattern.
|
|
179
|
+
*
|
|
180
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
181
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
182
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
183
|
+
*/
|
|
184
|
+
export const compileRegexFormat = createStringFormatCompiler('regex', stringFormatTesters['regex']);
|
|
185
|
+
|
|
186
|
+
// =============================================================================
|
|
187
|
+
// URI Format Compilers
|
|
188
|
+
// =============================================================================
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Compiles a validator for the 'uri' format.
|
|
192
|
+
* Validates absolute URI strings per RFC 3986.
|
|
193
|
+
*
|
|
194
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
195
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
196
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
197
|
+
* @example
|
|
198
|
+
* compileUriFormat(schemaObj, { format: 'uri' })('https://example.com'); // true
|
|
199
|
+
*/
|
|
200
|
+
export const compileUriFormat = createStringFormatCompiler('uri', stringFormatTesters['uri']);
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Compiles a validator for the 'uri--full' format.
|
|
204
|
+
* Validates absolute URI strings with stricter checking.
|
|
205
|
+
*
|
|
206
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
207
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
208
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
209
|
+
*/
|
|
210
|
+
export const compileUriFullFormat = createStringFormatCompiler('uri--full', stringFormatTesters['uri--full']);
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Compiles a validator for the 'uri-reference' format.
|
|
214
|
+
* Validates URI reference strings (absolute or relative) per RFC 3986.
|
|
215
|
+
*
|
|
216
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
217
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
218
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
219
|
+
*/
|
|
220
|
+
export const compileUriReferenceFormat = createStringFormatCompiler('uri-reference', stringFormatTesters['uri-reference']);
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Compiles a validator for the 'uri-reference--full' format.
|
|
224
|
+
* Validates URI reference strings with stricter checking.
|
|
225
|
+
*
|
|
226
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
227
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
228
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
229
|
+
*/
|
|
230
|
+
export const compileUriReferenceFullFormat = createStringFormatCompiler('uri-reference--full', stringFormatTesters['uri-reference--full']);
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Compiles a validator for the 'uri-template' format.
|
|
234
|
+
* Validates URI template strings per RFC 6570.
|
|
235
|
+
*
|
|
236
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
237
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
238
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
239
|
+
*/
|
|
240
|
+
export const compileUriTemplateFormat = createStringFormatCompiler('uri-template', stringFormatTesters['uri-template']);
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Compiles a validator for the 'url' format.
|
|
244
|
+
* Validates URL strings per the WHATWG URL Standard.
|
|
245
|
+
*
|
|
246
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
247
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
248
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
249
|
+
*/
|
|
250
|
+
export const compileUrlFormat = createStringFormatCompiler('url', stringFormatTesters['url']);
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Compiles a validator for the 'url--full' format.
|
|
254
|
+
* Validates URL strings with stricter checking.
|
|
255
|
+
*
|
|
256
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
257
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
258
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
259
|
+
*/
|
|
260
|
+
export const compileUrlFullFormat = createStringFormatCompiler('url--full', stringFormatTesters['url--full']);
|
|
261
|
+
|
|
262
|
+
// =============================================================================
|
|
263
|
+
// IRI Format Compilers (Internationalized Resource Identifiers)
|
|
264
|
+
// =============================================================================
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Compiles a validator for the 'iri' format.
|
|
268
|
+
* Validates IRI strings per RFC 3987.
|
|
269
|
+
*
|
|
270
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
271
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
272
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
273
|
+
*/
|
|
274
|
+
export const compileIriFormat = createStringFormatCompiler('iri', stringFormatTesters['iri']);
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Compiles a validator for the 'iri-reference' format.
|
|
278
|
+
* Validates IRI reference strings (absolute or relative) per RFC 3987.
|
|
279
|
+
*
|
|
280
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
281
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
282
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
283
|
+
*/
|
|
284
|
+
export const compileIriReferenceFormat = createStringFormatCompiler('iri-reference', stringFormatTesters['iri-reference']);
|
|
285
|
+
|
|
286
|
+
// =============================================================================
|
|
287
|
+
// Email Format Compilers
|
|
288
|
+
// =============================================================================
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Compiles a validator for the 'email' format.
|
|
292
|
+
* Validates email address strings per RFC 5321.
|
|
293
|
+
*
|
|
294
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
295
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
296
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
297
|
+
* @example
|
|
298
|
+
* compileEmailFormat(schemaObj, { format: 'email' })('user@example.com'); // true
|
|
299
|
+
*/
|
|
300
|
+
export const compileEmailFormat = createStringFormatCompiler('email', stringFormatTesters['email']);
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Compiles a validator for the 'email--full' format.
|
|
304
|
+
* Validates email address strings with stricter checking.
|
|
305
|
+
*
|
|
306
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
307
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
308
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
309
|
+
*/
|
|
310
|
+
export const compileEmailFullFormat = createStringFormatCompiler('email--full', stringFormatTesters['email--full']);
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Compiles a validator for the 'idn-email' format.
|
|
314
|
+
* Validates internationalized email addresses (EAI) per RFC 6531.
|
|
315
|
+
*
|
|
316
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
317
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
318
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
319
|
+
*/
|
|
320
|
+
export const compileIdnEmailFormat = createStringFormatCompiler('idn-email', stringFormatTesters['idn-email']);
|
|
321
|
+
|
|
322
|
+
// =============================================================================
|
|
323
|
+
// Hostname Format Compilers
|
|
324
|
+
// =============================================================================
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Compiles a validator for the 'hostname' format.
|
|
328
|
+
* Validates hostname strings per RFC 1123.
|
|
329
|
+
*
|
|
330
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
331
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
332
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
333
|
+
*/
|
|
334
|
+
export const compileHostnameFormat = createStringFormatCompiler('hostname', stringFormatTesters['hostname']);
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Compiles a validator for the 'idn-hostname' format.
|
|
338
|
+
* Validates internationalized domain names (IDN) per RFC 5890.
|
|
339
|
+
*
|
|
340
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
341
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
342
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
343
|
+
*/
|
|
344
|
+
export const compileIdnHostnameFormat = createStringFormatCompiler('idn-hostname', stringFormatTesters['idn-hostname']);
|
|
345
|
+
|
|
346
|
+
// =============================================================================
|
|
347
|
+
// IP Address Format Compilers
|
|
348
|
+
// =============================================================================
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Compiles a validator for the 'ipv4' format.
|
|
352
|
+
* Validates IPv4 address strings.
|
|
353
|
+
*
|
|
354
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
355
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
356
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
357
|
+
* @example
|
|
358
|
+
* compileIpv4Format(schemaObj, { format: 'ipv4' })('192.168.1.1'); // true
|
|
359
|
+
*/
|
|
360
|
+
export const compileIpv4Format = createStringFormatCompiler('ipv4', stringFormatTesters['ipv4']);
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Compiles a validator for the 'ipv6' format.
|
|
364
|
+
* Validates IPv6 address strings.
|
|
365
|
+
*
|
|
366
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
367
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
368
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
369
|
+
*/
|
|
370
|
+
export const compileIpv6Format = createStringFormatCompiler('ipv6', stringFormatTesters['ipv6']);
|
|
371
|
+
|
|
372
|
+
// =============================================================================
|
|
373
|
+
// UUID & GUID Format Compilers
|
|
374
|
+
// =============================================================================
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Compiles a validator for the 'uuid' format.
|
|
378
|
+
* Validates UUID strings per RFC 4122.
|
|
379
|
+
*
|
|
380
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
381
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
382
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
383
|
+
* @example
|
|
384
|
+
* compileUuidFormat(schemaObj, { format: 'uuid' })('550e8400-e29b-41d4-a716-446655440000'); // true
|
|
385
|
+
*/
|
|
386
|
+
export const compileUuidFormat = createStringFormatCompiler('uuid', stringFormatTesters['uuid']);
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Compiles a validator for the 'guid' format.
|
|
390
|
+
* Validates GUID strings (Microsoft format).
|
|
391
|
+
*
|
|
392
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
393
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
394
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
395
|
+
*/
|
|
396
|
+
export const compileGuidFormat = createStringFormatCompiler('guid', stringFormatTesters['guid']);
|
|
397
|
+
|
|
398
|
+
// =============================================================================
|
|
399
|
+
// ISBN Format Compilers
|
|
400
|
+
// =============================================================================
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Compiles a validator for the 'isbn10' format.
|
|
404
|
+
* Validates ISBN-10 identifier strings.
|
|
405
|
+
*
|
|
406
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
407
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
408
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
409
|
+
*/
|
|
410
|
+
export const compileIsbn10Format = createStringFormatCompiler('isbn10', stringFormatTesters['isbn10']);
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Compiles a validator for the 'isbn13' format.
|
|
414
|
+
* Validates ISBN-13 identifier strings.
|
|
415
|
+
*
|
|
416
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
417
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
418
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
419
|
+
*/
|
|
420
|
+
export const compileIsbn13Format = createStringFormatCompiler('isbn13', stringFormatTesters['isbn13']);
|
|
421
|
+
|
|
422
|
+
// =============================================================================
|
|
423
|
+
// Hardware Address Format Compilers
|
|
424
|
+
// =============================================================================
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Compiles a validator for the 'mac' format.
|
|
428
|
+
* Validates MAC address strings.
|
|
429
|
+
*
|
|
430
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
431
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
432
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
433
|
+
*/
|
|
434
|
+
export const compileMacFormat = createStringFormatCompiler('mac', stringFormatTesters['mac']);
|
|
435
|
+
|
|
436
|
+
// =============================================================================
|
|
437
|
+
// Encoding Format Compilers
|
|
438
|
+
// =============================================================================
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Compiles a validator for the 'base64' format.
|
|
442
|
+
* Validates Base64 encoded strings.
|
|
443
|
+
*
|
|
444
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
445
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
446
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
447
|
+
*/
|
|
448
|
+
export const compileBase64Format = createStringFormatCompiler('base64', stringFormatTesters['base64']);
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Compiles a validator for the 'byte' format.
|
|
452
|
+
* Alias for 'base64' format.
|
|
453
|
+
*
|
|
454
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
455
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
456
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
457
|
+
*/
|
|
458
|
+
export const compileByteFormat = createStringFormatCompiler('byte', stringFormatTesters['byte']);
|
|
459
|
+
|
|
460
|
+
// =============================================================================
|
|
461
|
+
// Country & Banking Format Compilers
|
|
462
|
+
// =============================================================================
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Compiles a validator for the 'country2' format.
|
|
466
|
+
* Validates ISO 3166-1 alpha-2 country codes.
|
|
467
|
+
*
|
|
468
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
469
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
470
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
471
|
+
* @example
|
|
472
|
+
* compileCountry2Format(schemaObj, { format: 'country2' })('US'); // true
|
|
473
|
+
* compileCountry2Format(schemaObj, { format: 'country2' })('XX'); // false (with error)
|
|
474
|
+
*/
|
|
475
|
+
export const compileCountry2Format = createStringFormatCompiler('country2', stringFormatTesters['country2']);
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Compiles a validator for the 'iban' format.
|
|
479
|
+
* Validates IBAN (International Bank Account Number) strings.
|
|
480
|
+
*
|
|
481
|
+
* @param {ValidationObject} schemaObj - The validation JSONSchema for error handling and options
|
|
482
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
483
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
484
|
+
*/
|
|
485
|
+
export const compileIbanFormat = createStringFormatCompiler('iban', stringFormatTesters['iban']);
|
|
486
|
+
|
|
487
|
+
// =============================================================================
|
|
488
|
+
// Aggregated Format Validators Object (Backward Compatibility)
|
|
489
|
+
// =============================================================================
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Object mapping format names to their compiler functions.
|
|
493
|
+
* Used for backward compatibility and aggregate imports.
|
|
494
|
+
*
|
|
495
|
+
* @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
|
|
496
|
+
*/
|
|
497
|
+
export const formatValidators = {
|
|
498
|
+
// Alphabetic & Case
|
|
499
|
+
'alpha': compileAlphaFormat,
|
|
500
|
+
'alphanumeric': compileAlphaNumericFormat,
|
|
501
|
+
'uppercase': compileUppercaseFormat,
|
|
502
|
+
'lowercase': compileLowercaseFormat,
|
|
503
|
+
// Identifiers
|
|
504
|
+
'identifier': compileIdentifierFormat,
|
|
505
|
+
'html-identifier': compileHtmlIdentifierFormat,
|
|
506
|
+
'css-identifier': compileCssIdentifierFormat,
|
|
507
|
+
// Numeric & Color
|
|
508
|
+
'hexadecimal': compileHexadecimalFormat,
|
|
509
|
+
'numeric': compileNumericFormat,
|
|
510
|
+
'color': compileColorFormat,
|
|
511
|
+
// Regex
|
|
512
|
+
'regex': compileRegexFormat,
|
|
513
|
+
// URI
|
|
514
|
+
'uri': compileUriFormat,
|
|
515
|
+
'uri--full': compileUriFullFormat,
|
|
516
|
+
'uri-reference': compileUriReferenceFormat,
|
|
517
|
+
'uri-reference--full': compileUriReferenceFullFormat,
|
|
518
|
+
'uri-template': compileUriTemplateFormat,
|
|
519
|
+
'url': compileUrlFormat,
|
|
520
|
+
'url--full': compileUrlFullFormat,
|
|
521
|
+
// IRI
|
|
522
|
+
'iri': compileIriFormat,
|
|
523
|
+
'iri-reference': compileIriReferenceFormat,
|
|
524
|
+
// Email
|
|
525
|
+
'email': compileEmailFormat,
|
|
526
|
+
'email--full': compileEmailFullFormat,
|
|
527
|
+
'idn-email': compileIdnEmailFormat,
|
|
528
|
+
// Hostname
|
|
529
|
+
'hostname': compileHostnameFormat,
|
|
530
|
+
'idn-hostname': compileIdnHostnameFormat,
|
|
531
|
+
// IP Address
|
|
532
|
+
'ipv4': compileIpv4Format,
|
|
533
|
+
'ipv6': compileIpv6Format,
|
|
534
|
+
// UUID & GUID
|
|
535
|
+
'uuid': compileUuidFormat,
|
|
536
|
+
'guid': compileGuidFormat,
|
|
537
|
+
// ISBN
|
|
538
|
+
'isbn10': compileIsbn10Format,
|
|
539
|
+
'isbn13': compileIsbn13Format,
|
|
540
|
+
// Hardware Address
|
|
541
|
+
'mac': compileMacFormat,
|
|
542
|
+
// Encoding
|
|
543
|
+
'base64': compileBase64Format,
|
|
544
|
+
'byte': compileByteFormat,
|
|
545
|
+
// Country & Banking
|
|
546
|
+
'country2': compileCountry2Format,
|
|
547
|
+
'iban': compileIbanFormat,
|
|
548
|
+
};
|