z-schema 12.2.0 → 12.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/z-schema +1 -1
- package/cjs/{index.js → index.cjs} +696 -687
- package/cjs/{index.d.ts → index.d.cts} +47 -26
- package/dist/{errors.d.mts → errors.d.ts} +2 -2
- package/dist/{errors.mjs → errors.js} +1 -2
- package/dist/{format-validators.mjs → format-validators.js} +43 -36
- package/dist/{index.d.mts → index.d.ts} +9 -9
- package/dist/{index.mjs → index.js} +3 -3
- package/dist/{json-schema-versions.d.mts → json-schema-versions.d.ts} +34 -3
- package/dist/{json-schema.d.mts → json-schema.d.ts} +7 -7
- package/dist/{json-schema.mjs → json-schema.js} +7 -12
- package/dist/{json-validation.mjs → json-validation.js} +143 -127
- package/dist/{report.d.mts → report.d.ts} +7 -8
- package/dist/{report.mjs → report.js} +28 -31
- package/dist/{schema-cache.d.mts → schema-cache.d.ts} +4 -4
- package/dist/{schema-cache.mjs → schema-cache.js} +10 -11
- package/dist/{schema-compiler.d.mts → schema-compiler.d.ts} +4 -4
- package/dist/{schema-compiler.mjs → schema-compiler.js} +95 -77
- package/dist/{schema-validator.d.mts → schema-validator.d.ts} +5 -5
- package/dist/{schema-validator.mjs → schema-validator.js} +138 -166
- package/dist/utils/{array.mjs → array.js} +4 -3
- package/dist/utils/{base64.mjs → base64.js} +3 -2
- package/dist/utils/{clone.mjs → clone.js} +18 -20
- package/dist/utils/{hostname.mjs → hostname.js} +19 -22
- package/dist/utils/{json.mjs → json.js} +11 -7
- package/dist/utils/{schema-regex.mjs → schema-regex.js} +5 -5
- package/dist/utils/{time.mjs → time.js} +5 -5
- package/dist/utils/unicode.js +22 -0
- package/dist/utils/{what-is.mjs → what-is.js} +1 -2
- package/dist/validation/{array.mjs → array.js} +18 -20
- package/dist/validation/{combinators.mjs → combinators.js} +16 -16
- package/dist/validation/{numeric.mjs → numeric.js} +11 -11
- package/dist/validation/{object.mjs → object.js} +35 -34
- package/dist/validation/{ref.mjs → ref.js} +4 -4
- package/dist/validation/{shared.mjs → shared.js} +12 -11
- package/dist/validation/{string.mjs → string.js} +32 -32
- package/dist/validation/type.js +34 -0
- package/dist/{z-schema-base.d.mts → z-schema-base.d.ts} +11 -12
- package/dist/{z-schema-base.mjs → z-schema-base.js} +45 -40
- package/dist/{z-schema-options.d.mts → z-schema-options.d.ts} +3 -3
- package/dist/{z-schema-options.mjs → z-schema-options.js} +4 -4
- package/dist/{z-schema-reader.d.mts → z-schema-reader.d.ts} +1 -1
- package/dist/{z-schema-versions.mjs → z-schema-versions.js} +21 -21
- package/dist/{z-schema.d.mts → z-schema.d.ts} +5 -13
- package/dist/{z-schema.mjs → z-schema.js} +37 -47
- package/package.json +22 -23
- package/src/errors.ts +1 -2
- package/src/format-validators.ts +139 -59
- package/src/json-schema-versions.ts +56 -2
- package/src/json-schema.ts +10 -9
- package/src/json-validation.ts +189 -146
- package/src/report.ts +37 -49
- package/src/schema-cache.ts +13 -13
- package/src/schema-compiler.ts +170 -117
- package/src/schema-validator.ts +239 -238
- package/src/utils/array.ts +9 -6
- package/src/utils/base64.ts +13 -2
- package/src/utils/clone.ts +28 -30
- package/src/utils/date.ts +6 -3
- package/src/utils/hostname.ts +27 -27
- package/src/utils/json.ts +16 -9
- package/src/utils/properties.ts +2 -2
- package/src/utils/schema-regex.ts +4 -4
- package/src/utils/time.ts +5 -5
- package/src/utils/unicode.ts +12 -5
- package/src/utils/what-is.ts +1 -5
- package/src/validation/array.ts +24 -22
- package/src/validation/combinators.ts +14 -14
- package/src/validation/numeric.ts +14 -28
- package/src/validation/object.ts +32 -36
- package/src/validation/ref.ts +5 -6
- package/src/validation/shared.ts +22 -21
- package/src/validation/string.ts +29 -39
- package/src/validation/type.ts +17 -17
- package/src/z-schema-base.ts +49 -38
- package/src/z-schema-options.ts +4 -3
- package/src/z-schema.ts +35 -45
- package/umd/ZSchema.js +711 -695
- package/umd/ZSchema.min.js +2 -2
- package/umd/package.json +3 -0
- package/dist/utils/unicode.mjs +0 -12
- package/dist/validation/type.mjs +0 -32
- /package/dist/{format-validators.d.mts → format-validators.d.ts} +0 -0
- /package/dist/{json-schema-versions.mjs → json-schema-versions.js} +0 -0
- /package/dist/schemas/{draft-04-schema.mjs → draft-04-schema.js} +0 -0
- /package/dist/schemas/{draft-06-schema.mjs → draft-06-schema.js} +0 -0
- /package/dist/schemas/{draft-07-schema.mjs → draft-07-schema.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-applicator.mjs → draft-2019-09-meta-applicator.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-content.mjs → draft-2019-09-meta-content.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-core.mjs → draft-2019-09-meta-core.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-format.mjs → draft-2019-09-meta-format.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-meta-data.mjs → draft-2019-09-meta-meta-data.js} +0 -0
- /package/dist/schemas/{draft-2019-09-meta-validation.mjs → draft-2019-09-meta-validation.js} +0 -0
- /package/dist/schemas/{draft-2019-09-schema.mjs → draft-2019-09-schema.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-applicator.mjs → draft-2020-12-meta-applicator.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-content.mjs → draft-2020-12-meta-content.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-core.mjs → draft-2020-12-meta-core.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-format-annotation.mjs → draft-2020-12-meta-format-annotation.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-format-assertion.mjs → draft-2020-12-meta-format-assertion.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-meta-data.mjs → draft-2020-12-meta-meta-data.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-unevaluated.mjs → draft-2020-12-meta-unevaluated.js} +0 -0
- /package/dist/schemas/{draft-2020-12-meta-validation.mjs → draft-2020-12-meta-validation.js} +0 -0
- /package/dist/schemas/{draft-2020-12-schema.mjs → draft-2020-12-schema.js} +0 -0
- /package/dist/utils/{constants.mjs → constants.js} +0 -0
- /package/dist/utils/{date.mjs → date.js} +0 -0
- /package/dist/utils/{properties.mjs → properties.js} +0 -0
- /package/dist/utils/{symbols.mjs → symbols.js} +0 -0
- /package/dist/utils/{uri.mjs → uri.js} +0 -0
- /package/dist/{z-schema-reader.mjs → z-schema-reader.js} +0 -0
|
@@ -1,61 +1,73 @@
|
|
|
1
|
-
import { isInteger, isObject } from "./utils/what-is.
|
|
2
|
-
import { getId } from "./json-schema.
|
|
3
|
-
import { shallowClone } from "./utils/clone.
|
|
4
|
-
import { Report } from "./report.
|
|
5
|
-
import { isFormatSupported } from "./format-validators.
|
|
6
|
-
import { compileSchemaRegex } from "./utils/schema-regex.
|
|
7
|
-
import { isUniqueArray } from "./utils/array.
|
|
8
|
-
import { validate } from "./json-validation.
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
48
|
-
if (typeof schema.pattern !== "string")
|
|
49
|
-
|
|
50
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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)
|
|
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
|
|
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
|
|
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
|
|
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
|
|
162
|
-
if (!isObject(schema.dependencies))
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
|
183
|
-
if (Array.isArray(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)
|
|
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
|
|
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 (!
|
|
201
|
-
if (isUniqueArray(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 (!
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (this.options.
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (
|
|
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
|
|
258
|
-
if (Array.isArray(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
|
|
269
|
-
if (Array.isArray(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
|
|
250
|
+
not(report, schema) {
|
|
280
251
|
const notSchema = schema.not;
|
|
281
|
-
if (!(report.options.version === "draft-04" ? isObject(notSchema) : typeof notSchema === "boolean" || isObject(notSchema)))
|
|
282
|
-
|
|
283
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
322
|
-
if (!isObject(schema.definitions))
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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
|
|
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
|
|
324
|
+
format(report, schema) {
|
|
352
325
|
if (this.options.formatAssertions === false) return;
|
|
353
|
-
if (typeof schema.format !== "string")
|
|
354
|
-
|
|
355
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
15
|
-
|
|
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.
|
|
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
|
-
|
|
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 = (
|
|
21
|
-
if (typeof
|
|
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
|
-
|
|
24
|
-
const cidx = visited.get(src);
|
|
22
|
+
const cidx = visited.get(node);
|
|
25
23
|
if (cidx !== void 0) return cloned[cidx];
|
|
26
|
-
visited.set(
|
|
27
|
-
if (Array.isArray(
|
|
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
|
-
|
|
35
|
-
|
|
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);
|