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.
Files changed (110) hide show
  1. package/README.md +2 -2
  2. package/bin/z-schema +4 -5
  3. package/cjs/{index.js → index.cjs} +696 -687
  4. package/cjs/{index.d.ts → index.d.cts} +47 -26
  5. package/dist/{errors.d.mts → errors.d.ts} +2 -2
  6. package/dist/{errors.mjs → errors.js} +1 -2
  7. package/dist/{format-validators.mjs → format-validators.js} +43 -36
  8. package/dist/{index.d.mts → index.d.ts} +9 -9
  9. package/dist/{index.mjs → index.js} +3 -3
  10. package/dist/{json-schema-versions.d.mts → json-schema-versions.d.ts} +34 -3
  11. package/dist/{json-schema.d.mts → json-schema.d.ts} +7 -7
  12. package/dist/{json-schema.mjs → json-schema.js} +7 -12
  13. package/dist/{json-validation.mjs → json-validation.js} +143 -127
  14. package/dist/{report.d.mts → report.d.ts} +7 -8
  15. package/dist/{report.mjs → report.js} +28 -31
  16. package/dist/{schema-cache.d.mts → schema-cache.d.ts} +4 -4
  17. package/dist/{schema-cache.mjs → schema-cache.js} +10 -11
  18. package/dist/{schema-compiler.d.mts → schema-compiler.d.ts} +4 -4
  19. package/dist/{schema-compiler.mjs → schema-compiler.js} +95 -77
  20. package/dist/{schema-validator.d.mts → schema-validator.d.ts} +5 -5
  21. package/dist/{schema-validator.mjs → schema-validator.js} +138 -166
  22. package/dist/utils/{array.mjs → array.js} +4 -3
  23. package/dist/utils/{base64.mjs → base64.js} +3 -2
  24. package/dist/utils/{clone.mjs → clone.js} +18 -20
  25. package/dist/utils/{hostname.mjs → hostname.js} +19 -22
  26. package/dist/utils/{json.mjs → json.js} +11 -7
  27. package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
  28. package/dist/utils/{time.mjs → time.js} +5 -5
  29. package/dist/utils/unicode.js +22 -0
  30. package/dist/utils/{what-is.mjs → what-is.js} +1 -2
  31. package/dist/validation/{array.mjs → array.js} +18 -20
  32. package/dist/validation/{combinators.mjs → combinators.js} +16 -16
  33. package/dist/validation/{numeric.mjs → numeric.js} +11 -11
  34. package/dist/validation/{object.mjs → object.js} +35 -34
  35. package/dist/validation/{ref.mjs → ref.js} +4 -4
  36. package/dist/validation/{shared.mjs → shared.js} +12 -11
  37. package/dist/validation/{string.mjs → string.js} +32 -32
  38. package/dist/validation/type.js +34 -0
  39. package/dist/{z-schema-base.d.mts → z-schema-base.d.ts} +11 -12
  40. package/dist/{z-schema-base.mjs → z-schema-base.js} +45 -40
  41. package/dist/{z-schema-options.d.mts → z-schema-options.d.ts} +3 -3
  42. package/dist/{z-schema-options.mjs → z-schema-options.js} +4 -4
  43. package/dist/{z-schema-reader.d.mts → z-schema-reader.d.ts} +1 -1
  44. package/dist/{z-schema-versions.mjs → z-schema-versions.js} +21 -21
  45. package/dist/{z-schema.d.mts → z-schema.d.ts} +5 -13
  46. package/dist/{z-schema.mjs → z-schema.js} +37 -47
  47. package/package.json +25 -23
  48. package/src/errors.ts +1 -2
  49. package/src/format-validators.ts +139 -59
  50. package/src/json-schema-versions.ts +56 -2
  51. package/src/json-schema.ts +10 -9
  52. package/src/json-validation.ts +189 -146
  53. package/src/report.ts +37 -49
  54. package/src/schema-cache.ts +13 -13
  55. package/src/schema-compiler.ts +170 -117
  56. package/src/schema-validator.ts +239 -238
  57. package/src/utils/array.ts +9 -6
  58. package/src/utils/base64.ts +13 -2
  59. package/src/utils/clone.ts +28 -30
  60. package/src/utils/date.ts +6 -3
  61. package/src/utils/hostname.ts +27 -27
  62. package/src/utils/json.ts +16 -9
  63. package/src/utils/properties.ts +2 -2
  64. package/src/utils/schema-regex.ts +4 -4
  65. package/src/utils/time.ts +5 -5
  66. package/src/utils/unicode.ts +12 -5
  67. package/src/utils/what-is.ts +1 -5
  68. package/src/validation/array.ts +24 -22
  69. package/src/validation/combinators.ts +14 -14
  70. package/src/validation/numeric.ts +14 -28
  71. package/src/validation/object.ts +32 -36
  72. package/src/validation/ref.ts +5 -6
  73. package/src/validation/shared.ts +22 -21
  74. package/src/validation/string.ts +29 -39
  75. package/src/validation/type.ts +17 -17
  76. package/src/z-schema-base.ts +49 -38
  77. package/src/z-schema-options.ts +4 -3
  78. package/src/z-schema.ts +35 -45
  79. package/umd/ZSchema.js +723 -697
  80. package/umd/ZSchema.min.js +2 -2
  81. package/umd/package.json +3 -0
  82. package/dist/utils/unicode.mjs +0 -12
  83. package/dist/validation/type.mjs +0 -32
  84. /package/dist/{format-validators.d.mts → format-validators.d.ts} +0 -0
  85. /package/dist/{json-schema-versions.mjs → json-schema-versions.js} +0 -0
  86. /package/dist/schemas/{draft-04-schema.mjs → draft-04-schema.js} +0 -0
  87. /package/dist/schemas/{draft-06-schema.mjs → draft-06-schema.js} +0 -0
  88. /package/dist/schemas/{draft-07-schema.mjs → draft-07-schema.js} +0 -0
  89. /package/dist/schemas/{draft-2019-09-meta-applicator.mjs → draft-2019-09-meta-applicator.js} +0 -0
  90. /package/dist/schemas/{draft-2019-09-meta-content.mjs → draft-2019-09-meta-content.js} +0 -0
  91. /package/dist/schemas/{draft-2019-09-meta-core.mjs → draft-2019-09-meta-core.js} +0 -0
  92. /package/dist/schemas/{draft-2019-09-meta-format.mjs → draft-2019-09-meta-format.js} +0 -0
  93. /package/dist/schemas/{draft-2019-09-meta-meta-data.mjs → draft-2019-09-meta-meta-data.js} +0 -0
  94. /package/dist/schemas/{draft-2019-09-meta-validation.mjs → draft-2019-09-meta-validation.js} +0 -0
  95. /package/dist/schemas/{draft-2019-09-schema.mjs → draft-2019-09-schema.js} +0 -0
  96. /package/dist/schemas/{draft-2020-12-meta-applicator.mjs → draft-2020-12-meta-applicator.js} +0 -0
  97. /package/dist/schemas/{draft-2020-12-meta-content.mjs → draft-2020-12-meta-content.js} +0 -0
  98. /package/dist/schemas/{draft-2020-12-meta-core.mjs → draft-2020-12-meta-core.js} +0 -0
  99. /package/dist/schemas/{draft-2020-12-meta-format-annotation.mjs → draft-2020-12-meta-format-annotation.js} +0 -0
  100. /package/dist/schemas/{draft-2020-12-meta-format-assertion.mjs → draft-2020-12-meta-format-assertion.js} +0 -0
  101. /package/dist/schemas/{draft-2020-12-meta-meta-data.mjs → draft-2020-12-meta-meta-data.js} +0 -0
  102. /package/dist/schemas/{draft-2020-12-meta-unevaluated.mjs → draft-2020-12-meta-unevaluated.js} +0 -0
  103. /package/dist/schemas/{draft-2020-12-meta-validation.mjs → draft-2020-12-meta-validation.js} +0 -0
  104. /package/dist/schemas/{draft-2020-12-schema.mjs → draft-2020-12-schema.js} +0 -0
  105. /package/dist/utils/{constants.mjs → constants.js} +0 -0
  106. /package/dist/utils/{date.mjs → date.js} +0 -0
  107. /package/dist/utils/{properties.mjs → properties.js} +0 -0
  108. /package/dist/utils/{symbols.mjs → symbols.js} +0 -0
  109. /package/dist/utils/{uri.mjs → uri.js} +0 -0
  110. /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: Map<unknown, Map<unknown, boolean>> = new Map();
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(zschemaOptions: ZSchemaOptions, validateOptions?: ValidateOptions); // primary
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 = [...parentOrOptions.__$recursiveAnchorStack];
106
- this.__$dynamicScopeStack = [...parentOrOptions.__$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<FV, FN extends (...args: any[]) => FV>(
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
- let path: Array<string | number> = [];
216
- if (this.parentReport) {
217
- path = path.concat(this.parentReport.path);
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
- path
226
- .map(function (segment) {
227
- segment = segment.toString();
228
-
229
- if (isAbsoluteUri(segment)) {
230
- return 'uri(' + segment + ')';
231
- }
232
-
233
- return segment.replace(/~/g, '~0').replace(/\//g, '~1');
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
- schemaPath = schemaPath.concat(this.parentReport.schemaPath);
237
+ return this.parentReport.schemaPath.concat(this.schemaPath);
245
238
  }
246
- schemaPath = schemaPath.concat(this.schemaPath);
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
- let path: Array<string | number> = [];
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: Array<any>) {
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('No errorMessage known for code ' + errorCode);
322
+ throw new Error(`No errorMessage known for code ${errorCode}`);
335
323
  }
336
324
 
337
- params = 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('{' + idx + '}', param.toString());
329
+ errorMessage = errorMessage.replace(`{${idx}}`, param.toString());
342
330
  }
343
331
 
344
- const err: SchemaErrorDetail = {
332
+ const err = {
345
333
  code: errorCode,
346
- params: 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: keyword,
352
- };
353
-
354
- (err as any)[schemaSymbol] = schema;
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 (const subReport of subReports) {
374
- for (const error of subReport.errors) {
375
- err.inner.push(error);
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) {
@@ -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.id;
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
- let _schema: JsonSchemaInternal;
47
-
48
- if (typeof schema === 'string') {
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(private validator: ZSchemaBase) {}
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) ? false : true;
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 undefined;
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);