z-schema 12.1.1 → 12.3.1
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 +4 -5
- package/cjs/{index.js → index.cjs} +696 -687
- 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} +43 -36
- 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} +7 -12
- package/dist/{json-validation.mjs → json-validation.js} +143 -127
- package/dist/{report.d.mts → report.d.ts} +7 -8
- package/dist/{report.mjs → report.js} +28 -31
- package/dist/{schema-cache.d.mts → schema-cache.d.ts} +4 -4
- 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} +95 -77
- 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.mjs → hostname.js} +19 -22
- package/dist/utils/{json.mjs → json.js} +11 -7
- package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
- package/dist/utils/{time.mjs → time.js} +5 -5
- 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} +4 -4
- package/dist/validation/{shared.mjs → shared.js} +12 -11
- package/dist/validation/{string.mjs → string.js} +32 -32
- 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} +4 -4
- 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} +37 -47
- package/package.json +25 -23
- package/src/errors.ts +1 -2
- package/src/format-validators.ts +139 -59
- 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 +27 -27
- 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 +5 -5
- 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 +29 -39
- 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 +723 -697
- package/umd/ZSchema.min.js +2 -2
- package/umd/package.json +3 -0
- 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/report.ts
CHANGED
|
@@ -76,7 +76,7 @@ export class Report {
|
|
|
76
76
|
commonErrorMessage?: string;
|
|
77
77
|
__$recursiveAnchorStack: JsonSchemaInternal[] = [];
|
|
78
78
|
__$dynamicScopeStack: JsonSchemaInternal[] = [];
|
|
79
|
-
__validationResultCache
|
|
79
|
+
__validationResultCache = new Map<unknown, Map<unknown, boolean>>();
|
|
80
80
|
errors: SchemaErrorDetail[] = [];
|
|
81
81
|
json?: unknown;
|
|
82
82
|
path: Array<number | string> = [];
|
|
@@ -88,8 +88,7 @@ export class Report {
|
|
|
88
88
|
reportOptions: ReportOptions;
|
|
89
89
|
validateOptions: ValidateOptions = {};
|
|
90
90
|
|
|
91
|
-
constructor(
|
|
92
|
-
constructor(parentReport: Report, validateOptions?: ValidateOptions); // subreport
|
|
91
|
+
constructor(parentOrOptions: ZSchemaOptions | Report, validateOptions?: ValidateOptions); // primary | subreport
|
|
93
92
|
constructor(parentReport: Report, reportOptions: ReportOptions, validateOptions?: ValidateOptions); // subreport with options
|
|
94
93
|
constructor(
|
|
95
94
|
parentOrOptions: ZSchemaOptions | Report,
|
|
@@ -102,8 +101,8 @@ export class Report {
|
|
|
102
101
|
// subreport
|
|
103
102
|
this.reportOptions = (reportOptionsOrValidate as ReportOptions) || {};
|
|
104
103
|
this.validateOptions = validateOptions || parentOrOptions.validateOptions;
|
|
105
|
-
this.__$recursiveAnchorStack =
|
|
106
|
-
this.__$dynamicScopeStack =
|
|
104
|
+
this.__$recursiveAnchorStack = parentOrOptions.__$recursiveAnchorStack.slice();
|
|
105
|
+
this.__$dynamicScopeStack = parentOrOptions.__$dynamicScopeStack.slice();
|
|
107
106
|
this.__validationResultCache = parentOrOptions.__validationResultCache;
|
|
108
107
|
} else {
|
|
109
108
|
// primary
|
|
@@ -122,7 +121,7 @@ export class Report {
|
|
|
122
121
|
return this.errors.length === 0;
|
|
123
122
|
}
|
|
124
123
|
|
|
125
|
-
addAsyncTask<
|
|
124
|
+
addAsyncTask<FN extends (...args: any[]) => any>(
|
|
126
125
|
fn: FN,
|
|
127
126
|
args: Parameters<FN>,
|
|
128
127
|
asyncTaskResultProcessFn: (result: ReturnType<FN>) => void
|
|
@@ -212,39 +211,32 @@ export class Report {
|
|
|
212
211
|
}
|
|
213
212
|
|
|
214
213
|
getPath(returnPathAsString?: boolean) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
path = path.concat(this.path);
|
|
214
|
+
const path: Array<string | number> = this.parentReport
|
|
215
|
+
? this.parentReport.path.concat(this.path)
|
|
216
|
+
: this.path.slice();
|
|
220
217
|
|
|
221
218
|
if (returnPathAsString !== true) {
|
|
222
219
|
// Sanitize the path segments (http://tools.ietf.org/html/rfc6901#section-4)
|
|
223
|
-
return
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
})
|
|
235
|
-
.join('/')
|
|
236
|
-
);
|
|
220
|
+
return `#/${path
|
|
221
|
+
.map((segment) => {
|
|
222
|
+
segment = segment.toString();
|
|
223
|
+
|
|
224
|
+
if (isAbsoluteUri(segment)) {
|
|
225
|
+
return `uri(${segment})`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return segment.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
229
|
+
})
|
|
230
|
+
.join('/')}`;
|
|
237
231
|
}
|
|
238
232
|
return path;
|
|
239
233
|
}
|
|
240
234
|
|
|
241
235
|
getSchemaPath(): Array<string | number> {
|
|
242
|
-
let schemaPath: Array<string | number> = [];
|
|
243
236
|
if (this.parentReport) {
|
|
244
|
-
|
|
237
|
+
return this.parentReport.schemaPath.concat(this.schemaPath);
|
|
245
238
|
}
|
|
246
|
-
|
|
247
|
-
return schemaPath;
|
|
239
|
+
return this.schemaPath.slice();
|
|
248
240
|
}
|
|
249
241
|
|
|
250
242
|
getSchemaId(): string | undefined {
|
|
@@ -253,16 +245,12 @@ export class Report {
|
|
|
253
245
|
}
|
|
254
246
|
|
|
255
247
|
// get the error path as an array
|
|
256
|
-
|
|
257
|
-
if (this.parentReport) {
|
|
258
|
-
path = path.concat(this.parentReport.path);
|
|
259
|
-
}
|
|
260
|
-
path = path.concat(this.path);
|
|
248
|
+
const path = this.parentReport ? this.parentReport.path.concat(this.path) : this.path.slice();
|
|
261
249
|
|
|
262
250
|
// try to find id in the error path
|
|
263
251
|
while (path.length > 0) {
|
|
264
252
|
const obj = get(this.rootSchema, path);
|
|
265
|
-
if (obj && obj.id) {
|
|
253
|
+
if (isObject(obj) && typeof obj.id === 'string') {
|
|
266
254
|
return obj.id;
|
|
267
255
|
}
|
|
268
256
|
path.pop();
|
|
@@ -272,7 +260,7 @@ export class Report {
|
|
|
272
260
|
return this.rootSchema.id;
|
|
273
261
|
}
|
|
274
262
|
|
|
275
|
-
hasError(errCode: string, errParams:
|
|
263
|
+
hasError(errCode: string, errParams: any[]) {
|
|
276
264
|
for (let idx = 0; idx < this.errors.length; idx++) {
|
|
277
265
|
if (this.errors[idx].code === errCode) {
|
|
278
266
|
// assume match
|
|
@@ -331,28 +319,27 @@ export class Report {
|
|
|
331
319
|
}
|
|
332
320
|
|
|
333
321
|
if (!errorMessage) {
|
|
334
|
-
throw new Error(
|
|
322
|
+
throw new Error(`No errorMessage known for code ${errorCode}`);
|
|
335
323
|
}
|
|
336
324
|
|
|
337
|
-
params
|
|
325
|
+
params ||= [];
|
|
338
326
|
|
|
339
327
|
for (let idx = 0; idx < params.length; idx++) {
|
|
340
328
|
const param = params[idx] === null || isObject(params[idx]) ? JSON.stringify(params[idx]) : params[idx];
|
|
341
|
-
errorMessage = errorMessage.replace(
|
|
329
|
+
errorMessage = errorMessage.replace(`{${idx}}`, param.toString());
|
|
342
330
|
}
|
|
343
331
|
|
|
344
|
-
const err
|
|
332
|
+
const err = {
|
|
345
333
|
code: errorCode,
|
|
346
|
-
params
|
|
334
|
+
params,
|
|
347
335
|
message: errorMessage,
|
|
348
336
|
path: this.getPath(this.options.reportPathAsArray),
|
|
349
337
|
schemaPath: this.getSchemaPath(),
|
|
350
338
|
schemaId: this.getSchemaId(),
|
|
351
|
-
keyword
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
(err as any)[jsonSymbol] = this.getJson();
|
|
339
|
+
keyword,
|
|
340
|
+
[schemaSymbol]: schema,
|
|
341
|
+
[jsonSymbol]: this.getJson(),
|
|
342
|
+
} as SchemaErrorDetail;
|
|
356
343
|
|
|
357
344
|
if (schema && typeof schema === 'string') {
|
|
358
345
|
err.description = schema;
|
|
@@ -370,9 +357,10 @@ export class Report {
|
|
|
370
357
|
subReports = [subReports];
|
|
371
358
|
}
|
|
372
359
|
err.inner = [];
|
|
373
|
-
for (
|
|
374
|
-
|
|
375
|
-
|
|
360
|
+
for (let si = 0; si < subReports.length; si++) {
|
|
361
|
+
const errs = subReports[si].errors;
|
|
362
|
+
for (let ei = 0; ei < errs.length; ei++) {
|
|
363
|
+
err.inner.push(errs[ei]);
|
|
376
364
|
}
|
|
377
365
|
}
|
|
378
366
|
if (err.inner.length === 0) {
|
package/src/schema-cache.ts
CHANGED
|
@@ -27,7 +27,7 @@ function getSafeRemotePath(uri: string): string | undefined {
|
|
|
27
27
|
const getEffectiveId = (schema: JsonSchemaInternal): string | undefined => {
|
|
28
28
|
let id = getId(schema);
|
|
29
29
|
if ((!id || !isAbsoluteUri(id)) && typeof schema.id === 'string' && isAbsoluteUri(schema.id)) {
|
|
30
|
-
id = schema
|
|
30
|
+
({ id } = schema);
|
|
31
31
|
}
|
|
32
32
|
return id;
|
|
33
33
|
};
|
|
@@ -43,13 +43,9 @@ export function prepareRemoteSchema(
|
|
|
43
43
|
validationOptions?: ZSchemaOptions,
|
|
44
44
|
maxCloneDepth?: number
|
|
45
45
|
): JsonSchemaInternal {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
_schema = JSON.parse(schema);
|
|
50
|
-
} else {
|
|
51
|
-
_schema = deepClone(schema, maxCloneDepth);
|
|
52
|
-
}
|
|
46
|
+
const _schema = (
|
|
47
|
+
typeof schema === 'string' ? JSON.parse(schema) : deepClone(schema, maxCloneDepth)
|
|
48
|
+
) as JsonSchemaInternal;
|
|
53
49
|
|
|
54
50
|
if (!_schema.id) {
|
|
55
51
|
_schema.id = uri;
|
|
@@ -63,10 +59,13 @@ export function prepareRemoteSchema(
|
|
|
63
59
|
}
|
|
64
60
|
|
|
65
61
|
export class SchemaCache {
|
|
66
|
-
static global_cache: SchemaCacheStorage = Object.create(null);
|
|
67
|
-
cache: SchemaCacheStorage = Object.create(null);
|
|
62
|
+
static global_cache: SchemaCacheStorage = Object.create(null) as SchemaCacheStorage;
|
|
63
|
+
cache: SchemaCacheStorage = Object.create(null) as SchemaCacheStorage;
|
|
64
|
+
private readonly validator: ZSchemaBase;
|
|
68
65
|
|
|
69
|
-
constructor(
|
|
66
|
+
constructor(validator: ZSchemaBase) {
|
|
67
|
+
this.validator = validator;
|
|
68
|
+
}
|
|
70
69
|
|
|
71
70
|
static cacheSchemaByUri(uri: string, schema: JsonSchemaInternal) {
|
|
72
71
|
const remotePath = getSafeRemotePath(uri);
|
|
@@ -190,7 +189,7 @@ export class SchemaCache {
|
|
|
190
189
|
usesAncestorReport = true;
|
|
191
190
|
} else {
|
|
192
191
|
remoteReport = new Report(report);
|
|
193
|
-
const noCache = result.id && isAbsoluteUri(result.id)
|
|
192
|
+
const noCache = !(result.id && isAbsoluteUri(result.id));
|
|
194
193
|
if (this.validator.sc.compileSchema(remoteReport, result, { noCache })) {
|
|
195
194
|
const savedOptions = this.validator.options;
|
|
196
195
|
try {
|
|
@@ -220,7 +219,7 @@ export class SchemaCache {
|
|
|
220
219
|
report.path.pop();
|
|
221
220
|
|
|
222
221
|
if (!remoteReportIsValid) {
|
|
223
|
-
return
|
|
222
|
+
return;
|
|
224
223
|
}
|
|
225
224
|
}
|
|
226
225
|
}
|
|
@@ -231,6 +230,7 @@ export class SchemaCache {
|
|
|
231
230
|
const parts = queryPath.split('/');
|
|
232
231
|
for (let idx = 0, lim = parts.length; result && idx < lim; idx++) {
|
|
233
232
|
const key = decodeJSONPointer(parts[idx]);
|
|
233
|
+
// oxlint-disable-next-line unicorn/prefer-ternary
|
|
234
234
|
if (idx === 0) {
|
|
235
235
|
// it's an id
|
|
236
236
|
result = findId(result, key, remotePath, remotePath, this.validator.options.maxRecursionDepth);
|