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.
Files changed (111) hide show
  1. package/README.md +2 -2
  2. package/bin/z-schema +1 -1
  3. package/cjs/{index.js → index.cjs} +838 -707
  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} +60 -43
  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} +6 -5
  13. package/dist/{json-validation.mjs → json-validation.js} +144 -127
  14. package/dist/{report.d.mts → report.d.ts} +8 -9
  15. package/dist/{report.mjs → report.js} +28 -31
  16. package/dist/{schema-cache.d.mts → schema-cache.d.ts} +5 -5
  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} +101 -79
  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.js +176 -0
  26. package/dist/utils/{json.mjs → json.js} +13 -8
  27. package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
  28. package/dist/utils/{time.mjs → time.js} +8 -8
  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} +5 -5
  36. package/dist/validation/{shared.mjs → shared.js} +15 -12
  37. package/dist/validation/{string.mjs → string.js} +35 -35
  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} +5 -5
  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} +38 -48
  47. package/package.json +24 -25
  48. package/src/errors.ts +1 -2
  49. package/src/format-validators.ts +173 -67
  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 +257 -31
  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 +8 -7
  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 +36 -42
  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 +853 -715
  80. package/umd/ZSchema.min.js +2 -2
  81. package/umd/package.json +3 -0
  82. package/dist/utils/hostname.mjs +0 -75
  83. package/dist/utils/unicode.mjs +0 -12
  84. package/dist/validation/type.mjs +0 -32
  85. /package/dist/{format-validators.d.mts → format-validators.d.ts} +0 -0
  86. /package/dist/{json-schema-versions.mjs → json-schema-versions.js} +0 -0
  87. /package/dist/schemas/{draft-04-schema.mjs → draft-04-schema.js} +0 -0
  88. /package/dist/schemas/{draft-06-schema.mjs → draft-06-schema.js} +0 -0
  89. /package/dist/schemas/{draft-07-schema.mjs → draft-07-schema.js} +0 -0
  90. /package/dist/schemas/{draft-2019-09-meta-applicator.mjs → draft-2019-09-meta-applicator.js} +0 -0
  91. /package/dist/schemas/{draft-2019-09-meta-content.mjs → draft-2019-09-meta-content.js} +0 -0
  92. /package/dist/schemas/{draft-2019-09-meta-core.mjs → draft-2019-09-meta-core.js} +0 -0
  93. /package/dist/schemas/{draft-2019-09-meta-format.mjs → draft-2019-09-meta-format.js} +0 -0
  94. /package/dist/schemas/{draft-2019-09-meta-meta-data.mjs → draft-2019-09-meta-meta-data.js} +0 -0
  95. /package/dist/schemas/{draft-2019-09-meta-validation.mjs → draft-2019-09-meta-validation.js} +0 -0
  96. /package/dist/schemas/{draft-2019-09-schema.mjs → draft-2019-09-schema.js} +0 -0
  97. /package/dist/schemas/{draft-2020-12-meta-applicator.mjs → draft-2020-12-meta-applicator.js} +0 -0
  98. /package/dist/schemas/{draft-2020-12-meta-content.mjs → draft-2020-12-meta-content.js} +0 -0
  99. /package/dist/schemas/{draft-2020-12-meta-core.mjs → draft-2020-12-meta-core.js} +0 -0
  100. /package/dist/schemas/{draft-2020-12-meta-format-annotation.mjs → draft-2020-12-meta-format-annotation.js} +0 -0
  101. /package/dist/schemas/{draft-2020-12-meta-format-assertion.mjs → draft-2020-12-meta-format-assertion.js} +0 -0
  102. /package/dist/schemas/{draft-2020-12-meta-meta-data.mjs → draft-2020-12-meta-meta-data.js} +0 -0
  103. /package/dist/schemas/{draft-2020-12-meta-unevaluated.mjs → draft-2020-12-meta-unevaluated.js} +0 -0
  104. /package/dist/schemas/{draft-2020-12-meta-validation.mjs → draft-2020-12-meta-validation.js} +0 -0
  105. /package/dist/schemas/{draft-2020-12-schema.mjs → draft-2020-12-schema.js} +0 -0
  106. /package/dist/utils/{constants.mjs → constants.js} +0 -0
  107. /package/dist/utils/{date.mjs → date.js} +0 -0
  108. /package/dist/utils/{properties.mjs → properties.js} +0 -0
  109. /package/dist/utils/{symbols.mjs → symbols.js} +0 -0
  110. /package/dist/utils/{uri.mjs → uri.js} +0 -0
  111. /package/dist/{z-schema-reader.mjs → z-schema-reader.js} +0 -0
@@ -1,22 +1,22 @@
1
- import { isObject } from "../utils/what-is.mjs";
2
- import { compileSchemaRegex } from "../utils/schema-regex.mjs";
3
- import { difference } from "../utils/array.mjs";
4
- import { deferOrRunSync, shouldSkipValidate, supportsDependentKeywords } from "./shared.mjs";
1
+ import { isObject } from "../utils/what-is.js";
2
+ import { compileSchemaRegex } from "../utils/schema-regex.js";
3
+ import { difference } from "../utils/array.js";
4
+ import { deferOrRunSync, shouldSkipValidate, supportsDependentKeywords } from "./shared.js";
5
5
  //#region src/validation/object.ts
6
- function maxPropertiesValidator(report, schema, json) {
7
- if (shouldSkipValidate(this.validateOptions, ["OBJECT_PROPERTIES_MAXIMUM"])) return;
6
+ function maxPropertiesValidator(ctx, report, schema, json) {
7
+ if (shouldSkipValidate(ctx.validateOptions, ["OBJECT_PROPERTIES_MAXIMUM"])) return;
8
8
  if (!isObject(json)) return;
9
9
  const keysCount = Object.keys(json).length;
10
10
  if (keysCount > schema.maxProperties) report.addError("OBJECT_PROPERTIES_MAXIMUM", [keysCount, schema.maxProperties], void 0, schema, "maxProperties");
11
11
  }
12
- function minPropertiesValidator(report, schema, json) {
13
- if (shouldSkipValidate(this.validateOptions, ["OBJECT_PROPERTIES_MINIMUM"])) return;
12
+ function minPropertiesValidator(ctx, report, schema, json) {
13
+ if (shouldSkipValidate(ctx.validateOptions, ["OBJECT_PROPERTIES_MINIMUM"])) return;
14
14
  if (!isObject(json)) return;
15
15
  const keysCount = Object.keys(json).length;
16
16
  if (keysCount < schema.minProperties) report.addError("OBJECT_PROPERTIES_MINIMUM", [keysCount, schema.minProperties], void 0, schema, "minProperties");
17
17
  }
18
- function requiredValidator(report, schema, json) {
19
- if (shouldSkipValidate(this.validateOptions, ["OBJECT_MISSING_REQUIRED_PROPERTY"])) return;
18
+ function requiredValidator(ctx, report, schema, json) {
19
+ if (shouldSkipValidate(ctx.validateOptions, ["OBJECT_MISSING_REQUIRED_PROPERTY"])) return;
20
20
  if (!isObject(json)) return;
21
21
  const idx = schema.required.length;
22
22
  for (let i = 0; i < idx; i++) {
@@ -24,30 +24,31 @@ function requiredValidator(report, schema, json) {
24
24
  if (!Object.hasOwn(json, requiredPropertyName)) report.addError("OBJECT_MISSING_REQUIRED_PROPERTY", [requiredPropertyName], void 0, schema, "required");
25
25
  }
26
26
  }
27
- function additionalPropertiesValidator(report, schema, json) {
28
- if (schema.properties === void 0 && schema.patternProperties === void 0) return propertiesValidator.call(this, report, schema, json);
27
+ function additionalPropertiesValidator(ctx, report, schema, json) {
28
+ if (schema.properties === void 0 && schema.patternProperties === void 0) propertiesValidator(ctx, report, schema, json);
29
29
  }
30
- function patternPropertiesValidator(report, schema, json) {
31
- if (schema.properties === void 0) return propertiesValidator.call(this, report, schema, json);
30
+ function patternPropertiesValidator(ctx, report, schema, json) {
31
+ if (schema.properties === void 0) propertiesValidator(ctx, report, schema, json);
32
32
  }
33
- function propertiesValidator(report, schema, json) {
34
- if (shouldSkipValidate(this.validateOptions, ["OBJECT_ADDITIONAL_PROPERTIES"])) return;
33
+ function propertiesValidator(ctx, report, schema, json) {
34
+ if (shouldSkipValidate(ctx.validateOptions, ["OBJECT_ADDITIONAL_PROPERTIES"])) return;
35
35
  if (!isObject(json)) return;
36
- const properties = schema.properties !== void 0 ? schema.properties : {};
37
- const patternProperties = schema.patternProperties !== void 0 ? schema.patternProperties : {};
36
+ const properties = schema.properties === void 0 ? {} : schema.properties;
37
+ const patternProperties = schema.patternProperties === void 0 ? {} : schema.patternProperties;
38
38
  if (schema.additionalProperties === false) {
39
39
  let s = Object.keys(json);
40
40
  const p = Object.keys(properties);
41
41
  const pp = Object.keys(patternProperties);
42
42
  s = difference(s, p);
43
- for (const ppKey of pp) {
43
+ for (let i = 0; i < pp.length; i++) {
44
+ const ppKey = pp[i];
44
45
  const result = compileSchemaRegex(ppKey);
45
46
  if (!result.ok) continue;
46
47
  const regExp = result.value;
47
- for (let idx2 = s.length - 1; idx2 >= 0; idx2--) if (regExp.test(s[idx2]) === true) s.splice(idx2, 1);
48
+ for (let idx2 = s.length - 1; idx2 >= 0; idx2--) if (regExp.test(s[idx2])) s.splice(idx2, 1);
48
49
  }
49
50
  if (s.length > 0) {
50
- if (Array.isArray(this.options.assumeAdditional)) for (const allowed of this.options.assumeAdditional) {
51
+ if (Array.isArray(ctx.options.assumeAdditional)) for (const allowed of ctx.options.assumeAdditional) {
51
52
  const io = s.indexOf(allowed);
52
53
  if (io !== -1) s.splice(io, 1);
53
54
  }
@@ -55,29 +56,29 @@ function propertiesValidator(report, schema, json) {
55
56
  }
56
57
  }
57
58
  }
58
- function dependenciesValidator(report, schema, json) {
59
- if (shouldSkipValidate(this.validateOptions, ["OBJECT_DEPENDENCY_KEY"])) return;
59
+ function dependenciesValidator(ctx, report, schema, json) {
60
+ if (shouldSkipValidate(ctx.validateOptions, ["OBJECT_DEPENDENCY_KEY"])) return;
60
61
  if (!isObject(json)) return;
61
62
  const keys = Object.keys(schema.dependencies);
62
63
  for (const dependencyName of keys) if (Object.hasOwn(json, dependencyName)) {
63
64
  const dependencyDefinition = schema.dependencies[dependencyName];
64
65
  if (Array.isArray(dependencyDefinition)) {
65
66
  for (const requiredPropertyName of dependencyDefinition) if (!Object.hasOwn(json, requiredPropertyName)) report.addError("OBJECT_DEPENDENCY_KEY", [requiredPropertyName, dependencyName], void 0, schema, "dependencies");
66
- } else this._jsonValidate(report, dependencyDefinition, json);
67
+ } else ctx._jsonValidate(report, dependencyDefinition, json);
67
68
  }
68
69
  }
69
- function dependentSchemasValidator(report, schema, json) {
70
- if (!supportsDependentKeywords(schema, this.options.version)) return;
70
+ function dependentSchemasValidator(ctx, report, schema, json) {
71
+ if (!supportsDependentKeywords(schema, ctx.options.version)) return;
71
72
  if (!isObject(json) || !isObject(schema.dependentSchemas)) return;
72
73
  const keys = Object.keys(schema.dependentSchemas);
73
74
  for (const dependencyName of keys) if (Object.hasOwn(json, dependencyName)) {
74
75
  const dependencySchema = schema.dependentSchemas[dependencyName];
75
- this._jsonValidate(report, dependencySchema, json);
76
+ ctx._jsonValidate(report, dependencySchema, json);
76
77
  }
77
78
  }
78
- function dependentRequiredValidator(report, schema, json) {
79
- if (!supportsDependentKeywords(schema, this.options.version)) return;
80
- if (shouldSkipValidate(this.validateOptions, ["OBJECT_DEPENDENCY_KEY"])) return;
79
+ function dependentRequiredValidator(ctx, report, schema, json) {
80
+ if (!supportsDependentKeywords(schema, ctx.options.version)) return;
81
+ if (shouldSkipValidate(ctx.validateOptions, ["OBJECT_DEPENDENCY_KEY"])) return;
81
82
  if (!isObject(json) || !isObject(schema.dependentRequired)) return;
82
83
  const keys = Object.keys(schema.dependentRequired);
83
84
  for (const dependencyName of keys) {
@@ -87,8 +88,8 @@ function dependentRequiredValidator(report, schema, json) {
87
88
  for (const requiredPropertyName of requiredProperties) if (!Object.hasOwn(json, requiredPropertyName)) report.addError("OBJECT_DEPENDENCY_KEY", [requiredPropertyName, dependencyName], void 0, schema, "dependentRequired");
88
89
  }
89
90
  }
90
- function propertyNamesValidator(report, schema, json) {
91
- if (shouldSkipValidate(this.validateOptions, ["PROPERTY_NAMES"])) return;
91
+ function propertyNamesValidator(ctx, report, schema, json) {
92
+ if (shouldSkipValidate(ctx.validateOptions, ["PROPERTY_NAMES"])) return;
92
93
  if (!isObject(json)) return;
93
94
  const propertyNamesSchema = schema.propertyNames;
94
95
  if (propertyNamesSchema === void 0) return;
@@ -98,10 +99,10 @@ function propertyNamesValidator(report, schema, json) {
98
99
  for (const key of keys) {
99
100
  const subReport = new Report_(report);
100
101
  subReports.push(subReport);
101
- this._jsonValidate(subReport, propertyNamesSchema, key);
102
+ ctx._jsonValidate(subReport, propertyNamesSchema, key);
102
103
  }
103
104
  const addPropertyNameErrors = () => {
104
- for (let idx = 0; idx < keys.length; idx++) if (subReports[idx].errors.length > 0) report.addError("PROPERTY_NAMES", [keys[idx]], subReports[idx], schema, void 0);
105
+ for (let idx = 0; idx < keys.length; idx++) if (subReports[idx].errors.length > 0) report.addError("PROPERTY_NAMES", [keys[idx]], subReports[idx], schema);
105
106
  };
106
107
  deferOrRunSync(report, subReports, addPropertyNameErrors);
107
108
  }
@@ -1,11 +1,11 @@
1
- import { getRemotePath } from "../utils/uri.mjs";
2
- import { findId, getId } from "../json-schema.mjs";
1
+ import { getRemotePath } from "../utils/uri.js";
2
+ import { findId, getId } from "../json-schema.js";
3
3
  //#region src/validation/ref.ts
4
4
  const getDynamicRefAnchorName = (dynamicRef) => {
5
5
  const hashIdx = dynamicRef.indexOf("#");
6
6
  if (hashIdx === -1) return;
7
7
  const fragment = dynamicRef.slice(hashIdx + 1);
8
- if (!fragment || fragment[0] === "/") return;
8
+ if (!fragment || fragment.startsWith("/")) return;
9
9
  return fragment;
10
10
  };
11
11
  const findDynamicAnchorInScope = (scopeSchema, anchorName) => {
@@ -32,7 +32,7 @@ const resolveRecursiveRef = (schema, recursiveAnchorStack) => {
32
32
  */
33
33
  const resolveDynamicRef = (schema, dynamicScopeStack) => {
34
34
  const resolved = schema.__$dynamicRefResolved;
35
- if (typeof resolved === "undefined" || !schema.$dynamicRef) return resolved;
35
+ if (resolved === void 0 || !schema.$dynamicRef) return resolved;
36
36
  let target = resolved;
37
37
  const anchorName = getDynamicRefAnchorName(schema.$dynamicRef);
38
38
  if (anchorName && typeof target === "object" && target.$dynamicAnchor === anchorName) for (let scopeIdx = 0; scopeIdx < dynamicScopeStack.length; scopeIdx++) {
@@ -46,4 +46,4 @@ const resolveDynamicRef = (schema, dynamicScopeStack) => {
46
46
  return target;
47
47
  };
48
48
  //#endregion
49
- export { resolveDynamicRef, resolveRecursiveRef };
49
+ export { findDynamicAnchorInScope, getDynamicRefAnchorName, resolveDynamicRef, resolveRecursiveRef };
@@ -1,10 +1,6 @@
1
- import { isObject } from "../utils/what-is.mjs";
1
+ import { isObject } from "../utils/what-is.js";
2
2
  //#region src/validation/shared.ts
3
- const shouldSkipValidate = function(options, errors) {
4
- return options && Array.isArray(options.includeErrors) && options.includeErrors.length > 0 && !errors.some(function(err) {
5
- return options.includeErrors.includes(err);
6
- });
7
- };
3
+ const shouldSkipValidate = (options, errors) => options && Array.isArray(options.includeErrors) && options.includeErrors.length > 0 && !errors.some((err) => options.includeErrors.includes(err));
8
4
  const supportsDependentKeywords = (schema, version) => {
9
5
  if (typeof schema.$schema === "string") return !/draft-04|draft-06|draft-07/.test(schema.$schema);
10
6
  return !(version === "draft-04" || version === "draft-06" || version === "draft-07");
@@ -13,7 +9,7 @@ const VOCAB_VALIDATION_2019_09 = "https://json-schema.org/draft/2019-09/vocab/va
13
9
  const VOCAB_VALIDATION_2020_12 = "https://json-schema.org/draft/2020-12/vocab/validation";
14
10
  const VOCAB_FORMAT_2019_09 = "https://json-schema.org/draft/2019-09/vocab/format";
15
11
  const VOCAB_FORMAT_ASSERTION_2020_12 = "https://json-schema.org/draft/2020-12/vocab/format-assertion";
16
- const VALIDATION_VOCAB_KEYWORDS = new Set([
12
+ const VALIDATION_VOCAB_KEYWORDS = /* @__PURE__ */ new Set([
17
13
  "type",
18
14
  "multipleOf",
19
15
  "maximum",
@@ -44,7 +40,9 @@ const isValidationVocabularyEnabled = (schema, report, version) => {
44
40
  const metaSchema = currentSchemaMeta || rootSchemaMeta;
45
41
  if (!metaSchema || typeof metaSchema !== "object" || !isObject(metaSchema.$vocabulary)) return true;
46
42
  const vocabulary = metaSchema.$vocabulary;
47
- if (Object.hasOwn(vocabulary, VOCAB_VALIDATION_2019_09) || Object.hasOwn(vocabulary, VOCAB_VALIDATION_2020_12)) return vocabulary[VOCAB_VALIDATION_2019_09] === true || vocabulary[VOCAB_VALIDATION_2020_12] === true;
43
+ const has2019 = Object.hasOwn(vocabulary, VOCAB_VALIDATION_2019_09);
44
+ const has2020 = Object.hasOwn(vocabulary, VOCAB_VALIDATION_2020_12);
45
+ if (has2019 || has2020) return vocabulary[VOCAB_VALIDATION_2019_09] || vocabulary[VOCAB_VALIDATION_2020_12];
48
46
  return false;
49
47
  };
50
48
  /**
@@ -60,8 +58,8 @@ const isFormatAssertionVocabEnabled = (schema, report, version) => {
60
58
  const metaSchema = currentSchemaMeta || rootSchemaMeta;
61
59
  if (!metaSchema || typeof metaSchema !== "object" || !isObject(metaSchema.$vocabulary)) return false;
62
60
  const vocabulary = metaSchema.$vocabulary;
63
- if (Object.hasOwn(vocabulary, VOCAB_FORMAT_ASSERTION_2020_12)) return vocabulary[VOCAB_FORMAT_ASSERTION_2020_12] === true;
64
- if (Object.hasOwn(vocabulary, VOCAB_FORMAT_2019_09)) return vocabulary[VOCAB_FORMAT_2019_09] === true;
61
+ if (Object.hasOwn(vocabulary, VOCAB_FORMAT_ASSERTION_2020_12)) return vocabulary[VOCAB_FORMAT_ASSERTION_2020_12];
62
+ if (Object.hasOwn(vocabulary, VOCAB_FORMAT_2019_09)) return vocabulary[VOCAB_FORMAT_2019_09];
65
63
  return false;
66
64
  };
67
65
  function cacheValidationResult(report, schema, json, passed) {
@@ -85,9 +83,14 @@ function getCachedValidationResult(report, schema, json) {
85
83
  */
86
84
  function deferOrRunSync(report, subReports, decisionFn) {
87
85
  const asyncTasksBefore = report.asyncTasks.length;
88
- for (const subReport of subReports) report.asyncTasks.push(...subReport.asyncTasks);
86
+ for (let i = 0; i < subReports.length; i++) {
87
+ const tasks = subReports[i].asyncTasks;
88
+ for (let j = 0; j < tasks.length; j++) report.asyncTasks.push(tasks[j]);
89
+ }
89
90
  if (report.asyncTasks.length > asyncTasksBefore) report.addAsyncTaskWithPath((callback) => {
90
- setTimeout(() => callback(null), 0);
91
+ setTimeout(() => {
92
+ callback(null);
93
+ }, 0);
91
94
  }, [], () => {
92
95
  decisionFn();
93
96
  });
@@ -1,22 +1,22 @@
1
- import { whatIs } from "../utils/what-is.mjs";
2
- import { getFormatValidators } from "../format-validators.mjs";
3
- import { compileSchemaRegex } from "../utils/schema-regex.mjs";
4
- import { isFormatAssertionVocabEnabled, shouldSkipValidate } from "./shared.mjs";
5
- import { decodeBase64, isValidBase64 } from "../utils/base64.mjs";
6
- import { unicodeLength } from "../utils/unicode.mjs";
1
+ import { whatIs } from "../utils/what-is.js";
2
+ import { resolveFormatValidator } from "../format-validators.js";
3
+ import { compileSchemaRegex } from "../utils/schema-regex.js";
4
+ import { isFormatAssertionVocabEnabled, shouldSkipValidate } from "./shared.js";
5
+ import { decodeBase64, isValidBase64 } from "../utils/base64.js";
6
+ import { unicodeLength } from "../utils/unicode.js";
7
7
  //#region src/validation/string.ts
8
- function minLengthValidator(report, schema, json) {
9
- if (shouldSkipValidate(this.validateOptions, ["MIN_LENGTH"])) return;
8
+ function minLengthValidator(ctx, report, schema, json) {
9
+ if (shouldSkipValidate(ctx.validateOptions, ["MIN_LENGTH"])) return;
10
10
  if (typeof json !== "string") return;
11
11
  if (unicodeLength(json) < schema.minLength) report.addError("MIN_LENGTH", [json.length, schema.minLength], void 0, schema, "minLength");
12
12
  }
13
- function maxLengthValidator(report, schema, json) {
14
- if (shouldSkipValidate(this.validateOptions, ["MAX_LENGTH"])) return;
13
+ function maxLengthValidator(ctx, report, schema, json) {
14
+ if (shouldSkipValidate(ctx.validateOptions, ["MAX_LENGTH"])) return;
15
15
  if (typeof json !== "string") return;
16
16
  if (unicodeLength(json) > schema.maxLength) report.addError("MAX_LENGTH", [json.length, schema.maxLength], void 0, schema, "maxLength");
17
17
  }
18
- function patternValidator(report, schema, json) {
19
- if (shouldSkipValidate(this.validateOptions, ["PATTERN"])) return;
18
+ function patternValidator(ctx, report, schema, json) {
19
+ if (shouldSkipValidate(ctx.validateOptions, ["PATTERN"])) return;
20
20
  if (typeof json !== "string") return;
21
21
  const result = compileSchemaRegex(schema.pattern);
22
22
  if (!result.ok) {
@@ -29,46 +29,46 @@ function patternValidator(report, schema, json) {
29
29
  }
30
30
  if (!result.value.test(json)) report.addError("PATTERN", [schema.pattern, json], void 0, schema, "pattern");
31
31
  }
32
- function formatValidator(report, schema, json) {
33
- if (this.options.formatAssertions === false) return;
34
- if (this.options.formatAssertions === true) {
35
- if (!isFormatAssertionVocabEnabled(schema, report, this.options.version)) return;
36
- }
37
- const isModernDraft = this.options.version === "draft2019-09" || this.options.version === "draft2020-12";
38
- const formatValidatorFn = getFormatValidators(this.options)[schema.format];
32
+ function formatValidator(ctx, report, schema, json) {
33
+ if (ctx.options.formatAssertions === false) return;
34
+ if (ctx.options.formatAssertions === true && !isFormatAssertionVocabEnabled(schema, report, ctx.options.version)) return;
35
+ const isModernDraft = ctx.options.version === "draft2019-09" || ctx.options.version === "draft2020-12";
36
+ const formatValidatorFn = resolveFormatValidator(schema.format, ctx.options);
39
37
  if (typeof formatValidatorFn === "function") {
40
- if (shouldSkipValidate(this.validateOptions, ["INVALID_FORMAT"])) return;
38
+ if (shouldSkipValidate(ctx.validateOptions, ["INVALID_FORMAT"])) return;
41
39
  if (report.hasError("INVALID_TYPE", [schema.type, whatIs(json)])) return;
42
- if (formatValidatorFn.length === 2) report.addAsyncTaskWithPath(formatValidatorFn, [json], function(result) {
40
+ if (formatValidatorFn.length === 2) report.addAsyncTaskWithPath(formatValidatorFn, [json], (result) => {
43
41
  if (result !== true) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
44
42
  });
45
43
  else {
46
- const result = formatValidatorFn.call(this, json);
44
+ const result = formatValidatorFn.call(ctx, json);
47
45
  if (result instanceof Promise) {
48
46
  const promiseResult = result;
49
47
  report.addAsyncTaskWithPath(async (callback) => {
48
+ let resolved = false;
50
49
  try {
51
- callback(await promiseResult);
52
- } catch (_error) {
53
- callback(false);
54
- }
55
- }, [], function(resolvedResult) {
50
+ resolved = await promiseResult;
51
+ } catch {}
52
+ callback(resolved);
53
+ }, [], (resolvedResult) => {
56
54
  if (resolvedResult !== true) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
57
55
  });
58
- } else if (result !== true) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
56
+ } else if (!result) report.addError("INVALID_FORMAT", [schema.format, JSON.stringify(json)], void 0, schema, "format");
59
57
  }
60
- } else if (this.options.ignoreUnknownFormats !== true && !isModernDraft) report.addError("UNKNOWN_FORMAT", [schema.format], void 0, schema, "format");
58
+ } else if (ctx.options.ignoreUnknownFormats !== true && !isModernDraft) report.addError("UNKNOWN_FORMAT", [schema.format], void 0, schema, "format");
61
59
  }
62
- function contentEncodingValidator(report, schema, json) {
63
- if (this.options.version !== "draft-07") return;
60
+ function contentEncodingValidator(ctx, report, schema, json) {
61
+ if (ctx.options.version !== "draft-07") return;
64
62
  if (typeof json !== "string") return;
65
- if (schema.contentEncoding !== "base64") return;
63
+ const { contentEncoding } = schema;
64
+ if (contentEncoding !== "base64") return;
66
65
  if (!isValidBase64(json)) report.addError("INVALID_FORMAT", ["contentEncoding:base64", JSON.stringify(json)], void 0, schema, "contentEncoding");
67
66
  }
68
- function contentMediaTypeValidator(report, schema, json) {
69
- if (this.options.version !== "draft-07") return;
67
+ function contentMediaTypeValidator(ctx, report, schema, json) {
68
+ if (ctx.options.version !== "draft-07") return;
70
69
  if (typeof json !== "string") return;
71
- if (schema.contentMediaType !== "application/json") return;
70
+ const { contentMediaType } = schema;
71
+ if (contentMediaType !== "application/json") return;
72
72
  let payload = json;
73
73
  if (schema.contentEncoding === "base64") {
74
74
  const decoded = decodeBase64(json);
@@ -0,0 +1,34 @@
1
+ import { whatIs } from "../utils/what-is.js";
2
+ import { areEqual } from "../utils/json.js";
3
+ import { shouldSkipValidate } from "./shared.js";
4
+ //#region src/validation/type.ts
5
+ function typeValidator(ctx, report, schema, json) {
6
+ if (shouldSkipValidate(ctx.validateOptions, ["INVALID_TYPE"])) return;
7
+ const jsonType = whatIs(json);
8
+ if (typeof schema.type === "string") {
9
+ if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) report.addError("INVALID_TYPE", [schema.type, jsonType], void 0, schema, "type");
10
+ } else if (!schema.type.includes(jsonType) && (jsonType !== "integer" || !schema.type.includes("number"))) report.addError("INVALID_TYPE", [JSON.stringify(schema.type), jsonType], void 0, schema, "type");
11
+ }
12
+ function enumValidator(ctx, report, schema, json) {
13
+ if (shouldSkipValidate(ctx.validateOptions, ["ENUM_CASE_MISMATCH", "ENUM_MISMATCH"])) return;
14
+ const eqOpts = { maxDepth: ctx.options.maxRecursionDepth };
15
+ const eqOptsCI = {
16
+ caseInsensitiveComparison: true,
17
+ maxDepth: ctx.options.maxRecursionDepth
18
+ };
19
+ let caseInsensitiveMatch = false, match = false;
20
+ for (const enumVal of schema.enum) if (areEqual(json, enumVal, eqOpts)) {
21
+ match = true;
22
+ break;
23
+ } else if (areEqual(json, enumVal, eqOptsCI)) caseInsensitiveMatch = true;
24
+ if (!match) {
25
+ const error = caseInsensitiveMatch && ctx.options.enumCaseInsensitiveComparison ? "ENUM_CASE_MISMATCH" : "ENUM_MISMATCH";
26
+ report.addError(error, [JSON.stringify(json)], void 0, schema, "enum");
27
+ }
28
+ }
29
+ function constValidator(ctx, report, schema, json) {
30
+ const constValue = schema.const;
31
+ if (!areEqual(json, constValue, { maxDepth: ctx.options.maxRecursionDepth })) report.addError("CONST", [JSON.stringify(constValue)], void 0, schema);
32
+ }
33
+ //#endregion
34
+ export { constValidator, enumValidator, typeValidator };
@@ -1,11 +1,11 @@
1
- import { FormatValidatorFn } from "./format-validators.mjs";
2
- import { ZSchemaOptions } from "./z-schema-options.mjs";
3
- import { Report } from "./report.mjs";
4
- import { Errors, ValidateError } from "./errors.mjs";
5
- import { SchemaCache } from "./schema-cache.mjs";
6
- import { SchemaValidator } from "./schema-validator.mjs";
7
- import { SchemaCompiler } from "./schema-compiler.mjs";
8
- import { JsonSchema, JsonSchemaInternal } from "./json-schema-versions.mjs";
1
+ import { FormatValidatorFn } from "./format-validators.js";
2
+ import { ZSchemaOptions } from "./z-schema-options.js";
3
+ import { Report } from "./report.js";
4
+ import { Errors, ValidateError } from "./errors.js";
5
+ import { SchemaCache } from "./schema-cache.js";
6
+ import { SchemaValidator } from "./schema-validator.js";
7
+ import { SchemaCompiler } from "./schema-compiler.js";
8
+ import { JsonSchema, JsonSchemaInternal } from "./json-schema-versions.js";
9
9
 
10
10
  //#region src/z-schema-base.d.ts
11
11
  interface ValidateOptions {
@@ -13,10 +13,10 @@ interface ValidateOptions {
13
13
  includeErrors?: Array<keyof typeof Errors>;
14
14
  excludeErrors?: Array<keyof typeof Errors>;
15
15
  }
16
- type ValidateResponse = {
16
+ interface ValidateResponse {
17
17
  valid: boolean;
18
18
  err?: ValidateError;
19
- };
19
+ }
20
20
  type ValidateCallback = (err: ValidateResponse['err'], valid: ValidateResponse['valid']) => void;
21
21
  declare class ZSchemaBase {
22
22
  scache: SchemaCache;
@@ -36,8 +36,7 @@ declare class ZSchemaBase {
36
36
  getDefaultSchemaId(): string;
37
37
  _validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions, callback: ValidateCallback): void;
38
38
  _validate(json: unknown, schema: JsonSchema | string, callback: ValidateCallback): void;
39
- _validate(json: unknown, schema: JsonSchema | string, options: ValidateOptions): true;
40
- _validate(json: unknown, schema: JsonSchema | string): true;
39
+ _validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): true;
41
40
  _validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): true;
42
41
  /**
43
42
  * Register a format validator on this instance only (does not affect other instances or the global registry).
@@ -1,18 +1,18 @@
1
- import { getRemotePath } from "./utils/uri.mjs";
2
- import { isObject, whatIs } from "./utils/what-is.mjs";
3
- import { isInternalKey } from "./json-schema.mjs";
4
- import { getValidateError } from "./errors.mjs";
5
- import { copyProp } from "./utils/properties.mjs";
6
- import { deepClone } from "./utils/clone.mjs";
7
- import { get, sortedKeys } from "./utils/json.mjs";
8
- import { Report } from "./report.mjs";
9
- import { VERSION_SCHEMA_URL_MAPPING } from "./json-schema-versions.mjs";
10
- import { defaultOptions, normalizeOptions } from "./z-schema-options.mjs";
11
- import { SchemaCache, prepareRemoteSchema } from "./schema-cache.mjs";
12
- import { getSupportedFormats } from "./format-validators.mjs";
13
- import { validate } from "./json-validation.mjs";
14
- import { SchemaCompiler } from "./schema-compiler.mjs";
15
- import { SchemaValidator } from "./schema-validator.mjs";
1
+ import { getRemotePath } from "./utils/uri.js";
2
+ import { isObject, whatIs } from "./utils/what-is.js";
3
+ import { isInternalKey } from "./json-schema.js";
4
+ import { getValidateError } from "./errors.js";
5
+ import { copyProp } from "./utils/properties.js";
6
+ import { deepClone } from "./utils/clone.js";
7
+ import { get, sortedKeys } from "./utils/json.js";
8
+ import { Report } from "./report.js";
9
+ import { VERSION_SCHEMA_URL_MAPPING } from "./json-schema-versions.js";
10
+ import { defaultOptions, normalizeOptions } from "./z-schema-options.js";
11
+ import { SchemaCache, prepareRemoteSchema } from "./schema-cache.js";
12
+ import { getSupportedFormats } from "./format-validators.js";
13
+ import { validate } from "./json-validation.js";
14
+ import { SchemaCompiler } from "./schema-compiler.js";
15
+ import { SchemaValidator } from "./schema-validator.js";
16
16
  //#region src/z-schema-base.ts
17
17
  /**
18
18
  * Module-private symbol used by `ZSchema.create()` to authorise construction.
@@ -40,7 +40,7 @@ var ZSchemaBase = class {
40
40
  * create a circular dependency).
41
41
  */
42
42
  _jsonValidate(report, schema, json) {
43
- return validate.call(this, report, schema, json);
43
+ return validate(this, report, schema, json);
44
44
  }
45
45
  getDefaultSchemaId() {
46
46
  return this.options.version && this.options.version !== "none" ? VERSION_SCHEMA_URL_MAPPING[this.options.version] : VERSION_SCHEMA_URL_MAPPING[defaultOptions.version];
@@ -53,9 +53,9 @@ var ZSchemaBase = class {
53
53
  if (!options) options = {};
54
54
  this.validateOptions = options;
55
55
  if (typeof schema !== "string" && typeof schema !== "boolean" && !isObject(schema)) {
56
- const e = /* @__PURE__ */ new Error("Invalid .validate call - schema must be a string or object but " + whatIs(schema) + " was passed!");
56
+ const e = /* @__PURE__ */ new Error(`Invalid .validate call - schema must be a string or object but ${whatIs(schema)} was passed!`);
57
57
  if (callback) {
58
- setTimeout(function() {
58
+ setTimeout(() => {
59
59
  callback(e, false);
60
60
  }, 0);
61
61
  return;
@@ -70,9 +70,9 @@ var ZSchemaBase = class {
70
70
  const schemaName = schema;
71
71
  _schema = this.scache.getSchema(report, schemaName);
72
72
  if (!_schema) {
73
- const e = /* @__PURE__ */ new Error("Schema with id '" + schemaName + "' wasn't found in the validator cache!");
73
+ const e = /* @__PURE__ */ new Error(`Schema with id '${schemaName}' wasn't found in the validator cache!`);
74
74
  if (callback) {
75
- setTimeout(function() {
75
+ setTimeout(() => {
76
76
  callback(e, false);
77
77
  }, 0);
78
78
  return;
@@ -90,9 +90,9 @@ var ZSchemaBase = class {
90
90
  report.rootSchema = _schema;
91
91
  _schema = get(_schema, options.schemaPath);
92
92
  if (!_schema) {
93
- const e = /* @__PURE__ */ new Error("Schema path '" + options.schemaPath + "' wasn't found in the schema!");
93
+ const e = /* @__PURE__ */ new Error(`Schema path '${options.schemaPath}' wasn't found in the schema!`);
94
94
  if (callback) {
95
- setTimeout(function() {
95
+ setTimeout(() => {
96
96
  callback(e, false);
97
97
  }, 0);
98
98
  return;
@@ -100,7 +100,7 @@ var ZSchemaBase = class {
100
100
  throw e;
101
101
  }
102
102
  }
103
- if (!foundError) validate.call(this, report, _schema, json);
103
+ if (!foundError) validate(this, report, _schema, json);
104
104
  if (callback) {
105
105
  report.processAsyncTasks(this.options.asyncTimeout, callback);
106
106
  return;
@@ -171,8 +171,8 @@ var ZSchemaBase = class {
171
171
  if (!err) return [];
172
172
  const details = err.details || [];
173
173
  const missingRefs = [];
174
- function collect(details) {
175
- for (const detail of details) {
174
+ function collect(items) {
175
+ for (const detail of items) {
176
176
  if (detail.code === "UNRESOLVABLE_REFERENCE" || detail.code === "SCHEMA_NOT_REACHABLE") missingRefs.push(detail.params[0]);
177
177
  if (detail.inner) collect(detail.inner);
178
178
  }
@@ -188,9 +188,13 @@ var ZSchemaBase = class {
188
188
  getMissingRemoteReferences(err) {
189
189
  const missingReferences = this.getMissingReferences(err);
190
190
  const missingRemoteReferences = [];
191
+ const seen = /* @__PURE__ */ new Set();
191
192
  for (const ref of missingReferences) {
192
193
  const remoteReference = getRemotePath(ref);
193
- if (remoteReference && !missingRemoteReferences.includes(remoteReference)) missingRemoteReferences.push(remoteReference);
194
+ if (remoteReference && !seen.has(remoteReference)) {
195
+ seen.add(remoteReference);
196
+ missingRemoteReferences.push(remoteReference);
197
+ }
194
198
  }
195
199
  return missingRemoteReferences;
196
200
  }
@@ -203,24 +207,25 @@ var ZSchemaBase = class {
203
207
  getResolvedSchema(schemaId) {
204
208
  const report = new Report(this.options);
205
209
  const schema = this.scache.getSchemaByUri(report, schemaId);
206
- if (!schema) return void 0;
210
+ if (!schema) return;
207
211
  const clonedSchema = deepClone(schema, this.options.maxRecursionDepth);
208
212
  const visited = /* @__PURE__ */ new WeakSet();
209
- const cleanup = function(schema) {
210
- let key;
211
- const typeOf = whatIs(schema);
213
+ const cleanup = (node) => {
214
+ const typeOf = whatIs(node);
212
215
  if (typeOf !== "object" && typeOf !== "array") return;
213
- if (visited.has(schema)) return;
214
- visited.add(schema);
215
- if (schema.$ref && schema.__$refResolved) {
216
- const from = schema.__$refResolved;
217
- const to = schema;
218
- delete schema.$ref;
219
- delete schema.__$refResolved;
220
- for (key in from) copyProp(from, to, key);
216
+ if (visited.has(node)) return;
217
+ visited.add(node);
218
+ const schemaNode = node;
219
+ if (schemaNode.$ref && schemaNode.__$refResolved) {
220
+ const from = schemaNode.__$refResolved;
221
+ const to = schemaNode;
222
+ delete schemaNode.$ref;
223
+ delete schemaNode.__$refResolved;
224
+ for (const key in from) if (Object.hasOwn(from, key)) copyProp(from, to, key);
221
225
  }
222
- for (key in schema) if (Object.hasOwn(schema, key)) if (isInternalKey(key)) delete schema[key];
223
- else cleanup(schema[key]);
226
+ const record = node;
227
+ for (const key in record) if (Object.hasOwn(record, key)) if (isInternalKey(key)) delete record[key];
228
+ else cleanup(record[key]);
224
229
  };
225
230
  cleanup(clonedSchema);
226
231
  return clonedSchema;
@@ -1,6 +1,6 @@
1
- import { FormatValidatorFn } from "./format-validators.mjs";
2
- import { Report } from "./report.mjs";
3
- import { JsonSchemaVersion } from "./json-schema-versions.mjs";
1
+ import { FormatValidatorFn } from "./format-validators.js";
2
+ import { Report } from "./report.js";
3
+ import { JsonSchemaVersion } from "./json-schema-versions.js";
4
4
 
5
5
  //#region src/z-schema-options.d.ts
6
6
  interface ZSchemaOptions {
@@ -1,6 +1,6 @@
1
- import { MAX_ASYNC_TIMEOUT } from "./utils/constants.mjs";
2
- import { shallowClone } from "./utils/clone.mjs";
3
- import { CURRENT_DEFAULT_SCHEMA_VERSION } from "./json-schema-versions.mjs";
1
+ import { MAX_ASYNC_TIMEOUT } from "./utils/constants.js";
2
+ import { shallowClone } from "./utils/clone.js";
3
+ import { CURRENT_DEFAULT_SCHEMA_VERSION } from "./json-schema-versions.js";
4
4
  //#region src/z-schema-options.ts
5
5
  const defaultOptions = {
6
6
  version: CURRENT_DEFAULT_SCHEMA_VERSION,
@@ -33,7 +33,7 @@ const normalizeOptions = (options) => {
33
33
  let normalized;
34
34
  if (typeof options === "object") {
35
35
  let keys = Object.keys(options);
36
- for (const key of keys) if (defaultOptions[key] === void 0) throw new Error("Unexpected option passed to constructor: " + key);
36
+ for (const key of keys) if (defaultOptions[key] === void 0) throw new Error(`Unexpected option passed to constructor: ${key}`);
37
37
  keys = Object.keys(defaultOptions);
38
38
  for (const key of keys) if (options[key] === void 0) options[key] = shallowClone(defaultOptions[key]);
39
39
  normalized = options;
@@ -52,4 +52,4 @@ const normalizeOptions = (options) => {
52
52
  return normalized;
53
53
  };
54
54
  //#endregion
55
- export { defaultOptions, normalizeOptions };
55
+ export { MAX_ASYNC_TIMEOUT, defaultOptions, normalizeOptions };
@@ -1,4 +1,4 @@
1
- import { JsonSchema } from "./json-schema-versions.mjs";
1
+ import { JsonSchema } from "./json-schema-versions.js";
2
2
 
3
3
  //#region src/z-schema-reader.d.ts
4
4
  type SchemaReader = (uri: string) => JsonSchema;