@jarenjs/formats 0.8.4 → 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/number.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
|
|
3
|
+
// The name -> predicate bindings live in ONE place: testers.js. This
|
|
4
|
+
// module only wraps them in the validator's compiler contract.
|
|
5
|
+
import { numberFormatTesters } from './testers.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {{format?: string, formatMinimum?: string, formatExclusiveMinimum?: string, formatMaximum?: string, formatExclusiveMaximum?: string}} JSONSchema
|
|
9
|
+
* @typedef {{
|
|
10
|
+
* options: {skipErrors: boolean},
|
|
11
|
+
* createErrorHandler: (expected: any, key: string, ...details: any[]) => (data: any, dataPath?: string) => boolean
|
|
12
|
+
* }} ValidationObject
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Creates a number format compiler function.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} formatName - The name of the format (e.g., 'int32', 'float64')
|
|
19
|
+
* @param {(value: number) => boolean} isNumberTest - The function to test if a number matches the format constraints
|
|
20
|
+
* @returns {(schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean} A compiler function that creates number format validators
|
|
21
|
+
* @example
|
|
22
|
+
* const compiler = createNumberFormatCompiler('int8', isValidInt8);
|
|
23
|
+
* const validator = compiler(schemaObj, { format: 'int8' });
|
|
24
|
+
* validator(127); // true
|
|
25
|
+
* validator('127'); // true (coerced to number)
|
|
26
|
+
* validator(128); // false (out of int8 range)
|
|
27
|
+
*/
|
|
28
|
+
function createNumberFormatCompiler(formatName, isNumberTest) {
|
|
29
|
+
return function compileNumberFormat(schemaObj, jsonSchema) {
|
|
30
|
+
if (jsonSchema.format !== formatName)
|
|
31
|
+
throw new Error('Format is not equal to jsonSchema (should not happen!)');
|
|
32
|
+
|
|
33
|
+
const addError = schemaObj.createErrorHandler(formatName, 'format');
|
|
34
|
+
|
|
35
|
+
return function validateNumberFormat(data, dataPath) {
|
|
36
|
+
return (data == null
|
|
37
|
+
|| isNumberTest(Number(data))
|
|
38
|
+
|| addError(data, dataPath));
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// =============================================================================
|
|
44
|
+
// Signed Integer Format Compilers
|
|
45
|
+
// =============================================================================
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Compiles a validator for the 'int8' format.
|
|
49
|
+
* Validates 8-bit signed integers (-128 to 127).
|
|
50
|
+
*
|
|
51
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
52
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
53
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
54
|
+
* @example
|
|
55
|
+
* compileInt8Format(schemaObj, { format: 'int8' })(127); // true
|
|
56
|
+
* compileInt8Format(schemaObj, { format: 'int8' })(128); // false (with error)
|
|
57
|
+
* compileInt8Format(schemaObj, { format: 'int8' })('64'); // true (coerced)
|
|
58
|
+
*/
|
|
59
|
+
export const compileInt8Format = createNumberFormatCompiler('int8', numberFormatTesters['int8']);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Compiles a validator for the 'int16' format.
|
|
63
|
+
* Validates 16-bit signed integers (-32768 to 32767).
|
|
64
|
+
*
|
|
65
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
66
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
67
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
68
|
+
*/
|
|
69
|
+
export const compileInt16Format = createNumberFormatCompiler('int16', numberFormatTesters['int16']);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Compiles a validator for the 'int32' format.
|
|
73
|
+
* Validates 32-bit signed integers (-2147483648 to 2147483647).
|
|
74
|
+
*
|
|
75
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
76
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
77
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
78
|
+
*/
|
|
79
|
+
export const compileInt32Format = createNumberFormatCompiler('int32', numberFormatTesters['int32']);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Compiles a validator for the 'int64' format.
|
|
83
|
+
* Validates 64-bit signed integers (approximate range in JavaScript).
|
|
84
|
+
*
|
|
85
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
86
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
87
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
88
|
+
*/
|
|
89
|
+
export const compileInt64Format = createNumberFormatCompiler('int64', numberFormatTesters['int64']);
|
|
90
|
+
|
|
91
|
+
// =============================================================================
|
|
92
|
+
// Unsigned Integer Format Compilers
|
|
93
|
+
// =============================================================================
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Compiles a validator for the 'uint8' format.
|
|
97
|
+
* Validates 8-bit unsigned integers (0 to 255).
|
|
98
|
+
*
|
|
99
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
100
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
101
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
102
|
+
* @example
|
|
103
|
+
* compileUInt8Format(schemaObj, { format: 'uint8' })(255); // true
|
|
104
|
+
* compileUInt8Format(schemaObj, { format: 'uint8' })(256); // false (with error)
|
|
105
|
+
*/
|
|
106
|
+
export const compileUInt8Format = createNumberFormatCompiler('uint8', numberFormatTesters['uint8']);
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Compiles a validator for the 'uint16' format.
|
|
110
|
+
* Validates 16-bit unsigned integers (0 to 65535).
|
|
111
|
+
*
|
|
112
|
+
* @param {ValidationObject} schemaObj - The validation object 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 compileUInt16Format = createNumberFormatCompiler('uint16', numberFormatTesters['uint16']);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Compiles a validator for the 'uint32' format.
|
|
120
|
+
* Validates 32-bit unsigned integers (0 to 4294967295).
|
|
121
|
+
*
|
|
122
|
+
* @param {ValidationObject} schemaObj - The validation object 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 compileUInt32Format = createNumberFormatCompiler('uint32', numberFormatTesters['uint32']);
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Compiles a validator for the 'uint64' format.
|
|
130
|
+
* Validates 64-bit unsigned integers (approximate range in JavaScript).
|
|
131
|
+
*
|
|
132
|
+
* @param {ValidationObject} schemaObj - The validation object 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 compileUInt64Format = createNumberFormatCompiler('uint64', numberFormatTesters['uint64']);
|
|
137
|
+
|
|
138
|
+
// =============================================================================
|
|
139
|
+
// Floating Point Format Compilers
|
|
140
|
+
// =============================================================================
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Compiles a validator for the 'float16' format.
|
|
144
|
+
* Validates 16-bit floating point numbers (IEEE 754 half-precision).
|
|
145
|
+
*
|
|
146
|
+
* @param {ValidationObject} schemaObj - The validation object 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 compileFloat16Format = createNumberFormatCompiler('float16', numberFormatTesters['float16']);
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Compiles a validator for the 'float32' format.
|
|
154
|
+
* Validates 32-bit floating point numbers (IEEE 754 single-precision).
|
|
155
|
+
*
|
|
156
|
+
* @param {ValidationObject} schemaObj - The validation object 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 compileFloat32Format = createNumberFormatCompiler('float32', numberFormatTesters['float32']);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Compiles a validator for the 'float64' format.
|
|
164
|
+
* Validates 64-bit floating point numbers (IEEE 754 double-precision).
|
|
165
|
+
*
|
|
166
|
+
* @param {ValidationObject} schemaObj - The validation object 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 compileFloat64Format = createNumberFormatCompiler('float64', numberFormatTesters['float64']);
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Compiles a validator for the 'float' format.
|
|
174
|
+
* Alias for 'float32' - validates 32-bit floating point numbers.
|
|
175
|
+
*
|
|
176
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
177
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
178
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
179
|
+
*/
|
|
180
|
+
export const compileFloatFormat = createNumberFormatCompiler('float', numberFormatTesters['float']);
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compiles a validator for the 'double' format.
|
|
184
|
+
* Alias for 'float64' - validates 64-bit floating point numbers.
|
|
185
|
+
*
|
|
186
|
+
* @param {ValidationObject} schemaObj - The validation object for error handling and options
|
|
187
|
+
* @param {JSONSchema} jsonSchema - The JSON schema containing the format definition
|
|
188
|
+
* @returns {(data: unknown, dataPath?: string) => boolean} A validator function
|
|
189
|
+
*/
|
|
190
|
+
export const compileDoubleFormat = createNumberFormatCompiler('double', numberFormatTesters['double']);
|
|
191
|
+
|
|
192
|
+
// =============================================================================
|
|
193
|
+
// Aggregated Format Validators Object (Backward Compatibility)
|
|
194
|
+
// =============================================================================
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Object mapping number format names to their compiler functions.
|
|
198
|
+
* Used for backward compatibility and aggregate imports.
|
|
199
|
+
*
|
|
200
|
+
* @type {Record<string, (schemaObj: ValidationObject, jsonSchema: JSONSchema) => (data: unknown, dataPath?: string) => boolean>}
|
|
201
|
+
*/
|
|
202
|
+
export const formatValidators = {
|
|
203
|
+
// Signed integer types
|
|
204
|
+
int8: compileInt8Format,
|
|
205
|
+
int16: compileInt16Format,
|
|
206
|
+
int32: compileInt32Format,
|
|
207
|
+
int64: compileInt64Format,
|
|
208
|
+
// Unsigned integer types
|
|
209
|
+
uint8: compileUInt8Format,
|
|
210
|
+
uint16: compileUInt16Format,
|
|
211
|
+
uint32: compileUInt32Format,
|
|
212
|
+
uint64: compileUInt64Format,
|
|
213
|
+
// Floating point types
|
|
214
|
+
float16: compileFloat16Format,
|
|
215
|
+
float32: compileFloat32Format,
|
|
216
|
+
float64: compileFloat64Format,
|
|
217
|
+
float: compileFloatFormat,
|
|
218
|
+
double: compileDoubleFormat,
|
|
219
|
+
};
|