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
package/src/schema-validator.ts
CHANGED
|
@@ -10,21 +10,25 @@ import { shallowClone } from './utils/clone.js';
|
|
|
10
10
|
import { compileSchemaRegex } from './utils/schema-regex.js';
|
|
11
11
|
import { isInteger, isObject } from './utils/what-is.js';
|
|
12
12
|
|
|
13
|
+
const PRIMITIVE_TYPES = ['array', 'boolean', 'integer', 'number', 'null', 'object', 'string'] as const;
|
|
14
|
+
const PRIMITIVE_TYPE_STR = PRIMITIVE_TYPES.join(',');
|
|
15
|
+
const PRIMITIVE_TYPES_SET = new Set<string>(PRIMITIVE_TYPES);
|
|
16
|
+
|
|
13
17
|
const SchemaValidators = {
|
|
14
|
-
$ref
|
|
18
|
+
$ref(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
15
19
|
// http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07
|
|
16
20
|
// http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
|
|
17
21
|
if (typeof schema.$ref !== 'string') {
|
|
18
22
|
report.addError('KEYWORD_TYPE_EXPECTED', ['$ref', 'string'], undefined, schema, '$ref');
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
|
-
$schema
|
|
25
|
+
$schema(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
22
26
|
// http://json-schema.org/latest/json-schema-core.html#rfc.section.6
|
|
23
27
|
if (typeof schema.$schema !== 'string') {
|
|
24
28
|
report.addError('KEYWORD_TYPE_EXPECTED', ['$schema', 'string'], undefined, schema, '$schema');
|
|
25
29
|
}
|
|
26
30
|
},
|
|
27
|
-
multipleOf
|
|
31
|
+
multipleOf(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
28
32
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.1.1
|
|
29
33
|
if (typeof schema.multipleOf !== 'number') {
|
|
30
34
|
report.addError('KEYWORD_TYPE_EXPECTED', ['multipleOf', 'number'], undefined, schema, 'multipleOf');
|
|
@@ -32,13 +36,13 @@ const SchemaValidators = {
|
|
|
32
36
|
report.addError('KEYWORD_MUST_BE', ['multipleOf', 'strictly greater than 0'], undefined, schema, 'multipleOf');
|
|
33
37
|
}
|
|
34
38
|
},
|
|
35
|
-
maximum
|
|
39
|
+
maximum(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
36
40
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.2.1
|
|
37
41
|
if (typeof schema.maximum !== 'number') {
|
|
38
42
|
report.addError('KEYWORD_TYPE_EXPECTED', ['maximum', 'number'], undefined, schema, 'maximum');
|
|
39
43
|
}
|
|
40
44
|
},
|
|
41
|
-
exclusiveMaximum
|
|
45
|
+
exclusiveMaximum(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
42
46
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.2.1
|
|
43
47
|
if (report.options.version === 'draft-04') {
|
|
44
48
|
if (typeof schema.exclusiveMaximum !== 'boolean') {
|
|
@@ -52,25 +56,23 @@ const SchemaValidators = {
|
|
|
52
56
|
} else if (schema.maximum === undefined) {
|
|
53
57
|
report.addError('KEYWORD_DEPENDENCY', ['exclusiveMaximum', 'maximum'], undefined, schema, 'exclusiveMaximum');
|
|
54
58
|
}
|
|
55
|
-
} else {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
);
|
|
64
|
-
}
|
|
59
|
+
} else if (typeof schema.exclusiveMaximum !== 'boolean' && typeof schema.exclusiveMaximum !== 'number') {
|
|
60
|
+
report.addError(
|
|
61
|
+
'KEYWORD_TYPE_EXPECTED',
|
|
62
|
+
['exclusiveMaximum', ['boolean', 'number']],
|
|
63
|
+
undefined,
|
|
64
|
+
schema,
|
|
65
|
+
'exclusiveMaximum'
|
|
66
|
+
);
|
|
65
67
|
}
|
|
66
68
|
},
|
|
67
|
-
minimum
|
|
69
|
+
minimum(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
68
70
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.3.1
|
|
69
71
|
if (typeof schema.minimum !== 'number') {
|
|
70
72
|
report.addError('KEYWORD_TYPE_EXPECTED', ['minimum', 'number'], undefined, schema, 'minimum');
|
|
71
73
|
}
|
|
72
74
|
},
|
|
73
|
-
exclusiveMinimum
|
|
75
|
+
exclusiveMinimum(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
74
76
|
if (report.options.version === 'draft-04') {
|
|
75
77
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.3.1
|
|
76
78
|
if (typeof schema.exclusiveMinimum !== 'boolean') {
|
|
@@ -84,19 +86,17 @@ const SchemaValidators = {
|
|
|
84
86
|
} else if (schema.minimum === undefined) {
|
|
85
87
|
report.addError('KEYWORD_DEPENDENCY', ['exclusiveMinimum', 'minimum'], undefined, schema, 'exclusiveMinimum');
|
|
86
88
|
}
|
|
87
|
-
} else {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
);
|
|
96
|
-
}
|
|
89
|
+
} else if (typeof schema.exclusiveMinimum !== 'boolean' && typeof schema.exclusiveMinimum !== 'number') {
|
|
90
|
+
report.addError(
|
|
91
|
+
'KEYWORD_TYPE_EXPECTED',
|
|
92
|
+
['exclusiveMinimum', ['boolean', 'number']],
|
|
93
|
+
undefined,
|
|
94
|
+
schema,
|
|
95
|
+
'exclusiveMinimum'
|
|
96
|
+
);
|
|
97
97
|
}
|
|
98
98
|
},
|
|
99
|
-
maxLength
|
|
99
|
+
maxLength(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
100
100
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.1.1
|
|
101
101
|
if (!isInteger(schema.maxLength)) {
|
|
102
102
|
report.addError('KEYWORD_TYPE_EXPECTED', ['maxLength', 'integer'], undefined, schema, 'maxLength');
|
|
@@ -104,7 +104,7 @@ const SchemaValidators = {
|
|
|
104
104
|
report.addError('KEYWORD_MUST_BE', ['maxLength', 'greater than, or equal to 0'], undefined, schema, 'maxLength');
|
|
105
105
|
}
|
|
106
106
|
},
|
|
107
|
-
minLength
|
|
107
|
+
minLength(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
108
108
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.2.1
|
|
109
109
|
if (!isInteger(schema.minLength)) {
|
|
110
110
|
report.addError('KEYWORD_TYPE_EXPECTED', ['minLength', 'integer'], undefined, schema, 'minLength');
|
|
@@ -112,26 +112,26 @@ const SchemaValidators = {
|
|
|
112
112
|
report.addError('KEYWORD_MUST_BE', ['minLength', 'greater than, or equal to 0'], undefined, schema, 'minLength');
|
|
113
113
|
}
|
|
114
114
|
},
|
|
115
|
-
pattern
|
|
115
|
+
pattern(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
116
116
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.3.1
|
|
117
117
|
if (typeof schema.pattern !== 'string') {
|
|
118
118
|
report.addError('KEYWORD_TYPE_EXPECTED', ['pattern', 'string'], undefined, schema, 'pattern');
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
// Use shared regex compilation helper
|
|
122
|
+
// Import at top of file
|
|
123
|
+
const result = compileSchemaRegex(schema.pattern);
|
|
124
|
+
if (!result.ok) {
|
|
125
|
+
report.addError(
|
|
126
|
+
'KEYWORD_PATTERN',
|
|
127
|
+
['pattern', schema.pattern, result.error.message],
|
|
128
|
+
undefined,
|
|
129
|
+
schema,
|
|
130
|
+
'pattern'
|
|
131
|
+
);
|
|
132
132
|
}
|
|
133
133
|
},
|
|
134
|
-
additionalItems
|
|
134
|
+
additionalItems(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
135
135
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.1.1
|
|
136
136
|
if (typeof schema.additionalItems !== 'boolean' && !isObject(schema.additionalItems)) {
|
|
137
137
|
report.addError(
|
|
@@ -147,7 +147,7 @@ const SchemaValidators = {
|
|
|
147
147
|
report.path.pop();
|
|
148
148
|
}
|
|
149
149
|
},
|
|
150
|
-
items
|
|
150
|
+
items(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
151
151
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.1.1
|
|
152
152
|
if (Array.isArray(schema.items)) {
|
|
153
153
|
for (let idx = 0; idx < schema.items.length; idx++) {
|
|
@@ -159,7 +159,7 @@ const SchemaValidators = {
|
|
|
159
159
|
}
|
|
160
160
|
} else if (isObject(schema.items) || (report.options.version !== 'draft-04' && typeof schema.items === 'boolean')) {
|
|
161
161
|
report.path.push('items');
|
|
162
|
-
this.validateSchema(report, schema.items
|
|
162
|
+
this.validateSchema(report, schema.items);
|
|
163
163
|
report.path.pop();
|
|
164
164
|
} else {
|
|
165
165
|
report.addError(
|
|
@@ -180,7 +180,7 @@ const SchemaValidators = {
|
|
|
180
180
|
schema.additionalItems = false;
|
|
181
181
|
}
|
|
182
182
|
},
|
|
183
|
-
maxItems
|
|
183
|
+
maxItems(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
184
184
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.2.1
|
|
185
185
|
if (typeof schema.maxItems !== 'number') {
|
|
186
186
|
report.addError('KEYWORD_TYPE_EXPECTED', ['maxItems', 'integer'], undefined, schema, 'maxItems');
|
|
@@ -188,7 +188,7 @@ const SchemaValidators = {
|
|
|
188
188
|
report.addError('KEYWORD_MUST_BE', ['maxItems', 'greater than, or equal to 0'], undefined, schema, 'maxItems');
|
|
189
189
|
}
|
|
190
190
|
},
|
|
191
|
-
minItems
|
|
191
|
+
minItems(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
192
192
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.3.1
|
|
193
193
|
if (!isInteger(schema.minItems)) {
|
|
194
194
|
report.addError('KEYWORD_TYPE_EXPECTED', ['minItems', 'integer'], undefined, schema, 'minItems');
|
|
@@ -196,13 +196,13 @@ const SchemaValidators = {
|
|
|
196
196
|
report.addError('KEYWORD_MUST_BE', ['minItems', 'greater than, or equal to 0'], undefined, schema, 'minItems');
|
|
197
197
|
}
|
|
198
198
|
},
|
|
199
|
-
uniqueItems
|
|
199
|
+
uniqueItems(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
200
200
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.4.1
|
|
201
201
|
if (typeof schema.uniqueItems !== 'boolean') {
|
|
202
202
|
report.addError('KEYWORD_TYPE_EXPECTED', ['uniqueItems', 'boolean'], undefined, schema, 'uniqueItems');
|
|
203
203
|
}
|
|
204
204
|
},
|
|
205
|
-
maxProperties
|
|
205
|
+
maxProperties(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
206
206
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.1.1
|
|
207
207
|
if (!isInteger(schema.maxProperties)) {
|
|
208
208
|
report.addError('KEYWORD_TYPE_EXPECTED', ['maxProperties', 'integer'], undefined, schema, 'maxProperties');
|
|
@@ -216,7 +216,7 @@ const SchemaValidators = {
|
|
|
216
216
|
);
|
|
217
217
|
}
|
|
218
218
|
},
|
|
219
|
-
minProperties
|
|
219
|
+
minProperties(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
220
220
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.2.1
|
|
221
221
|
if (!isInteger(schema.minProperties)) {
|
|
222
222
|
report.addError('KEYWORD_TYPE_EXPECTED', ['minProperties', 'integer'], undefined, schema, 'minProperties');
|
|
@@ -230,7 +230,7 @@ const SchemaValidators = {
|
|
|
230
230
|
);
|
|
231
231
|
}
|
|
232
232
|
},
|
|
233
|
-
required
|
|
233
|
+
required(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
234
234
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.3.1
|
|
235
235
|
if (!Array.isArray(schema.required)) {
|
|
236
236
|
report.addError('KEYWORD_TYPE_EXPECTED', ['required', 'array'], undefined, schema, 'required');
|
|
@@ -248,12 +248,12 @@ const SchemaValidators = {
|
|
|
248
248
|
report.addError('KEYWORD_VALUE_TYPE', ['required', 'string'], undefined, schema, 'required');
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
|
-
if (isUniqueArray(schema.required)
|
|
251
|
+
if (!isUniqueArray(schema.required)) {
|
|
252
252
|
report.addError('KEYWORD_MUST_BE', ['required', 'an array with unique items'], undefined, schema, 'required');
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
},
|
|
256
|
-
additionalProperties
|
|
256
|
+
additionalProperties(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
257
257
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.1
|
|
258
258
|
if (typeof schema.additionalProperties !== 'boolean' && !isObject(schema.additionalProperties)) {
|
|
259
259
|
report.addError(
|
|
@@ -269,7 +269,7 @@ const SchemaValidators = {
|
|
|
269
269
|
report.path.pop();
|
|
270
270
|
}
|
|
271
271
|
},
|
|
272
|
-
properties
|
|
272
|
+
properties(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
273
273
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.1
|
|
274
274
|
if (!isObject(schema.properties)) {
|
|
275
275
|
report.addError('KEYWORD_TYPE_EXPECTED', ['properties', 'object'], undefined, schema, 'properties');
|
|
@@ -299,7 +299,7 @@ const SchemaValidators = {
|
|
|
299
299
|
report.addError('CUSTOM_MODE_FORCE_PROPERTIES', ['properties'], undefined, schema, 'properties');
|
|
300
300
|
}
|
|
301
301
|
},
|
|
302
|
-
patternProperties
|
|
302
|
+
patternProperties(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
303
303
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.1
|
|
304
304
|
if (!isObject(schema.patternProperties)) {
|
|
305
305
|
report.addError('KEYWORD_TYPE_EXPECTED', ['patternProperties', 'object'], undefined, schema, 'patternProperties');
|
|
@@ -332,158 +332,154 @@ const SchemaValidators = {
|
|
|
332
332
|
report.addError('CUSTOM_MODE_FORCE_PROPERTIES', ['patternProperties'], undefined, schema, 'patternProperties');
|
|
333
333
|
}
|
|
334
334
|
},
|
|
335
|
-
dependencies
|
|
335
|
+
dependencies(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
336
336
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.5.1
|
|
337
337
|
if (!isObject(schema.dependencies)) {
|
|
338
338
|
report.addError('KEYWORD_TYPE_EXPECTED', ['dependencies', 'object'], undefined, schema, 'dependencies');
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
if (isUniqueArray(depArray) === false) {
|
|
364
|
-
report.addError(
|
|
365
|
-
'KEYWORD_MUST_BE',
|
|
366
|
-
['dependencies', 'an array with unique items'],
|
|
367
|
-
undefined,
|
|
368
|
-
schema,
|
|
369
|
-
'dependencies'
|
|
370
|
-
);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const keys = Object.keys(schema.dependencies);
|
|
342
|
+
for (const schemaKey of keys) {
|
|
343
|
+
const schemaDependency = (schema.dependencies as Record<string, unknown>)[schemaKey];
|
|
344
|
+
const isSchemaDependency =
|
|
345
|
+
isObject(schemaDependency) || (report.options.version !== 'draft-04' && typeof schemaDependency === 'boolean');
|
|
346
|
+
|
|
347
|
+
if (isSchemaDependency) {
|
|
348
|
+
report.path.push('dependencies');
|
|
349
|
+
report.path.push(schemaKey);
|
|
350
|
+
this.validateSchema(report, schemaDependency as JsonSchemaInternal);
|
|
351
|
+
report.path.pop();
|
|
352
|
+
report.path.pop();
|
|
353
|
+
} else if (Array.isArray(schemaDependency)) {
|
|
354
|
+
const depArray = schemaDependency as string[];
|
|
355
|
+
if (report.options.version === 'draft-04' && depArray.length === 0) {
|
|
356
|
+
report.addError('KEYWORD_MUST_BE', ['dependencies', 'not empty array'], undefined, schema, 'dependencies');
|
|
357
|
+
}
|
|
358
|
+
for (const dep of depArray) {
|
|
359
|
+
if (typeof dep !== 'string') {
|
|
360
|
+
report.addError('KEYWORD_VALUE_TYPE', ['dependencies', 'string'], undefined, schema, 'dependencies');
|
|
371
361
|
}
|
|
372
|
-
}
|
|
362
|
+
}
|
|
363
|
+
if (!isUniqueArray(depArray)) {
|
|
373
364
|
report.addError(
|
|
374
|
-
'
|
|
375
|
-
['dependencies',
|
|
365
|
+
'KEYWORD_MUST_BE',
|
|
366
|
+
['dependencies', 'an array with unique items'],
|
|
376
367
|
undefined,
|
|
377
368
|
schema,
|
|
378
369
|
'dependencies'
|
|
379
370
|
);
|
|
380
371
|
}
|
|
372
|
+
} else {
|
|
373
|
+
report.addError(
|
|
374
|
+
'KEYWORD_VALUE_TYPE',
|
|
375
|
+
['dependencies', report.options.version === 'draft-04' ? 'object or array' : 'boolean, object or array'],
|
|
376
|
+
undefined,
|
|
377
|
+
schema,
|
|
378
|
+
'dependencies'
|
|
379
|
+
);
|
|
381
380
|
}
|
|
382
381
|
}
|
|
383
382
|
},
|
|
384
|
-
enum
|
|
383
|
+
enum(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
385
384
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.1.1
|
|
386
|
-
if (Array.isArray(schema.enum)
|
|
385
|
+
if (!Array.isArray(schema.enum)) {
|
|
387
386
|
report.addError('KEYWORD_TYPE_EXPECTED', ['enum', 'array'], undefined, schema, 'enum');
|
|
388
387
|
} else if (schema.enum.length === 0) {
|
|
389
388
|
report.addError('KEYWORD_MUST_BE', ['enum', 'an array with at least one element'], undefined, schema, 'enum');
|
|
390
|
-
} else if (isUniqueArray(schema.enum)
|
|
389
|
+
} else if (!isUniqueArray(schema.enum)) {
|
|
391
390
|
report.addError('KEYWORD_MUST_BE', ['enum', 'an array with unique elements'], undefined, schema, 'enum');
|
|
392
391
|
}
|
|
393
392
|
},
|
|
394
|
-
type
|
|
393
|
+
type(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
395
394
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.1
|
|
396
|
-
const primitiveTypes = ['array', 'boolean', 'integer', 'number', 'null', 'object', 'string'];
|
|
397
|
-
const primitiveTypeStr = primitiveTypes.join(',');
|
|
398
395
|
const isArray = Array.isArray(schema.type);
|
|
399
396
|
|
|
400
397
|
if (Array.isArray(schema.type)) {
|
|
401
398
|
for (const typeItem of schema.type) {
|
|
402
|
-
if (!
|
|
403
|
-
report.addError('KEYWORD_TYPE_EXPECTED', ['type',
|
|
399
|
+
if (!PRIMITIVE_TYPES_SET.has(typeItem)) {
|
|
400
|
+
report.addError('KEYWORD_TYPE_EXPECTED', ['type', PRIMITIVE_TYPE_STR], undefined, schema, 'type');
|
|
404
401
|
}
|
|
405
402
|
}
|
|
406
|
-
if (isUniqueArray(schema.type)
|
|
403
|
+
if (!isUniqueArray(schema.type)) {
|
|
407
404
|
report.addError('KEYWORD_MUST_BE', ['type', 'an object with unique properties'], undefined, schema, 'type');
|
|
408
405
|
}
|
|
409
406
|
} else if (typeof schema.type === 'string') {
|
|
410
|
-
if (!
|
|
411
|
-
report.addError('KEYWORD_TYPE_EXPECTED', ['type',
|
|
407
|
+
if (!PRIMITIVE_TYPES_SET.has(schema.type)) {
|
|
408
|
+
report.addError('KEYWORD_TYPE_EXPECTED', ['type', PRIMITIVE_TYPE_STR], undefined, schema, 'type');
|
|
412
409
|
}
|
|
413
410
|
} else {
|
|
414
411
|
report.addError('KEYWORD_TYPE_EXPECTED', ['type', ['string', 'array']], undefined, schema, 'type');
|
|
415
412
|
}
|
|
416
413
|
|
|
417
|
-
if (
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
allOf: function (this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
414
|
+
if (
|
|
415
|
+
this.options.noEmptyStrings === true &&
|
|
416
|
+
(schema.type === 'string' || (isArray && schema.type!.includes('string'))) &&
|
|
417
|
+
schema.minLength === undefined &&
|
|
418
|
+
schema.enum === undefined &&
|
|
419
|
+
schema.format === undefined
|
|
420
|
+
) {
|
|
421
|
+
schema.minLength = 1;
|
|
422
|
+
}
|
|
423
|
+
if (
|
|
424
|
+
this.options.noEmptyArrays === true &&
|
|
425
|
+
(schema.type === 'array' || (isArray && schema.type!.includes('array'))) &&
|
|
426
|
+
schema.minItems === undefined
|
|
427
|
+
) {
|
|
428
|
+
schema.minItems = 1;
|
|
429
|
+
}
|
|
430
|
+
if (
|
|
431
|
+
this.options.forceProperties === true &&
|
|
432
|
+
(schema.type === 'object' || (isArray && schema.type!.includes('object'))) &&
|
|
433
|
+
schema.properties === undefined &&
|
|
434
|
+
schema.patternProperties === undefined
|
|
435
|
+
) {
|
|
436
|
+
report.addError('KEYWORD_UNDEFINED_STRICT', ['properties'], undefined, schema, 'properties');
|
|
437
|
+
}
|
|
438
|
+
if (
|
|
439
|
+
this.options.forceItems === true &&
|
|
440
|
+
(schema.type === 'array' || (isArray && schema.type!.includes('array'))) &&
|
|
441
|
+
schema.items === undefined
|
|
442
|
+
) {
|
|
443
|
+
report.addError('KEYWORD_UNDEFINED_STRICT', ['items'], undefined, schema, 'items');
|
|
444
|
+
}
|
|
445
|
+
if (
|
|
446
|
+
this.options.forceMinItems === true &&
|
|
447
|
+
(schema.type === 'array' || (isArray && schema.type!.includes('array'))) &&
|
|
448
|
+
schema.minItems === undefined
|
|
449
|
+
) {
|
|
450
|
+
report.addError('KEYWORD_UNDEFINED_STRICT', ['minItems'], undefined, schema, 'minItems');
|
|
451
|
+
}
|
|
452
|
+
if (
|
|
453
|
+
this.options.forceMaxItems === true &&
|
|
454
|
+
(schema.type === 'array' || (isArray && schema.type!.includes('array'))) &&
|
|
455
|
+
schema.maxItems === undefined
|
|
456
|
+
) {
|
|
457
|
+
report.addError('KEYWORD_UNDEFINED_STRICT', ['maxItems'], undefined, schema, 'maxItems');
|
|
458
|
+
}
|
|
459
|
+
if (
|
|
460
|
+
this.options.forceMinLength === true &&
|
|
461
|
+
(schema.type === 'string' || (isArray && schema.type!.includes('string'))) &&
|
|
462
|
+
schema.minLength === undefined &&
|
|
463
|
+
schema.format === undefined &&
|
|
464
|
+
schema.enum === undefined &&
|
|
465
|
+
schema.pattern === undefined
|
|
466
|
+
) {
|
|
467
|
+
report.addError('KEYWORD_UNDEFINED_STRICT', ['minLength'], undefined, schema, 'minLength');
|
|
468
|
+
}
|
|
469
|
+
if (
|
|
470
|
+
this.options.forceMaxLength === true &&
|
|
471
|
+
(schema.type === 'string' || (isArray && schema.type!.includes('string'))) &&
|
|
472
|
+
schema.maxLength === undefined &&
|
|
473
|
+
schema.format === undefined &&
|
|
474
|
+
schema.enum === undefined &&
|
|
475
|
+
schema.pattern === undefined
|
|
476
|
+
) {
|
|
477
|
+
report.addError('KEYWORD_UNDEFINED_STRICT', ['maxLength'], undefined, schema, 'maxLength');
|
|
478
|
+
}
|
|
479
|
+
},
|
|
480
|
+
allOf(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
485
481
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.3.1
|
|
486
|
-
if (Array.isArray(schema.allOf)
|
|
482
|
+
if (!Array.isArray(schema.allOf)) {
|
|
487
483
|
report.addError('KEYWORD_TYPE_EXPECTED', ['allOf', 'array'], undefined, schema, 'allOf');
|
|
488
484
|
} else if (schema.allOf.length === 0) {
|
|
489
485
|
report.addError('KEYWORD_MUST_BE', ['allOf', 'an array with at least one element'], undefined, schema, 'allOf');
|
|
@@ -497,9 +493,9 @@ const SchemaValidators = {
|
|
|
497
493
|
}
|
|
498
494
|
}
|
|
499
495
|
},
|
|
500
|
-
anyOf
|
|
496
|
+
anyOf(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
501
497
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.4.1
|
|
502
|
-
if (Array.isArray(schema.anyOf)
|
|
498
|
+
if (!Array.isArray(schema.anyOf)) {
|
|
503
499
|
report.addError('KEYWORD_TYPE_EXPECTED', ['anyOf', 'array'], undefined, schema, 'anyOf');
|
|
504
500
|
} else if (schema.anyOf.length === 0) {
|
|
505
501
|
report.addError('KEYWORD_MUST_BE', ['anyOf', 'an array with at least one element'], undefined, schema, 'anyOf');
|
|
@@ -513,9 +509,9 @@ const SchemaValidators = {
|
|
|
513
509
|
}
|
|
514
510
|
}
|
|
515
511
|
},
|
|
516
|
-
oneOf
|
|
512
|
+
oneOf(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
517
513
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.5.1
|
|
518
|
-
if (Array.isArray(schema.oneOf)
|
|
514
|
+
if (!Array.isArray(schema.oneOf)) {
|
|
519
515
|
report.addError('KEYWORD_TYPE_EXPECTED', ['oneOf', 'array'], undefined, schema, 'oneOf');
|
|
520
516
|
} else if (schema.oneOf.length === 0) {
|
|
521
517
|
report.addError('KEYWORD_MUST_BE', ['oneOf', 'an array with at least one element'], undefined, schema, 'oneOf');
|
|
@@ -529,7 +525,7 @@ const SchemaValidators = {
|
|
|
529
525
|
}
|
|
530
526
|
}
|
|
531
527
|
},
|
|
532
|
-
not
|
|
528
|
+
not(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
533
529
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.6.1
|
|
534
530
|
const notSchema = schema.not;
|
|
535
531
|
const isValidNotSchema =
|
|
@@ -545,13 +541,13 @@ const SchemaValidators = {
|
|
|
545
541
|
schema,
|
|
546
542
|
'not'
|
|
547
543
|
);
|
|
548
|
-
|
|
549
|
-
report.path.push('not');
|
|
550
|
-
this.validateSchema(report, notSchema as JsonSchemaInternal);
|
|
551
|
-
report.path.pop();
|
|
544
|
+
return;
|
|
552
545
|
}
|
|
546
|
+
report.path.push('not');
|
|
547
|
+
this.validateSchema(report, notSchema!);
|
|
548
|
+
report.path.pop();
|
|
553
549
|
},
|
|
554
|
-
if
|
|
550
|
+
if(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
555
551
|
if (report.options.version !== 'draft-07') {
|
|
556
552
|
return;
|
|
557
553
|
}
|
|
@@ -564,10 +560,10 @@ const SchemaValidators = {
|
|
|
564
560
|
}
|
|
565
561
|
|
|
566
562
|
report.path.push('if');
|
|
567
|
-
this.validateSchema(report, ifSchema
|
|
563
|
+
this.validateSchema(report, ifSchema);
|
|
568
564
|
report.path.pop();
|
|
569
565
|
},
|
|
570
|
-
then
|
|
566
|
+
then(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
571
567
|
if (report.options.version !== 'draft-07') {
|
|
572
568
|
return;
|
|
573
569
|
}
|
|
@@ -580,10 +576,10 @@ const SchemaValidators = {
|
|
|
580
576
|
}
|
|
581
577
|
|
|
582
578
|
report.path.push('then');
|
|
583
|
-
this.validateSchema(report, thenSchema
|
|
579
|
+
this.validateSchema(report, thenSchema);
|
|
584
580
|
report.path.pop();
|
|
585
581
|
},
|
|
586
|
-
else
|
|
582
|
+
else(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
587
583
|
if (report.options.version !== 'draft-07') {
|
|
588
584
|
return;
|
|
589
585
|
}
|
|
@@ -596,26 +592,26 @@ const SchemaValidators = {
|
|
|
596
592
|
}
|
|
597
593
|
|
|
598
594
|
report.path.push('else');
|
|
599
|
-
this.validateSchema(report, elseSchema
|
|
595
|
+
this.validateSchema(report, elseSchema);
|
|
600
596
|
report.path.pop();
|
|
601
597
|
},
|
|
602
|
-
definitions
|
|
598
|
+
definitions(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
603
599
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.7.1
|
|
604
600
|
if (!isObject(schema.definitions)) {
|
|
605
601
|
report.addError('KEYWORD_TYPE_EXPECTED', ['definitions', 'object'], undefined, schema, 'definitions');
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
const keys = Object.keys(schema.definitions);
|
|
605
|
+
for (const key of keys) {
|
|
606
|
+
const val = schema.definitions[key];
|
|
607
|
+
report.path.push('definitions');
|
|
608
|
+
report.path.push(key);
|
|
609
|
+
this.validateSchema(report, val);
|
|
610
|
+
report.path.pop();
|
|
611
|
+
report.path.pop();
|
|
616
612
|
}
|
|
617
613
|
},
|
|
618
|
-
$defs
|
|
614
|
+
$defs(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
619
615
|
if (report.options.version !== 'draft2019-09' && report.options.version !== 'draft2020-12') {
|
|
620
616
|
return;
|
|
621
617
|
}
|
|
@@ -630,30 +626,30 @@ const SchemaValidators = {
|
|
|
630
626
|
const val = schema.$defs[key];
|
|
631
627
|
report.path.push('$defs');
|
|
632
628
|
report.path.push(key);
|
|
633
|
-
this.validateSchema(report, val
|
|
629
|
+
this.validateSchema(report, val);
|
|
634
630
|
report.path.pop();
|
|
635
631
|
report.path.pop();
|
|
636
632
|
}
|
|
637
633
|
},
|
|
638
|
-
format
|
|
634
|
+
format(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
639
635
|
if (this.options.formatAssertions === false) {
|
|
640
636
|
return;
|
|
641
637
|
}
|
|
642
638
|
|
|
643
639
|
if (typeof schema.format !== 'string') {
|
|
644
640
|
report.addError('KEYWORD_TYPE_EXPECTED', ['format', 'string'], undefined, schema, 'format');
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
const isModernDraft = this.options.version === 'draft2019-09' || this.options.version === 'draft2020-12';
|
|
644
|
+
if (
|
|
645
|
+
!isFormatSupported(schema.format, this.options.customFormats) &&
|
|
646
|
+
this.options.ignoreUnknownFormats !== true &&
|
|
647
|
+
!isModernDraft
|
|
648
|
+
) {
|
|
649
|
+
report.addError('UNKNOWN_FORMAT', [schema.format], undefined, schema, 'format');
|
|
654
650
|
}
|
|
655
651
|
},
|
|
656
|
-
contentEncoding
|
|
652
|
+
contentEncoding(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
657
653
|
if (report.options.version !== 'draft-07') {
|
|
658
654
|
return;
|
|
659
655
|
}
|
|
@@ -662,7 +658,7 @@ const SchemaValidators = {
|
|
|
662
658
|
report.addError('KEYWORD_TYPE_EXPECTED', ['contentEncoding', 'string'], undefined, schema, 'contentEncoding');
|
|
663
659
|
}
|
|
664
660
|
},
|
|
665
|
-
contentMediaType
|
|
661
|
+
contentMediaType(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
666
662
|
if (report.options.version !== 'draft-07') {
|
|
667
663
|
return;
|
|
668
664
|
}
|
|
@@ -671,32 +667,36 @@ const SchemaValidators = {
|
|
|
671
667
|
report.addError('KEYWORD_TYPE_EXPECTED', ['contentMediaType', 'string'], undefined, schema, 'contentMediaType');
|
|
672
668
|
}
|
|
673
669
|
},
|
|
674
|
-
id
|
|
670
|
+
id(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
675
671
|
// http://json-schema.org/latest/json-schema-core.html#rfc.section.7.2
|
|
676
672
|
if (typeof schema.id !== 'string') {
|
|
677
673
|
report.addError('KEYWORD_TYPE_EXPECTED', ['id', 'string'], undefined, schema, 'id');
|
|
678
674
|
}
|
|
679
675
|
},
|
|
680
|
-
title
|
|
676
|
+
title(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
681
677
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
|
|
682
678
|
if (typeof schema.title !== 'string') {
|
|
683
679
|
report.addError('KEYWORD_TYPE_EXPECTED', ['title', 'string'], undefined, schema, 'title');
|
|
684
680
|
}
|
|
685
681
|
},
|
|
686
|
-
description
|
|
682
|
+
description(this: SchemaValidator, report: Report, schema: JsonSchemaInternal) {
|
|
687
683
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
|
|
688
684
|
if (typeof schema.description !== 'string') {
|
|
689
685
|
report.addError('KEYWORD_TYPE_EXPECTED', ['description', 'string'], undefined, schema, 'description');
|
|
690
686
|
}
|
|
691
687
|
},
|
|
692
|
-
default
|
|
688
|
+
default(this: SchemaValidator) /* report, schema */ {
|
|
693
689
|
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2
|
|
694
690
|
// There are no restrictions placed on the value of this keyword.
|
|
695
691
|
},
|
|
696
692
|
} as const;
|
|
697
693
|
|
|
698
694
|
export class SchemaValidator {
|
|
699
|
-
|
|
695
|
+
private readonly validator: ZSchemaBase;
|
|
696
|
+
|
|
697
|
+
constructor(validator: ZSchemaBase) {
|
|
698
|
+
this.validator = validator;
|
|
699
|
+
}
|
|
700
700
|
|
|
701
701
|
get options() {
|
|
702
702
|
return this.validator.options;
|
|
@@ -731,35 +731,38 @@ export class SchemaValidator {
|
|
|
731
731
|
if (hasParentSchema) {
|
|
732
732
|
if (schema.__$schemaResolved && schema.__$schemaResolved !== schema) {
|
|
733
733
|
const subReport = new Report(report);
|
|
734
|
-
const valid = validate
|
|
735
|
-
if (valid
|
|
734
|
+
const valid = validate(this.validator, subReport, schema.__$schemaResolved, schema);
|
|
735
|
+
if (!valid) {
|
|
736
736
|
report.addError('PARENT_SCHEMA_VALIDATION_FAILED', undefined, subReport, schema, '$schema');
|
|
737
737
|
}
|
|
738
|
-
} else {
|
|
739
|
-
|
|
740
|
-
report.addError('REF_UNRESOLVED', [schema.$schema!], undefined, schema, '$schema');
|
|
741
|
-
}
|
|
738
|
+
} else if (this.validator.options.ignoreUnresolvableReferences !== true) {
|
|
739
|
+
report.addError('REF_UNRESOLVED', [schema.$schema!], undefined, schema, '$schema');
|
|
742
740
|
}
|
|
743
741
|
}
|
|
744
742
|
|
|
745
743
|
if (this.validator.options.noTypeless === true) {
|
|
746
744
|
// issue #36 - inherit type to anyOf, oneOf, allOf if noTypeless is defined
|
|
747
745
|
if (schema.type !== undefined) {
|
|
748
|
-
|
|
746
|
+
const inheritType = (sch: JsonSchema) => {
|
|
747
|
+
if (!sch.type) {
|
|
748
|
+
sch.type = schema.type;
|
|
749
|
+
}
|
|
750
|
+
};
|
|
749
751
|
if (Array.isArray(schema.anyOf)) {
|
|
750
|
-
|
|
752
|
+
for (let i = 0; i < schema.anyOf.length; i++) {
|
|
753
|
+
inheritType(schema.anyOf[i]);
|
|
754
|
+
}
|
|
751
755
|
}
|
|
752
756
|
if (Array.isArray(schema.oneOf)) {
|
|
753
|
-
|
|
757
|
+
for (let i = 0; i < schema.oneOf.length; i++) {
|
|
758
|
+
inheritType(schema.oneOf[i]);
|
|
759
|
+
}
|
|
754
760
|
}
|
|
755
761
|
if (Array.isArray(schema.allOf)) {
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
schemas.forEach(function (sch) {
|
|
759
|
-
if (!sch.type) {
|
|
760
|
-
sch.type = schema.type;
|
|
762
|
+
for (let i = 0; i < schema.allOf.length; i++) {
|
|
763
|
+
inheritType(schema.allOf[i]);
|
|
761
764
|
}
|
|
762
|
-
}
|
|
765
|
+
}
|
|
763
766
|
}
|
|
764
767
|
// end issue #36
|
|
765
768
|
if (
|
|
@@ -781,10 +784,8 @@ export class SchemaValidator {
|
|
|
781
784
|
}
|
|
782
785
|
if (Object.hasOwn(SchemaValidators, key)) {
|
|
783
786
|
SchemaValidators[key as keyof typeof SchemaValidators].call(this, report, schema);
|
|
784
|
-
} else if (!hasParentSchema) {
|
|
785
|
-
|
|
786
|
-
report.addError('KEYWORD_UNEXPECTED', [key], undefined, schema, undefined);
|
|
787
|
-
}
|
|
787
|
+
} else if (!hasParentSchema && this.validator.options.noExtraKeywords === true) {
|
|
788
|
+
report.addError('KEYWORD_UNEXPECTED', [key], undefined, schema);
|
|
788
789
|
}
|
|
789
790
|
}
|
|
790
791
|
|
|
@@ -798,7 +799,7 @@ export class SchemaValidator {
|
|
|
798
799
|
report.path.push('enum');
|
|
799
800
|
for (let idx = 0; idx < schema.enum.length; idx++) {
|
|
800
801
|
report.path.push(idx);
|
|
801
|
-
validate
|
|
802
|
+
validate(this.validator, report, tmpSchema, schema.enum[idx]);
|
|
802
803
|
report.path.pop();
|
|
803
804
|
}
|
|
804
805
|
report.path.pop();
|
|
@@ -806,7 +807,7 @@ export class SchemaValidator {
|
|
|
806
807
|
|
|
807
808
|
if (schema.default) {
|
|
808
809
|
report.path.push('default');
|
|
809
|
-
validate
|
|
810
|
+
validate(this.validator, report, schema, schema.default);
|
|
810
811
|
report.path.pop();
|
|
811
812
|
}
|
|
812
813
|
}
|