z-schema 12.2.0 → 12.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/z-schema +1 -1
- package/cjs/{index.js → index.cjs} +838 -707
- package/cjs/{index.d.ts → index.d.cts} +47 -26
- package/dist/{errors.d.mts → errors.d.ts} +2 -2
- package/dist/{errors.mjs → errors.js} +1 -2
- package/dist/{format-validators.mjs → format-validators.js} +60 -43
- package/dist/{index.d.mts → index.d.ts} +9 -9
- package/dist/{index.mjs → index.js} +3 -3
- package/dist/{json-schema-versions.d.mts → json-schema-versions.d.ts} +34 -3
- package/dist/{json-schema.d.mts → json-schema.d.ts} +7 -7
- package/dist/{json-schema.mjs → json-schema.js} +6 -5
- package/dist/{json-validation.mjs → json-validation.js} +144 -127
- package/dist/{report.d.mts → report.d.ts} +8 -9
- package/dist/{report.mjs → report.js} +28 -31
- package/dist/{schema-cache.d.mts → schema-cache.d.ts} +5 -5
- package/dist/{schema-cache.mjs → schema-cache.js} +10 -11
- package/dist/{schema-compiler.d.mts → schema-compiler.d.ts} +4 -4
- package/dist/{schema-compiler.mjs → schema-compiler.js} +101 -79
- package/dist/{schema-validator.d.mts → schema-validator.d.ts} +5 -5
- package/dist/{schema-validator.mjs → schema-validator.js} +138 -166
- package/dist/utils/{array.mjs → array.js} +4 -3
- package/dist/utils/{base64.mjs → base64.js} +3 -2
- package/dist/utils/{clone.mjs → clone.js} +18 -20
- package/dist/utils/hostname.js +176 -0
- package/dist/utils/{json.mjs → json.js} +13 -8
- package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
- package/dist/utils/{time.mjs → time.js} +8 -8
- package/dist/utils/unicode.js +22 -0
- package/dist/utils/{what-is.mjs → what-is.js} +1 -2
- package/dist/validation/{array.mjs → array.js} +18 -20
- package/dist/validation/{combinators.mjs → combinators.js} +16 -16
- package/dist/validation/{numeric.mjs → numeric.js} +11 -11
- package/dist/validation/{object.mjs → object.js} +35 -34
- package/dist/validation/{ref.mjs → ref.js} +5 -5
- package/dist/validation/{shared.mjs → shared.js} +15 -12
- package/dist/validation/{string.mjs → string.js} +35 -35
- package/dist/validation/type.js +34 -0
- package/dist/{z-schema-base.d.mts → z-schema-base.d.ts} +11 -12
- package/dist/{z-schema-base.mjs → z-schema-base.js} +45 -40
- package/dist/{z-schema-options.d.mts → z-schema-options.d.ts} +3 -3
- package/dist/{z-schema-options.mjs → z-schema-options.js} +5 -5
- package/dist/{z-schema-reader.d.mts → z-schema-reader.d.ts} +1 -1
- package/dist/{z-schema-versions.mjs → z-schema-versions.js} +21 -21
- package/dist/{z-schema.d.mts → z-schema.d.ts} +5 -13
- package/dist/{z-schema.mjs → z-schema.js} +38 -48
- package/package.json +24 -25
- package/src/errors.ts +1 -2
- package/src/format-validators.ts +173 -67
- package/src/json-schema-versions.ts +56 -2
- package/src/json-schema.ts +10 -9
- package/src/json-validation.ts +189 -146
- package/src/report.ts +37 -49
- package/src/schema-cache.ts +13 -13
- package/src/schema-compiler.ts +170 -117
- package/src/schema-validator.ts +239 -238
- package/src/utils/array.ts +9 -6
- package/src/utils/base64.ts +13 -2
- package/src/utils/clone.ts +28 -30
- package/src/utils/date.ts +6 -3
- package/src/utils/hostname.ts +257 -31
- package/src/utils/json.ts +16 -9
- package/src/utils/properties.ts +2 -2
- package/src/utils/schema-regex.ts +4 -4
- package/src/utils/time.ts +8 -7
- package/src/utils/unicode.ts +12 -5
- package/src/utils/what-is.ts +1 -5
- package/src/validation/array.ts +24 -22
- package/src/validation/combinators.ts +14 -14
- package/src/validation/numeric.ts +14 -28
- package/src/validation/object.ts +32 -36
- package/src/validation/ref.ts +5 -6
- package/src/validation/shared.ts +22 -21
- package/src/validation/string.ts +36 -42
- package/src/validation/type.ts +17 -17
- package/src/z-schema-base.ts +49 -38
- package/src/z-schema-options.ts +4 -3
- package/src/z-schema.ts +35 -45
- package/umd/ZSchema.js +853 -715
- package/umd/ZSchema.min.js +2 -2
- package/umd/package.json +3 -0
- package/dist/utils/hostname.mjs +0 -75
- package/dist/utils/unicode.mjs +0 -12
- package/dist/validation/type.mjs +0 -32
- /package/dist/{format-validators.d.mts → format-validators.d.ts} +0 -0
- /package/dist/{json-schema-versions.mjs → json-schema-versions.js} +0 -0
- /package/dist/schemas/{draft-04-schema.mjs → draft-04-schema.js} +0 -0
- /package/dist/schemas/{draft-06-schema.mjs → draft-06-schema.js} +0 -0
- /package/dist/schemas/{draft-07-schema.mjs → draft-07-schema.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-applicator.mjs → draft-2019-09-meta-applicator.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-content.mjs → draft-2019-09-meta-content.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-core.mjs → draft-2019-09-meta-core.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-format.mjs → draft-2019-09-meta-format.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-meta-data.mjs → draft-2019-09-meta-meta-data.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-validation.mjs → draft-2019-09-meta-validation.js} +0 -0
- /package/dist/schemas/{draft-2019-09-schema.mjs → draft-2019-09-schema.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-applicator.mjs → draft-2020-12-meta-applicator.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-content.mjs → draft-2020-12-meta-content.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-core.mjs → draft-2020-12-meta-core.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-format-annotation.mjs → draft-2020-12-meta-format-annotation.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-format-assertion.mjs → draft-2020-12-meta-format-assertion.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-meta-data.mjs → draft-2020-12-meta-meta-data.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-unevaluated.mjs → draft-2020-12-meta-unevaluated.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-validation.mjs → draft-2020-12-meta-validation.js} +0 -0
- /package/dist/schemas/{draft-2020-12-schema.mjs → draft-2020-12-schema.js} +0 -0
- /package/dist/utils/{constants.mjs → constants.js} +0 -0
- /package/dist/utils/{date.mjs → date.js} +0 -0
- /package/dist/utils/{properties.mjs → properties.js} +0 -0
- /package/dist/utils/{symbols.mjs → symbols.js} +0 -0
- /package/dist/utils/{uri.mjs → uri.js} +0 -0
- /package/dist/{z-schema-reader.mjs → z-schema-reader.js} +0 -0
package/src/z-schema.ts
CHANGED
|
@@ -15,11 +15,6 @@ import { defaultOptions } from './z-schema-options.js';
|
|
|
15
15
|
import { getSchemaReader, setSchemaReader } from './z-schema-reader.js';
|
|
16
16
|
|
|
17
17
|
export class ZSchema extends ZSchemaBase {
|
|
18
|
-
/** @internal Use ZSchema.create() instead. */
|
|
19
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol) {
|
|
20
|
-
super(options, token);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
18
|
// ----- static methods start -----
|
|
24
19
|
|
|
25
20
|
// class scoped format functions
|
|
@@ -30,7 +25,7 @@ export class ZSchema extends ZSchemaBase {
|
|
|
30
25
|
* @param validatorFunction - A sync or async function `(value: unknown) => boolean | Promise<boolean>`.
|
|
31
26
|
*/
|
|
32
27
|
public static registerFormat(name: string, validatorFunction: FormatValidatorFn): void {
|
|
33
|
-
|
|
28
|
+
registerFormat(name, validatorFunction);
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
/**
|
|
@@ -38,7 +33,7 @@ export class ZSchema extends ZSchemaBase {
|
|
|
38
33
|
* @param name - The format name to unregister.
|
|
39
34
|
*/
|
|
40
35
|
public static unregisterFormat(name: string): void {
|
|
41
|
-
|
|
36
|
+
unregisterFormat(name);
|
|
42
37
|
}
|
|
43
38
|
|
|
44
39
|
/** Returns the names of all globally registered format validators. */
|
|
@@ -72,7 +67,7 @@ export class ZSchema extends ZSchemaBase {
|
|
|
72
67
|
* @param schemaReader - A function `(uri: string) => JsonSchema | undefined`, or `undefined` to clear.
|
|
73
68
|
*/
|
|
74
69
|
public static setSchemaReader(schemaReader: SchemaReader | undefined) {
|
|
75
|
-
|
|
70
|
+
setSchemaReader(schemaReader);
|
|
76
71
|
}
|
|
77
72
|
|
|
78
73
|
public static schemaSymbol = schemaSymbol;
|
|
@@ -148,8 +143,8 @@ export class ZSchema extends ZSchemaBase {
|
|
|
148
143
|
try {
|
|
149
144
|
this._validate(json, schema, options ?? {});
|
|
150
145
|
return { valid: true };
|
|
151
|
-
} catch (
|
|
152
|
-
return { valid: false, err:
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return { valid: false, err: error as ValidateError };
|
|
153
148
|
}
|
|
154
149
|
}
|
|
155
150
|
|
|
@@ -164,11 +159,15 @@ export class ZSchema extends ZSchemaBase {
|
|
|
164
159
|
validateAsync(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<true> {
|
|
165
160
|
return new Promise((resolve, reject) => {
|
|
166
161
|
try {
|
|
167
|
-
this._validate(json, schema, options || {}, (err, valid) =>
|
|
168
|
-
err || valid
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
162
|
+
this._validate(json, schema, options || {}, (err, valid) => {
|
|
163
|
+
if (err || !valid) {
|
|
164
|
+
reject(err ?? new Error('Validation failed'));
|
|
165
|
+
} else {
|
|
166
|
+
resolve(valid);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
} catch (error) {
|
|
170
|
+
reject(error instanceof Error ? error : new Error(String(error), { cause: error }));
|
|
172
171
|
}
|
|
173
172
|
});
|
|
174
173
|
}
|
|
@@ -187,8 +186,8 @@ export class ZSchema extends ZSchemaBase {
|
|
|
187
186
|
this._validate(json, schema, options || {}, (err, valid) => {
|
|
188
187
|
resolve({ valid, err });
|
|
189
188
|
});
|
|
190
|
-
} catch (
|
|
191
|
-
resolve({ valid: false, err:
|
|
189
|
+
} catch (error) {
|
|
190
|
+
resolve({ valid: false, err: error as ValidateError });
|
|
192
191
|
}
|
|
193
192
|
});
|
|
194
193
|
}
|
|
@@ -212,8 +211,8 @@ export class ZSchema extends ZSchemaBase {
|
|
|
212
211
|
try {
|
|
213
212
|
this._validateSchema(schemaOrArr);
|
|
214
213
|
return { valid: true };
|
|
215
|
-
} catch (
|
|
216
|
-
return { valid: false, err:
|
|
214
|
+
} catch (error) {
|
|
215
|
+
return { valid: false, err: error as ValidateError };
|
|
217
216
|
}
|
|
218
217
|
}
|
|
219
218
|
}
|
|
@@ -223,11 +222,6 @@ export class ZSchema extends ZSchemaBase {
|
|
|
223
222
|
* Created via `ZSchema.create({ safe: true })`.
|
|
224
223
|
*/
|
|
225
224
|
export class ZSchemaSafe extends ZSchemaBase {
|
|
226
|
-
/** @internal Use ZSchema.create() instead. */
|
|
227
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol) {
|
|
228
|
-
super(options, token);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
225
|
/**
|
|
232
226
|
* Validate JSON data against a schema.
|
|
233
227
|
* @param json - The data to validate.
|
|
@@ -239,8 +233,8 @@ export class ZSchemaSafe extends ZSchemaBase {
|
|
|
239
233
|
try {
|
|
240
234
|
this._validate(json, schema, options);
|
|
241
235
|
return { valid: true };
|
|
242
|
-
} catch (
|
|
243
|
-
return { valid: false, err:
|
|
236
|
+
} catch (error) {
|
|
237
|
+
return { valid: false, err: error as ValidateError };
|
|
244
238
|
}
|
|
245
239
|
}
|
|
246
240
|
|
|
@@ -253,8 +247,8 @@ export class ZSchemaSafe extends ZSchemaBase {
|
|
|
253
247
|
try {
|
|
254
248
|
this._validateSchema(schemaOrArr);
|
|
255
249
|
return { valid: true };
|
|
256
|
-
} catch (
|
|
257
|
-
return { valid: false, err:
|
|
250
|
+
} catch (error) {
|
|
251
|
+
return { valid: false, err: error as ValidateError };
|
|
258
252
|
}
|
|
259
253
|
}
|
|
260
254
|
}
|
|
@@ -264,11 +258,6 @@ export class ZSchemaSafe extends ZSchemaBase {
|
|
|
264
258
|
* Created via `ZSchema.create({ async: true })`.
|
|
265
259
|
*/
|
|
266
260
|
export class ZSchemaAsync extends ZSchemaBase {
|
|
267
|
-
/** @internal Use ZSchema.create() instead. */
|
|
268
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol) {
|
|
269
|
-
super(options, token);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
261
|
/**
|
|
273
262
|
* Validate JSON data against a schema asynchronously.
|
|
274
263
|
* @param json - The data to validate.
|
|
@@ -280,9 +269,15 @@ export class ZSchemaAsync extends ZSchemaBase {
|
|
|
280
269
|
validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions = {}): Promise<true> {
|
|
281
270
|
return new Promise((resolve, reject) => {
|
|
282
271
|
try {
|
|
283
|
-
this._validate(json, schema, options, (err, valid) =>
|
|
284
|
-
|
|
285
|
-
|
|
272
|
+
this._validate(json, schema, options, (err, valid) => {
|
|
273
|
+
if (err || !valid) {
|
|
274
|
+
reject(err ?? new Error('Validation failed'));
|
|
275
|
+
} else {
|
|
276
|
+
resolve(valid);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
} catch (error) {
|
|
280
|
+
reject(error instanceof Error ? error : new Error(String(error), { cause: error }));
|
|
286
281
|
}
|
|
287
282
|
});
|
|
288
283
|
}
|
|
@@ -303,11 +298,6 @@ export class ZSchemaAsync extends ZSchemaBase {
|
|
|
303
298
|
* Created via `ZSchema.create({ async: true, safe: true })`.
|
|
304
299
|
*/
|
|
305
300
|
export class ZSchemaAsyncSafe extends ZSchemaBase {
|
|
306
|
-
/** @internal Use ZSchema.create() instead. */
|
|
307
|
-
constructor(options: ZSchemaOptions | undefined, token: symbol) {
|
|
308
|
-
super(options, token);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
301
|
/**
|
|
312
302
|
* Validate JSON data against a schema asynchronously.
|
|
313
303
|
* The promise always resolves (never rejects).
|
|
@@ -322,8 +312,8 @@ export class ZSchemaAsyncSafe extends ZSchemaBase {
|
|
|
322
312
|
this._validate(json, schema, options, (err, valid) => {
|
|
323
313
|
resolve({ valid, err });
|
|
324
314
|
});
|
|
325
|
-
} catch (
|
|
326
|
-
resolve({ valid: false, err:
|
|
315
|
+
} catch (error) {
|
|
316
|
+
resolve({ valid: false, err: error as ValidateError });
|
|
327
317
|
}
|
|
328
318
|
});
|
|
329
319
|
}
|
|
@@ -337,8 +327,8 @@ export class ZSchemaAsyncSafe extends ZSchemaBase {
|
|
|
337
327
|
try {
|
|
338
328
|
this._validateSchema(schemaOrArr);
|
|
339
329
|
return { valid: true };
|
|
340
|
-
} catch (
|
|
341
|
-
return { valid: false, err:
|
|
330
|
+
} catch (error) {
|
|
331
|
+
return { valid: false, err: error as ValidateError };
|
|
342
332
|
}
|
|
343
333
|
}
|
|
344
334
|
}
|