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
@@ -1,61 +1,73 @@
1
- import { isInteger, isObject } from "./utils/what-is.mjs";
2
- import { getId } from "./json-schema.mjs";
3
- import { shallowClone } from "./utils/clone.mjs";
4
- import { Report } from "./report.mjs";
5
- import { isFormatSupported } from "./format-validators.mjs";
6
- import { compileSchemaRegex } from "./utils/schema-regex.mjs";
7
- import { isUniqueArray } from "./utils/array.mjs";
8
- import { validate } from "./json-validation.mjs";
1
+ import { isInteger, isObject } from "./utils/what-is.js";
2
+ import { getId } from "./json-schema.js";
3
+ import { shallowClone } from "./utils/clone.js";
4
+ import { Report } from "./report.js";
5
+ import { isFormatSupported } from "./format-validators.js";
6
+ import { compileSchemaRegex } from "./utils/schema-regex.js";
7
+ import { isUniqueArray } from "./utils/array.js";
8
+ import { validate } from "./json-validation.js";
9
9
  //#region src/schema-validator.ts
10
+ const PRIMITIVE_TYPES = [
11
+ "array",
12
+ "boolean",
13
+ "integer",
14
+ "number",
15
+ "null",
16
+ "object",
17
+ "string"
18
+ ];
19
+ const PRIMITIVE_TYPE_STR = PRIMITIVE_TYPES.join(",");
20
+ const PRIMITIVE_TYPES_SET = new Set(PRIMITIVE_TYPES);
10
21
  const SchemaValidators = {
11
- $ref: function(report, schema) {
22
+ $ref(report, schema) {
12
23
  if (typeof schema.$ref !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["$ref", "string"], void 0, schema, "$ref");
13
24
  },
14
- $schema: function(report, schema) {
25
+ $schema(report, schema) {
15
26
  if (typeof schema.$schema !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["$schema", "string"], void 0, schema, "$schema");
16
27
  },
17
- multipleOf: function(report, schema) {
28
+ multipleOf(report, schema) {
18
29
  if (typeof schema.multipleOf !== "number") report.addError("KEYWORD_TYPE_EXPECTED", ["multipleOf", "number"], void 0, schema, "multipleOf");
19
30
  else if (schema.multipleOf <= 0) report.addError("KEYWORD_MUST_BE", ["multipleOf", "strictly greater than 0"], void 0, schema, "multipleOf");
20
31
  },
21
- maximum: function(report, schema) {
32
+ maximum(report, schema) {
22
33
  if (typeof schema.maximum !== "number") report.addError("KEYWORD_TYPE_EXPECTED", ["maximum", "number"], void 0, schema, "maximum");
23
34
  },
24
- exclusiveMaximum: function(report, schema) {
35
+ exclusiveMaximum(report, schema) {
25
36
  if (report.options.version === "draft-04") {
26
37
  if (typeof schema.exclusiveMaximum !== "boolean") report.addError("KEYWORD_TYPE_EXPECTED", ["exclusiveMaximum", "boolean"], void 0, schema, "exclusiveMaximum");
27
38
  else if (schema.maximum === void 0) report.addError("KEYWORD_DEPENDENCY", ["exclusiveMaximum", "maximum"], void 0, schema, "exclusiveMaximum");
28
39
  } else if (typeof schema.exclusiveMaximum !== "boolean" && typeof schema.exclusiveMaximum !== "number") report.addError("KEYWORD_TYPE_EXPECTED", ["exclusiveMaximum", ["boolean", "number"]], void 0, schema, "exclusiveMaximum");
29
40
  },
30
- minimum: function(report, schema) {
41
+ minimum(report, schema) {
31
42
  if (typeof schema.minimum !== "number") report.addError("KEYWORD_TYPE_EXPECTED", ["minimum", "number"], void 0, schema, "minimum");
32
43
  },
33
- exclusiveMinimum: function(report, schema) {
44
+ exclusiveMinimum(report, schema) {
34
45
  if (report.options.version === "draft-04") {
35
46
  if (typeof schema.exclusiveMinimum !== "boolean") report.addError("KEYWORD_TYPE_EXPECTED", ["exclusiveMinimum", "boolean"], void 0, schema, "exclusiveMinimum");
36
47
  else if (schema.minimum === void 0) report.addError("KEYWORD_DEPENDENCY", ["exclusiveMinimum", "minimum"], void 0, schema, "exclusiveMinimum");
37
48
  } else if (typeof schema.exclusiveMinimum !== "boolean" && typeof schema.exclusiveMinimum !== "number") report.addError("KEYWORD_TYPE_EXPECTED", ["exclusiveMinimum", ["boolean", "number"]], void 0, schema, "exclusiveMinimum");
38
49
  },
39
- maxLength: function(report, schema) {
50
+ maxLength(report, schema) {
40
51
  if (!isInteger(schema.maxLength)) report.addError("KEYWORD_TYPE_EXPECTED", ["maxLength", "integer"], void 0, schema, "maxLength");
41
52
  else if (schema.maxLength < 0) report.addError("KEYWORD_MUST_BE", ["maxLength", "greater than, or equal to 0"], void 0, schema, "maxLength");
42
53
  },
43
- minLength: function(report, schema) {
54
+ minLength(report, schema) {
44
55
  if (!isInteger(schema.minLength)) report.addError("KEYWORD_TYPE_EXPECTED", ["minLength", "integer"], void 0, schema, "minLength");
45
56
  else if (schema.minLength < 0) report.addError("KEYWORD_MUST_BE", ["minLength", "greater than, or equal to 0"], void 0, schema, "minLength");
46
57
  },
47
- pattern: function(report, schema) {
48
- if (typeof schema.pattern !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["pattern", "string"], void 0, schema, "pattern");
49
- else {
50
- const result = compileSchemaRegex(schema.pattern);
51
- if (!result.ok) report.addError("KEYWORD_PATTERN", [
52
- "pattern",
53
- schema.pattern,
54
- result.error.message
55
- ], void 0, schema, "pattern");
58
+ pattern(report, schema) {
59
+ if (typeof schema.pattern !== "string") {
60
+ report.addError("KEYWORD_TYPE_EXPECTED", ["pattern", "string"], void 0, schema, "pattern");
61
+ return;
56
62
  }
63
+ const result = compileSchemaRegex(schema.pattern);
64
+ if (!result.ok) report.addError("KEYWORD_PATTERN", [
65
+ "pattern",
66
+ schema.pattern,
67
+ result.error.message
68
+ ], void 0, schema, "pattern");
57
69
  },
58
- additionalItems: function(report, schema) {
70
+ additionalItems(report, schema) {
59
71
  if (typeof schema.additionalItems !== "boolean" && !isObject(schema.additionalItems)) report.addError("KEYWORD_TYPE_EXPECTED", ["additionalItems", ["boolean", "object"]], void 0, schema, "additionalItems");
60
72
  else if (isObject(schema.additionalItems)) {
61
73
  report.path.push("additionalItems");
@@ -63,7 +75,7 @@ const SchemaValidators = {
63
75
  report.path.pop();
64
76
  }
65
77
  },
66
- items: function(report, schema) {
78
+ items(report, schema) {
67
79
  if (Array.isArray(schema.items)) for (let idx = 0; idx < schema.items.length; idx++) {
68
80
  report.path.push("items");
69
81
  report.path.push(idx);
@@ -83,34 +95,34 @@ const SchemaValidators = {
83
95
  if (this.options.forceAdditional === true && schema.additionalItems === void 0 && Array.isArray(schema.items)) report.addError("KEYWORD_UNDEFINED_STRICT", ["additionalItems"], void 0, schema, "additionalItems");
84
96
  if (this.options.assumeAdditional && schema.additionalItems === void 0 && Array.isArray(schema.items)) schema.additionalItems = false;
85
97
  },
86
- maxItems: function(report, schema) {
98
+ maxItems(report, schema) {
87
99
  if (typeof schema.maxItems !== "number") report.addError("KEYWORD_TYPE_EXPECTED", ["maxItems", "integer"], void 0, schema, "maxItems");
88
100
  else if (schema.maxItems < 0) report.addError("KEYWORD_MUST_BE", ["maxItems", "greater than, or equal to 0"], void 0, schema, "maxItems");
89
101
  },
90
- minItems: function(report, schema) {
102
+ minItems(report, schema) {
91
103
  if (!isInteger(schema.minItems)) report.addError("KEYWORD_TYPE_EXPECTED", ["minItems", "integer"], void 0, schema, "minItems");
92
104
  else if (schema.minItems < 0) report.addError("KEYWORD_MUST_BE", ["minItems", "greater than, or equal to 0"], void 0, schema, "minItems");
93
105
  },
94
- uniqueItems: function(report, schema) {
106
+ uniqueItems(report, schema) {
95
107
  if (typeof schema.uniqueItems !== "boolean") report.addError("KEYWORD_TYPE_EXPECTED", ["uniqueItems", "boolean"], void 0, schema, "uniqueItems");
96
108
  },
97
- maxProperties: function(report, schema) {
109
+ maxProperties(report, schema) {
98
110
  if (!isInteger(schema.maxProperties)) report.addError("KEYWORD_TYPE_EXPECTED", ["maxProperties", "integer"], void 0, schema, "maxProperties");
99
111
  else if (schema.maxProperties < 0) report.addError("KEYWORD_MUST_BE", ["maxProperties", "greater than, or equal to 0"], void 0, schema, "maxProperties");
100
112
  },
101
- minProperties: function(report, schema) {
113
+ minProperties(report, schema) {
102
114
  if (!isInteger(schema.minProperties)) report.addError("KEYWORD_TYPE_EXPECTED", ["minProperties", "integer"], void 0, schema, "minProperties");
103
115
  else if (schema.minProperties < 0) report.addError("KEYWORD_MUST_BE", ["minProperties", "greater than, or equal to 0"], void 0, schema, "minProperties");
104
116
  },
105
- required: function(report, schema) {
117
+ required(report, schema) {
106
118
  if (!Array.isArray(schema.required)) report.addError("KEYWORD_TYPE_EXPECTED", ["required", "array"], void 0, schema, "required");
107
119
  else if (report.options.version === "draft-04" && schema.required.length === 0) report.addError("KEYWORD_MUST_BE", ["required", "an array with at least one element"], void 0, schema, "required");
108
120
  else {
109
121
  for (const item of schema.required) if (typeof item !== "string") report.addError("KEYWORD_VALUE_TYPE", ["required", "string"], void 0, schema, "required");
110
- if (isUniqueArray(schema.required) === false) report.addError("KEYWORD_MUST_BE", ["required", "an array with unique items"], void 0, schema, "required");
122
+ if (!isUniqueArray(schema.required)) report.addError("KEYWORD_MUST_BE", ["required", "an array with unique items"], void 0, schema, "required");
111
123
  }
112
124
  },
113
- additionalProperties: function(report, schema) {
125
+ additionalProperties(report, schema) {
114
126
  if (typeof schema.additionalProperties !== "boolean" && !isObject(schema.additionalProperties)) report.addError("KEYWORD_TYPE_EXPECTED", ["additionalProperties", ["boolean", "object"]], void 0, schema, "additionalProperties");
115
127
  else if (isObject(schema.additionalProperties)) {
116
128
  report.path.push("additionalProperties");
@@ -118,7 +130,7 @@ const SchemaValidators = {
118
130
  report.path.pop();
119
131
  }
120
132
  },
121
- properties: function(report, schema) {
133
+ properties(report, schema) {
122
134
  if (!isObject(schema.properties)) {
123
135
  report.addError("KEYWORD_TYPE_EXPECTED", ["properties", "object"], void 0, schema, "properties");
124
136
  return;
@@ -136,7 +148,7 @@ const SchemaValidators = {
136
148
  if (this.options.assumeAdditional && schema.additionalProperties === void 0) schema.additionalProperties = false;
137
149
  if (this.options.forceProperties === true && keys.length === 0) report.addError("CUSTOM_MODE_FORCE_PROPERTIES", ["properties"], void 0, schema, "properties");
138
150
  },
139
- patternProperties: function(report, schema) {
151
+ patternProperties(report, schema) {
140
152
  if (!isObject(schema.patternProperties)) {
141
153
  report.addError("KEYWORD_TYPE_EXPECTED", ["patternProperties", "object"], void 0, schema, "patternProperties");
142
154
  return;
@@ -158,93 +170,52 @@ const SchemaValidators = {
158
170
  }
159
171
  if (this.options.forceProperties === true && keys.length === 0) report.addError("CUSTOM_MODE_FORCE_PROPERTIES", ["patternProperties"], void 0, schema, "patternProperties");
160
172
  },
161
- dependencies: function(report, schema) {
162
- if (!isObject(schema.dependencies)) report.addError("KEYWORD_TYPE_EXPECTED", ["dependencies", "object"], void 0, schema, "dependencies");
163
- else {
164
- const keys = Object.keys(schema.dependencies);
165
- for (const schemaKey of keys) {
166
- const schemaDependency = schema.dependencies[schemaKey];
167
- if (isObject(schemaDependency) || report.options.version !== "draft-04" && typeof schemaDependency === "boolean") {
168
- report.path.push("dependencies");
169
- report.path.push(schemaKey);
170
- this.validateSchema(report, schemaDependency);
171
- report.path.pop();
172
- report.path.pop();
173
- } else if (Array.isArray(schemaDependency)) {
174
- const depArray = schemaDependency;
175
- if (report.options.version === "draft-04" && depArray.length === 0) report.addError("KEYWORD_MUST_BE", ["dependencies", "not empty array"], void 0, schema, "dependencies");
176
- for (const dep of depArray) if (typeof dep !== "string") report.addError("KEYWORD_VALUE_TYPE", ["dependencies", "string"], void 0, schema, "dependencies");
177
- if (isUniqueArray(depArray) === false) report.addError("KEYWORD_MUST_BE", ["dependencies", "an array with unique items"], void 0, schema, "dependencies");
178
- } else report.addError("KEYWORD_VALUE_TYPE", ["dependencies", report.options.version === "draft-04" ? "object or array" : "boolean, object or array"], void 0, schema, "dependencies");
179
- }
173
+ dependencies(report, schema) {
174
+ if (!isObject(schema.dependencies)) {
175
+ report.addError("KEYWORD_TYPE_EXPECTED", ["dependencies", "object"], void 0, schema, "dependencies");
176
+ return;
177
+ }
178
+ const keys = Object.keys(schema.dependencies);
179
+ for (const schemaKey of keys) {
180
+ const schemaDependency = schema.dependencies[schemaKey];
181
+ if (isObject(schemaDependency) || report.options.version !== "draft-04" && typeof schemaDependency === "boolean") {
182
+ report.path.push("dependencies");
183
+ report.path.push(schemaKey);
184
+ this.validateSchema(report, schemaDependency);
185
+ report.path.pop();
186
+ report.path.pop();
187
+ } else if (Array.isArray(schemaDependency)) {
188
+ const depArray = schemaDependency;
189
+ if (report.options.version === "draft-04" && depArray.length === 0) report.addError("KEYWORD_MUST_BE", ["dependencies", "not empty array"], void 0, schema, "dependencies");
190
+ for (const dep of depArray) if (typeof dep !== "string") report.addError("KEYWORD_VALUE_TYPE", ["dependencies", "string"], void 0, schema, "dependencies");
191
+ if (!isUniqueArray(depArray)) report.addError("KEYWORD_MUST_BE", ["dependencies", "an array with unique items"], void 0, schema, "dependencies");
192
+ } else report.addError("KEYWORD_VALUE_TYPE", ["dependencies", report.options.version === "draft-04" ? "object or array" : "boolean, object or array"], void 0, schema, "dependencies");
180
193
  }
181
194
  },
182
- enum: function(report, schema) {
183
- if (Array.isArray(schema.enum) === false) report.addError("KEYWORD_TYPE_EXPECTED", ["enum", "array"], void 0, schema, "enum");
195
+ enum(report, schema) {
196
+ if (!Array.isArray(schema.enum)) report.addError("KEYWORD_TYPE_EXPECTED", ["enum", "array"], void 0, schema, "enum");
184
197
  else if (schema.enum.length === 0) report.addError("KEYWORD_MUST_BE", ["enum", "an array with at least one element"], void 0, schema, "enum");
185
- else if (isUniqueArray(schema.enum) === false) report.addError("KEYWORD_MUST_BE", ["enum", "an array with unique elements"], void 0, schema, "enum");
198
+ else if (!isUniqueArray(schema.enum)) report.addError("KEYWORD_MUST_BE", ["enum", "an array with unique elements"], void 0, schema, "enum");
186
199
  },
187
- type: function(report, schema) {
188
- const primitiveTypes = [
189
- "array",
190
- "boolean",
191
- "integer",
192
- "number",
193
- "null",
194
- "object",
195
- "string"
196
- ];
197
- const primitiveTypeStr = primitiveTypes.join(",");
200
+ type(report, schema) {
198
201
  const isArray = Array.isArray(schema.type);
199
202
  if (Array.isArray(schema.type)) {
200
- for (const typeItem of schema.type) if (!primitiveTypes.includes(typeItem)) report.addError("KEYWORD_TYPE_EXPECTED", ["type", primitiveTypeStr], void 0, schema, "type");
201
- if (isUniqueArray(schema.type) === false) report.addError("KEYWORD_MUST_BE", ["type", "an object with unique properties"], void 0, schema, "type");
203
+ for (const typeItem of schema.type) if (!PRIMITIVE_TYPES_SET.has(typeItem)) report.addError("KEYWORD_TYPE_EXPECTED", ["type", PRIMITIVE_TYPE_STR], void 0, schema, "type");
204
+ if (!isUniqueArray(schema.type)) report.addError("KEYWORD_MUST_BE", ["type", "an object with unique properties"], void 0, schema, "type");
202
205
  } else if (typeof schema.type === "string") {
203
- if (!primitiveTypes.includes(schema.type)) report.addError("KEYWORD_TYPE_EXPECTED", ["type", primitiveTypeStr], void 0, schema, "type");
206
+ if (!PRIMITIVE_TYPES_SET.has(schema.type)) report.addError("KEYWORD_TYPE_EXPECTED", ["type", PRIMITIVE_TYPE_STR], void 0, schema, "type");
204
207
  } else report.addError("KEYWORD_TYPE_EXPECTED", ["type", ["string", "array"]], void 0, schema, "type");
205
- if (this.options.noEmptyStrings === true) {
206
- if (schema.type === "string" || isArray && schema.type.includes("string")) {
207
- if (schema.minLength === void 0 && schema.enum === void 0 && schema.format === void 0) schema.minLength = 1;
208
- }
209
- }
210
- if (this.options.noEmptyArrays === true) {
211
- if (schema.type === "array" || isArray && schema.type.includes("array")) {
212
- if (schema.minItems === void 0) schema.minItems = 1;
213
- }
214
- }
215
- if (this.options.forceProperties === true) {
216
- if (schema.type === "object" || isArray && schema.type.includes("object")) {
217
- if (schema.properties === void 0 && schema.patternProperties === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["properties"], void 0, schema, "properties");
218
- }
219
- }
220
- if (this.options.forceItems === true) {
221
- if (schema.type === "array" || isArray && schema.type.includes("array")) {
222
- if (schema.items === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["items"], void 0, schema, "items");
223
- }
224
- }
225
- if (this.options.forceMinItems === true) {
226
- if (schema.type === "array" || isArray && schema.type.includes("array")) {
227
- if (schema.minItems === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["minItems"], void 0, schema, "minItems");
228
- }
229
- }
230
- if (this.options.forceMaxItems === true) {
231
- if (schema.type === "array" || isArray && schema.type.includes("array")) {
232
- if (schema.maxItems === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["maxItems"], void 0, schema, "maxItems");
233
- }
234
- }
235
- if (this.options.forceMinLength === true) {
236
- if (schema.type === "string" || isArray && schema.type.includes("string")) {
237
- if (schema.minLength === void 0 && schema.format === void 0 && schema.enum === void 0 && schema.pattern === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["minLength"], void 0, schema, "minLength");
238
- }
239
- }
240
- if (this.options.forceMaxLength === true) {
241
- if (schema.type === "string" || isArray && schema.type.includes("string")) {
242
- if (schema.maxLength === void 0 && schema.format === void 0 && schema.enum === void 0 && schema.pattern === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["maxLength"], void 0, schema, "maxLength");
243
- }
244
- }
245
- },
246
- allOf: function(report, schema) {
247
- if (Array.isArray(schema.allOf) === false) report.addError("KEYWORD_TYPE_EXPECTED", ["allOf", "array"], void 0, schema, "allOf");
208
+ if (this.options.noEmptyStrings === true && (schema.type === "string" || isArray && schema.type.includes("string")) && schema.minLength === void 0 && schema.enum === void 0 && schema.format === void 0) schema.minLength = 1;
209
+ if (this.options.noEmptyArrays === true && (schema.type === "array" || isArray && schema.type.includes("array")) && schema.minItems === void 0) schema.minItems = 1;
210
+ if (this.options.forceProperties === true && (schema.type === "object" || isArray && schema.type.includes("object")) && schema.properties === void 0 && schema.patternProperties === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["properties"], void 0, schema, "properties");
211
+ if (this.options.forceItems === true && (schema.type === "array" || isArray && schema.type.includes("array")) && schema.items === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["items"], void 0, schema, "items");
212
+ if (this.options.forceMinItems === true && (schema.type === "array" || isArray && schema.type.includes("array")) && schema.minItems === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["minItems"], void 0, schema, "minItems");
213
+ if (this.options.forceMaxItems === true && (schema.type === "array" || isArray && schema.type.includes("array")) && schema.maxItems === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["maxItems"], void 0, schema, "maxItems");
214
+ if (this.options.forceMinLength === true && (schema.type === "string" || isArray && schema.type.includes("string")) && schema.minLength === void 0 && schema.format === void 0 && schema.enum === void 0 && schema.pattern === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["minLength"], void 0, schema, "minLength");
215
+ if (this.options.forceMaxLength === true && (schema.type === "string" || isArray && schema.type.includes("string")) && schema.maxLength === void 0 && schema.format === void 0 && schema.enum === void 0 && schema.pattern === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["maxLength"], void 0, schema, "maxLength");
216
+ },
217
+ allOf(report, schema) {
218
+ if (!Array.isArray(schema.allOf)) report.addError("KEYWORD_TYPE_EXPECTED", ["allOf", "array"], void 0, schema, "allOf");
248
219
  else if (schema.allOf.length === 0) report.addError("KEYWORD_MUST_BE", ["allOf", "an array with at least one element"], void 0, schema, "allOf");
249
220
  else for (let idx = 0; idx < schema.allOf.length; idx++) {
250
221
  report.path.push("allOf");
@@ -254,8 +225,8 @@ const SchemaValidators = {
254
225
  report.path.pop();
255
226
  }
256
227
  },
257
- anyOf: function(report, schema) {
258
- if (Array.isArray(schema.anyOf) === false) report.addError("KEYWORD_TYPE_EXPECTED", ["anyOf", "array"], void 0, schema, "anyOf");
228
+ anyOf(report, schema) {
229
+ if (!Array.isArray(schema.anyOf)) report.addError("KEYWORD_TYPE_EXPECTED", ["anyOf", "array"], void 0, schema, "anyOf");
259
230
  else if (schema.anyOf.length === 0) report.addError("KEYWORD_MUST_BE", ["anyOf", "an array with at least one element"], void 0, schema, "anyOf");
260
231
  else for (let idx = 0; idx < schema.anyOf.length; idx++) {
261
232
  report.path.push("anyOf");
@@ -265,8 +236,8 @@ const SchemaValidators = {
265
236
  report.path.pop();
266
237
  }
267
238
  },
268
- oneOf: function(report, schema) {
269
- if (Array.isArray(schema.oneOf) === false) report.addError("KEYWORD_TYPE_EXPECTED", ["oneOf", "array"], void 0, schema, "oneOf");
239
+ oneOf(report, schema) {
240
+ if (!Array.isArray(schema.oneOf)) report.addError("KEYWORD_TYPE_EXPECTED", ["oneOf", "array"], void 0, schema, "oneOf");
270
241
  else if (schema.oneOf.length === 0) report.addError("KEYWORD_MUST_BE", ["oneOf", "an array with at least one element"], void 0, schema, "oneOf");
271
242
  else for (let idx = 0; idx < schema.oneOf.length; idx++) {
272
243
  report.path.push("oneOf");
@@ -276,16 +247,17 @@ const SchemaValidators = {
276
247
  report.path.pop();
277
248
  }
278
249
  },
279
- not: function(report, schema) {
250
+ not(report, schema) {
280
251
  const notSchema = schema.not;
281
- if (!(report.options.version === "draft-04" ? isObject(notSchema) : typeof notSchema === "boolean" || isObject(notSchema))) report.addError("KEYWORD_TYPE_EXPECTED", ["not", report.options.version === "draft-04" ? "object" : ["boolean", "object"]], void 0, schema, "not");
282
- else {
283
- report.path.push("not");
284
- this.validateSchema(report, notSchema);
285
- report.path.pop();
252
+ if (!(report.options.version === "draft-04" ? isObject(notSchema) : typeof notSchema === "boolean" || isObject(notSchema))) {
253
+ report.addError("KEYWORD_TYPE_EXPECTED", ["not", report.options.version === "draft-04" ? "object" : ["boolean", "object"]], void 0, schema, "not");
254
+ return;
286
255
  }
256
+ report.path.push("not");
257
+ this.validateSchema(report, notSchema);
258
+ report.path.pop();
287
259
  },
288
- if: function(report, schema) {
260
+ if(report, schema) {
289
261
  if (report.options.version !== "draft-07") return;
290
262
  const ifSchema = schema.if;
291
263
  if (!(typeof ifSchema === "boolean" || isObject(ifSchema))) {
@@ -296,7 +268,7 @@ const SchemaValidators = {
296
268
  this.validateSchema(report, ifSchema);
297
269
  report.path.pop();
298
270
  },
299
- then: function(report, schema) {
271
+ then(report, schema) {
300
272
  if (report.options.version !== "draft-07") return;
301
273
  const thenSchema = schema.then;
302
274
  if (!(typeof thenSchema === "boolean" || isObject(thenSchema))) {
@@ -307,7 +279,7 @@ const SchemaValidators = {
307
279
  this.validateSchema(report, thenSchema);
308
280
  report.path.pop();
309
281
  },
310
- else: function(report, schema) {
282
+ else(report, schema) {
311
283
  if (report.options.version !== "draft-07") return;
312
284
  const elseSchema = schema.else;
313
285
  if (!(typeof elseSchema === "boolean" || isObject(elseSchema))) {
@@ -318,21 +290,22 @@ const SchemaValidators = {
318
290
  this.validateSchema(report, elseSchema);
319
291
  report.path.pop();
320
292
  },
321
- definitions: function(report, schema) {
322
- if (!isObject(schema.definitions)) report.addError("KEYWORD_TYPE_EXPECTED", ["definitions", "object"], void 0, schema, "definitions");
323
- else {
324
- const keys = Object.keys(schema.definitions);
325
- for (const key of keys) {
326
- const val = schema.definitions[key];
327
- report.path.push("definitions");
328
- report.path.push(key);
329
- this.validateSchema(report, val);
330
- report.path.pop();
331
- report.path.pop();
332
- }
293
+ definitions(report, schema) {
294
+ if (!isObject(schema.definitions)) {
295
+ report.addError("KEYWORD_TYPE_EXPECTED", ["definitions", "object"], void 0, schema, "definitions");
296
+ return;
297
+ }
298
+ const keys = Object.keys(schema.definitions);
299
+ for (const key of keys) {
300
+ const val = schema.definitions[key];
301
+ report.path.push("definitions");
302
+ report.path.push(key);
303
+ this.validateSchema(report, val);
304
+ report.path.pop();
305
+ report.path.pop();
333
306
  }
334
307
  },
335
- $defs: function(report, schema) {
308
+ $defs(report, schema) {
336
309
  if (report.options.version !== "draft2019-09" && report.options.version !== "draft2020-12") return;
337
310
  if (!isObject(schema.$defs)) {
338
311
  report.addError("KEYWORD_TYPE_EXPECTED", ["$defs", "object"], void 0, schema, "$defs");
@@ -348,34 +321,36 @@ const SchemaValidators = {
348
321
  report.path.pop();
349
322
  }
350
323
  },
351
- format: function(report, schema) {
324
+ format(report, schema) {
352
325
  if (this.options.formatAssertions === false) return;
353
- if (typeof schema.format !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["format", "string"], void 0, schema, "format");
354
- else {
355
- const isModernDraft = this.options.version === "draft2019-09" || this.options.version === "draft2020-12";
356
- if (!isFormatSupported(schema.format, this.options.customFormats) && this.options.ignoreUnknownFormats !== true && !isModernDraft) report.addError("UNKNOWN_FORMAT", [schema.format], void 0, schema, "format");
326
+ if (typeof schema.format !== "string") {
327
+ report.addError("KEYWORD_TYPE_EXPECTED", ["format", "string"], void 0, schema, "format");
328
+ return;
357
329
  }
330
+ const isModernDraft = this.options.version === "draft2019-09" || this.options.version === "draft2020-12";
331
+ if (!isFormatSupported(schema.format, this.options.customFormats) && this.options.ignoreUnknownFormats !== true && !isModernDraft) report.addError("UNKNOWN_FORMAT", [schema.format], void 0, schema, "format");
358
332
  },
359
- contentEncoding: function(report, schema) {
333
+ contentEncoding(report, schema) {
360
334
  if (report.options.version !== "draft-07") return;
361
335
  if (typeof schema.contentEncoding !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["contentEncoding", "string"], void 0, schema, "contentEncoding");
362
336
  },
363
- contentMediaType: function(report, schema) {
337
+ contentMediaType(report, schema) {
364
338
  if (report.options.version !== "draft-07") return;
365
339
  if (typeof schema.contentMediaType !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["contentMediaType", "string"], void 0, schema, "contentMediaType");
366
340
  },
367
- id: function(report, schema) {
341
+ id(report, schema) {
368
342
  if (typeof schema.id !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["id", "string"], void 0, schema, "id");
369
343
  },
370
- title: function(report, schema) {
344
+ title(report, schema) {
371
345
  if (typeof schema.title !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["title", "string"], void 0, schema, "title");
372
346
  },
373
- description: function(report, schema) {
347
+ description(report, schema) {
374
348
  if (typeof schema.description !== "string") report.addError("KEYWORD_TYPE_EXPECTED", ["description", "string"], void 0, schema, "description");
375
349
  },
376
- default: function() {}
350
+ default() {}
377
351
  };
378
352
  var SchemaValidator = class {
353
+ validator;
379
354
  constructor(validator) {
380
355
  this.validator = validator;
381
356
  }
@@ -395,18 +370,17 @@ var SchemaValidator = class {
395
370
  if (hasParentSchema) {
396
371
  if (schema.__$schemaResolved && schema.__$schemaResolved !== schema) {
397
372
  const subReport = new Report(report);
398
- if (validate.call(this.validator, subReport, schema.__$schemaResolved, schema) === false) report.addError("PARENT_SCHEMA_VALIDATION_FAILED", void 0, subReport, schema, "$schema");
373
+ if (!validate(this.validator, subReport, schema.__$schemaResolved, schema)) report.addError("PARENT_SCHEMA_VALIDATION_FAILED", void 0, subReport, schema, "$schema");
399
374
  } else if (this.validator.options.ignoreUnresolvableReferences !== true) report.addError("REF_UNRESOLVED", [schema.$schema], void 0, schema, "$schema");
400
375
  }
401
376
  if (this.validator.options.noTypeless === true) {
402
377
  if (schema.type !== void 0) {
403
- let schemas = [];
404
- if (Array.isArray(schema.anyOf)) schemas = schemas.concat(schema.anyOf);
405
- if (Array.isArray(schema.oneOf)) schemas = schemas.concat(schema.oneOf);
406
- if (Array.isArray(schema.allOf)) schemas = schemas.concat(schema.allOf);
407
- schemas.forEach(function(sch) {
378
+ const inheritType = (sch) => {
408
379
  if (!sch.type) sch.type = schema.type;
409
- });
380
+ };
381
+ if (Array.isArray(schema.anyOf)) for (let i = 0; i < schema.anyOf.length; i++) inheritType(schema.anyOf[i]);
382
+ if (Array.isArray(schema.oneOf)) for (let i = 0; i < schema.oneOf.length; i++) inheritType(schema.oneOf[i]);
383
+ if (Array.isArray(schema.allOf)) for (let i = 0; i < schema.allOf.length; i++) inheritType(schema.allOf[i]);
410
384
  }
411
385
  if (schema.enum === void 0 && schema.type === void 0 && schema.anyOf === void 0 && schema.oneOf === void 0 && schema.not === void 0 && schema.$ref === void 0) report.addError("KEYWORD_UNDEFINED_STRICT", ["type"], void 0, schema, "type");
412
386
  }
@@ -414,9 +388,7 @@ var SchemaValidator = class {
414
388
  for (const key of keys) {
415
389
  if (key.startsWith("__")) continue;
416
390
  if (Object.hasOwn(SchemaValidators, key)) SchemaValidators[key].call(this, report, schema);
417
- else if (!hasParentSchema) {
418
- if (this.validator.options.noExtraKeywords === true) report.addError("KEYWORD_UNEXPECTED", [key], void 0, schema, void 0);
419
- }
391
+ else if (!hasParentSchema && this.validator.options.noExtraKeywords === true) report.addError("KEYWORD_UNEXPECTED", [key], void 0, schema);
420
392
  }
421
393
  if (this.validator.options.pedanticCheck === true) {
422
394
  if (schema.enum) {
@@ -426,14 +398,14 @@ var SchemaValidator = class {
426
398
  report.path.push("enum");
427
399
  for (let idx = 0; idx < schema.enum.length; idx++) {
428
400
  report.path.push(idx);
429
- validate.call(this.validator, report, tmpSchema, schema.enum[idx]);
401
+ validate(this.validator, report, tmpSchema, schema.enum[idx]);
430
402
  report.path.pop();
431
403
  }
432
404
  report.path.pop();
433
405
  }
434
406
  if (schema.default) {
435
407
  report.path.push("default");
436
- validate.call(this.validator, report, schema, schema.default);
408
+ validate(this.validator, report, schema, schema.default);
437
409
  report.path.pop();
438
410
  }
439
411
  }
@@ -1,4 +1,4 @@
1
- import { areEqual } from "./json.mjs";
1
+ import { areEqual } from "./json.js";
2
2
  //#region src/utils/array.ts
3
3
  /**
4
4
  * Check if all elements in an array are unique.
@@ -22,7 +22,7 @@ const isUniqueArray = (arr, indexes, maxDepth) => {
22
22
  const seen = /* @__PURE__ */ new Set();
23
23
  for (let i = 0; i < l; i++) {
24
24
  const v = arr[i];
25
- const key = typeof v + ":" + String(v);
25
+ const key = `${typeof v}:${String(v)}`;
26
26
  if (seen.has(key)) {
27
27
  if (indexes) for (let j = 0; j < i; j++) {
28
28
  const prev = arr[j];
@@ -37,7 +37,8 @@ const isUniqueArray = (arr, indexes, maxDepth) => {
37
37
  }
38
38
  return true;
39
39
  }
40
- for (let i = 0; i < l; i++) for (let j = i + 1; j < l; j++) if (areEqual(arr[i], arr[j], { maxDepth })) {
40
+ const eqOpts = { maxDepth };
41
+ for (let i = 0; i < l; i++) for (let j = i + 1; j < l; j++) if (areEqual(arr[i], arr[j], eqOpts)) {
41
42
  if (indexes) indexes.push(i, j);
42
43
  return false;
43
44
  }
@@ -11,8 +11,9 @@ const decodeBase64 = (value) => {
11
11
  } catch {
12
12
  return;
13
13
  }
14
- if (typeof Buffer !== "undefined") try {
15
- return Buffer.from(value, "base64").toString("utf8");
14
+ const bufferCtor = globalThis.Buffer;
15
+ if (bufferCtor !== void 0) try {
16
+ return bufferCtor.from(value, "base64").toString("utf-8");
16
17
  } catch {
17
18
  return;
18
19
  }
@@ -1,39 +1,37 @@
1
- import { copyProp } from "./properties.mjs";
1
+ import { copyProp } from "./properties.js";
2
2
  //#region src/utils/clone.ts
3
3
  const shallowClone = (src) => {
4
4
  if (src == null || typeof src !== "object") return src;
5
- let res;
6
5
  if (Array.isArray(src)) {
7
- res = [];
6
+ const res = [];
8
7
  for (let i = 0; i < src.length; i++) res[i] = src[i];
9
- } else {
10
- res = {};
11
- const keys = Object.keys(src).sort();
12
- for (const key of keys) copyProp(src, res, key);
8
+ return res;
13
9
  }
10
+ const res = {};
11
+ const keys = Object.keys(src).sort();
12
+ for (let i = 0; i < keys.length; i++) copyProp(src, res, keys[i]);
14
13
  return res;
15
14
  };
16
15
  const deepClone = (src, maxDepth = 100) => {
17
16
  let vidx = 0;
18
17
  const visited = /* @__PURE__ */ new Map();
19
18
  const cloned = [];
20
- const cloneDeepInner = (src, _depth) => {
21
- if (typeof src !== "object" || src === null) return src;
19
+ const cloneDeepInner = (node, _depth) => {
20
+ if (typeof node !== "object" || node === null) return node;
22
21
  if (_depth >= maxDepth) throw new Error(`Maximum recursion depth (${maxDepth}) exceeded in deepClone. If your schema or data is deeply nested and valid, increase the maxRecursionDepth option.`);
23
- let res;
24
- const cidx = visited.get(src);
22
+ const cidx = visited.get(node);
25
23
  if (cidx !== void 0) return cloned[cidx];
26
- visited.set(src, vidx++);
27
- if (Array.isArray(src)) {
28
- res = [];
29
- cloned.push(res);
30
- for (let i = 0; i < src.length; i++) res[i] = cloneDeepInner(src[i], _depth + 1);
31
- } else {
32
- res = {};
24
+ visited.set(node, vidx++);
25
+ if (Array.isArray(node)) {
26
+ const res = [];
33
27
  cloned.push(res);
34
- const keys = Object.keys(src).sort();
35
- for (const key of keys) copyProp(src, res, key, (v) => cloneDeepInner(v, _depth + 1));
28
+ for (let i = 0; i < node.length; i++) res[i] = cloneDeepInner(node[i], _depth + 1);
29
+ return res;
36
30
  }
31
+ const res = {};
32
+ cloned.push(res);
33
+ const keys = Object.keys(node).sort();
34
+ for (let i = 0; i < keys.length; i++) copyProp(node, res, keys[i], (v) => cloneDeepInner(v, _depth + 1));
37
35
  return res;
38
36
  };
39
37
  return cloneDeepInner(src, 0);