z-schema 12.0.4 → 12.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/z-schema +7 -2
- package/cjs/index.d.ts +541 -530
- package/cjs/index.js +3963 -7740
- package/dist/errors.d.mts +64 -0
- package/dist/errors.mjs +67 -0
- package/dist/format-validators.d.mts +14 -0
- package/dist/format-validators.mjs +261 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +7 -0
- package/dist/json-schema-versions.d.mts +137 -0
- package/dist/json-schema-versions.mjs +11 -0
- package/dist/json-schema.d.mts +61 -0
- package/dist/json-schema.mjs +53 -0
- package/dist/json-validation.mjs +441 -0
- package/dist/report.d.mts +97 -0
- package/dist/report.mjs +194 -0
- package/dist/schema-cache.d.mts +23 -0
- package/dist/schema-cache.mjs +144 -0
- package/dist/schema-compiler.d.mts +23 -0
- package/dist/schema-compiler.mjs +265 -0
- package/dist/schema-validator.d.mts +15 -0
- package/dist/schema-validator.mjs +446 -0
- package/dist/schemas/draft-04-schema.mjs +125 -0
- package/dist/schemas/draft-06-schema.mjs +126 -0
- package/dist/schemas/draft-07-schema.mjs +141 -0
- package/dist/schemas/draft-2019-09-meta-applicator.mjs +46 -0
- package/dist/schemas/draft-2019-09-meta-content.mjs +15 -0
- package/dist/schemas/draft-2019-09-meta-core.mjs +52 -0
- package/dist/schemas/draft-2019-09-meta-format.mjs +11 -0
- package/dist/schemas/draft-2019-09-meta-meta-data.mjs +31 -0
- package/dist/schemas/draft-2019-09-meta-validation.mjs +80 -0
- package/dist/schemas/draft-2019-09-schema.mjs +39 -0
- package/dist/schemas/draft-2020-12-meta-applicator.mjs +45 -0
- package/dist/schemas/draft-2020-12-meta-content.mjs +15 -0
- package/dist/schemas/draft-2020-12-meta-core.mjs +46 -0
- package/dist/schemas/draft-2020-12-meta-format-annotation.mjs +11 -0
- package/dist/schemas/draft-2020-12-meta-format-assertion.mjs +11 -0
- package/dist/schemas/draft-2020-12-meta-meta-data.mjs +31 -0
- package/dist/schemas/draft-2020-12-meta-unevaluated.mjs +14 -0
- package/dist/schemas/draft-2020-12-meta-validation.mjs +80 -0
- package/dist/schemas/draft-2020-12-schema.mjs +55 -0
- package/dist/utils/array.mjs +54 -0
- package/dist/utils/base64.mjs +21 -0
- package/dist/utils/clone.mjs +42 -0
- package/dist/utils/constants.mjs +16 -0
- package/dist/utils/date.mjs +19 -0
- package/dist/utils/hostname.mjs +73 -0
- package/dist/utils/json.mjs +34 -0
- package/dist/utils/properties.mjs +11 -0
- package/dist/utils/schema-regex.mjs +49 -0
- package/dist/utils/symbols.mjs +5 -0
- package/dist/utils/time.mjs +42 -0
- package/dist/utils/unicode.mjs +12 -0
- package/dist/utils/uri.mjs +12 -0
- package/dist/utils/what-is.mjs +23 -0
- package/dist/validation/array.mjs +57 -0
- package/dist/validation/combinators.mjs +62 -0
- package/dist/validation/numeric.mjs +38 -0
- package/dist/validation/object.mjs +109 -0
- package/dist/validation/ref.mjs +49 -0
- package/dist/validation/shared.mjs +97 -0
- package/dist/validation/string.mjs +88 -0
- package/dist/validation/type.mjs +32 -0
- package/dist/z-schema-base.d.mts +85 -0
- package/dist/z-schema-base.mjs +230 -0
- package/dist/z-schema-options.d.mts +35 -0
- package/dist/z-schema-options.mjs +55 -0
- package/dist/z-schema-reader.d.mts +6 -0
- package/dist/z-schema-reader.mjs +10 -0
- package/dist/z-schema-versions.mjs +67 -0
- package/dist/z-schema.d.mts +193 -0
- package/dist/z-schema.mjs +300 -0
- package/package.json +49 -64
- package/src/index.ts +3 -3
- package/src/schema-compiler.ts +30 -10
- package/src/schema-validator.ts +1 -1
- package/src/utils/schema-regex.ts +13 -2
- package/src/z-schema-base.ts +1 -1
- package/src/z-schema-versions.ts +1 -2
- package/src/z-schema.ts +0 -1
- package/umd/ZSchema.js +5934 -8110
- package/umd/ZSchema.min.js +2 -1
- package/dist/errors.js +0 -72
- package/dist/format-validators.js +0 -392
- package/dist/index.js +0 -5
- package/dist/json-schema-versions.js +0 -8
- package/dist/json-schema.js +0 -84
- package/dist/json-validation.js +0 -728
- package/dist/package.json +0 -3
- package/dist/report.js +0 -265
- package/dist/schema-cache.js +0 -211
- package/dist/schema-compiler.js +0 -415
- package/dist/schema-validator.js +0 -721
- package/dist/schemas/draft-04-schema.json +0 -149
- package/dist/schemas/draft-06-schema.json +0 -155
- package/dist/schemas/draft-07-schema.json +0 -172
- package/dist/schemas/draft-2019-09-meta-applicator.json +0 -52
- package/dist/schemas/draft-2019-09-meta-content.json +0 -12
- package/dist/schemas/draft-2019-09-meta-core.json +0 -53
- package/dist/schemas/draft-2019-09-meta-format.json +0 -10
- package/dist/schemas/draft-2019-09-meta-meta-data.json +0 -32
- package/dist/schemas/draft-2019-09-meta-validation.json +0 -94
- package/dist/schemas/draft-2019-09-schema.json +0 -41
- package/dist/schemas/draft-2020-12-meta-applicator.json +0 -44
- package/dist/schemas/draft-2020-12-meta-content.json +0 -12
- package/dist/schemas/draft-2020-12-meta-core.json +0 -47
- package/dist/schemas/draft-2020-12-meta-format-annotation.json +0 -10
- package/dist/schemas/draft-2020-12-meta-format-assertion.json +0 -10
- package/dist/schemas/draft-2020-12-meta-meta-data.json +0 -32
- package/dist/schemas/draft-2020-12-meta-unevaluated.json +0 -11
- package/dist/schemas/draft-2020-12-meta-validation.json +0 -94
- package/dist/schemas/draft-2020-12-schema.json +0 -57
- package/dist/types/errors.d.ts +0 -64
- package/dist/types/format-validators.d.ts +0 -11
- package/dist/types/index.d.ts +0 -14
- package/dist/types/json-schema-versions.d.ts +0 -142
- package/dist/types/json-schema.d.ts +0 -66
- package/dist/types/json-validation.d.ts +0 -6
- package/dist/types/report.d.ts +0 -94
- package/dist/types/schema-cache.d.ts +0 -27
- package/dist/types/schema-compiler.d.ts +0 -31
- package/dist/types/schema-validator.d.ts +0 -10
- package/dist/types/utils/array.d.ts +0 -9
- package/dist/types/utils/base64.d.ts +0 -2
- package/dist/types/utils/clone.d.ts +0 -2
- package/dist/types/utils/constants.d.ts +0 -19
- package/dist/types/utils/date.d.ts +0 -1
- package/dist/types/utils/hostname.d.ts +0 -2
- package/dist/types/utils/json.d.ts +0 -9
- package/dist/types/utils/properties.d.ts +0 -1
- package/dist/types/utils/schema-regex.d.ts +0 -10
- package/dist/types/utils/symbols.d.ts +0 -2
- package/dist/types/utils/time.d.ts +0 -12
- package/dist/types/utils/unicode.d.ts +0 -5
- package/dist/types/utils/uri.d.ts +0 -4
- package/dist/types/utils/what-is.d.ts +0 -4
- package/dist/types/validation/array.d.ts +0 -12
- package/dist/types/validation/combinators.d.ts +0 -10
- package/dist/types/validation/numeric.d.ts +0 -8
- package/dist/types/validation/object.d.ts +0 -13
- package/dist/types/validation/ref.d.ts +0 -11
- package/dist/types/validation/shared.d.ts +0 -26
- package/dist/types/validation/string.d.ts +0 -9
- package/dist/types/validation/type.d.ts +0 -6
- package/dist/types/z-schema-base.d.ts +0 -87
- package/dist/types/z-schema-options.d.ts +0 -35
- package/dist/types/z-schema-reader.d.ts +0 -4
- package/dist/types/z-schema-versions.d.ts +0 -1
- package/dist/types/z-schema.d.ts +0 -191
- package/dist/utils/array.js +0 -69
- package/dist/utils/base64.js +0 -29
- package/dist/utils/clone.js +0 -59
- package/dist/utils/constants.js +0 -19
- package/dist/utils/date.js +0 -21
- package/dist/utils/hostname.js +0 -146
- package/dist/utils/json.js +0 -70
- package/dist/utils/properties.js +0 -10
- package/dist/utils/schema-regex.js +0 -52
- package/dist/utils/symbols.js +0 -2
- package/dist/utils/time.js +0 -50
- package/dist/utils/unicode.js +0 -12
- package/dist/utils/uri.js +0 -15
- package/dist/utils/what-is.js +0 -32
- package/dist/validation/array.js +0 -127
- package/dist/validation/combinators.js +0 -106
- package/dist/validation/numeric.js +0 -97
- package/dist/validation/object.js +0 -237
- package/dist/validation/ref.js +0 -70
- package/dist/validation/shared.js +0 -136
- package/dist/validation/string.js +0 -170
- package/dist/validation/type.js +0 -55
- package/dist/z-schema-base.js +0 -286
- package/dist/z-schema-options.js +0 -99
- package/dist/z-schema-reader.js +0 -7
- package/dist/z-schema-versions.js +0 -110
- package/dist/z-schema.js +0 -287
package/dist/schema-compiler.js
DELETED
|
@@ -1,415 +0,0 @@
|
|
|
1
|
-
import { getId, isInternalKey, NON_SCHEMA_KEYWORDS } from './json-schema.js';
|
|
2
|
-
import { Report } from './report.js';
|
|
3
|
-
import { DEFAULT_MAX_RECURSION_DEPTH } from './utils/constants.js';
|
|
4
|
-
import { getRemotePath, isAbsoluteUri } from './utils/uri.js';
|
|
5
|
-
import { getSchemaReader } from './z-schema-reader.js';
|
|
6
|
-
/** Safely assign a property on `obj`, refusing prototype-polluting keys. */
|
|
7
|
-
function safeSetProperty(obj, key, value) {
|
|
8
|
-
const unsafeTargets = [
|
|
9
|
-
Object.prototype,
|
|
10
|
-
Function.prototype,
|
|
11
|
-
Array.prototype,
|
|
12
|
-
];
|
|
13
|
-
if (unsafeTargets.includes(obj)) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
/** Reject property names that could pollute Object.prototype (CWE-1321). */
|
|
17
|
-
if (key !== '__proto__' && key !== 'constructor' && key !== 'prototype') {
|
|
18
|
-
obj[key] = value;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
export const collectIds = (obj, maxDepth = DEFAULT_MAX_RECURSION_DEPTH) => {
|
|
22
|
-
const ids = [];
|
|
23
|
-
function walk(node, scope, _depth = 0) {
|
|
24
|
-
if (typeof node !== 'object' || node == null)
|
|
25
|
-
return;
|
|
26
|
-
if (_depth >= maxDepth) {
|
|
27
|
-
throw new Error(`Maximum recursion depth (${maxDepth}) exceeded in collectIds. ` +
|
|
28
|
-
'If your schema is deeply nested and valid, increase the maxRecursionDepth option.');
|
|
29
|
-
}
|
|
30
|
-
let addedScope = false;
|
|
31
|
-
const nodeId = getId(node);
|
|
32
|
-
if (typeof nodeId === 'string') {
|
|
33
|
-
let type = isAbsoluteUri(nodeId) ? 'absolute' : 'relative';
|
|
34
|
-
if (scope.length === 0) {
|
|
35
|
-
type = 'root';
|
|
36
|
-
}
|
|
37
|
-
const id = {
|
|
38
|
-
id: nodeId,
|
|
39
|
-
type,
|
|
40
|
-
obj: node,
|
|
41
|
-
};
|
|
42
|
-
if (type === 'absolute' || (type === 'root' && isAbsoluteUri(nodeId))) {
|
|
43
|
-
id.absoluteUri = nodeId;
|
|
44
|
-
}
|
|
45
|
-
else if (type === 'root' && typeof node.id === 'string' && isAbsoluteUri(node.id) && node.id !== nodeId) {
|
|
46
|
-
id.absoluteUri = resolveSchemaScopeId(node.id, node, nodeId);
|
|
47
|
-
}
|
|
48
|
-
else if (type === 'relative') {
|
|
49
|
-
id.absoluteParent = scope
|
|
50
|
-
.filter((x) => x.type === 'absolute' || (x.type === 'root' && x.absoluteUri))
|
|
51
|
-
.slice(-1)[0];
|
|
52
|
-
if (id.absoluteParent) {
|
|
53
|
-
const parentUri = id.absoluteParent.absoluteUri || id.absoluteParent.id;
|
|
54
|
-
id.absoluteUri = resolveSchemaScopeId(parentUri, node, id.id);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
ids.push(id);
|
|
58
|
-
scope.push(id);
|
|
59
|
-
addedScope = true;
|
|
60
|
-
}
|
|
61
|
-
if (Array.isArray(node)) {
|
|
62
|
-
for (const item of node) {
|
|
63
|
-
walk(item, scope, _depth + 1);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
for (const key of Object.keys(node)) {
|
|
68
|
-
if (isInternalKey(key) || NON_SCHEMA_KEYWORDS.includes(key))
|
|
69
|
-
continue;
|
|
70
|
-
walk(node[key], scope, _depth + 1);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
if (addedScope) {
|
|
74
|
-
scope.pop();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
walk(obj, []);
|
|
78
|
-
return ids;
|
|
79
|
-
};
|
|
80
|
-
export const collectReferences = (obj, results, scope, path, options, maxDepth = DEFAULT_MAX_RECURSION_DEPTH, _depth = 0) => {
|
|
81
|
-
results = results || [];
|
|
82
|
-
scope = scope || [];
|
|
83
|
-
path = path || [];
|
|
84
|
-
options = options || {};
|
|
85
|
-
if (typeof obj !== 'object' || obj === null) {
|
|
86
|
-
return results;
|
|
87
|
-
}
|
|
88
|
-
if (_depth >= maxDepth) {
|
|
89
|
-
throw new Error(`Maximum recursion depth (${maxDepth}) exceeded in collectReferences. ` +
|
|
90
|
-
'If your schema is deeply nested and valid, increase the maxRecursionDepth option.');
|
|
91
|
-
}
|
|
92
|
-
const hasRef = typeof obj.$ref === 'string' && typeof obj.__$refResolved === 'undefined';
|
|
93
|
-
let addedScope = false;
|
|
94
|
-
const isRootScope = scope.length === 0;
|
|
95
|
-
const objId = getId(obj);
|
|
96
|
-
let scopeId = objId;
|
|
97
|
-
if (typeof obj.id === 'string' && isAbsoluteUri(obj.id) && (!scopeId || !isAbsoluteUri(scopeId))) {
|
|
98
|
-
scopeId = obj.id;
|
|
99
|
-
}
|
|
100
|
-
if (typeof scopeId === 'string' && (isRootScope || !hasRef || options.useRefObjectScope === true)) {
|
|
101
|
-
const base = scope.length > 0 ? scope[scope.length - 1] : undefined;
|
|
102
|
-
scope.push(resolveSchemaScopeId(base, obj, scopeId));
|
|
103
|
-
addedScope = true;
|
|
104
|
-
}
|
|
105
|
-
if (hasRef) {
|
|
106
|
-
results.push({
|
|
107
|
-
ref: resolveReference(scope[scope.length - 1], obj.$ref),
|
|
108
|
-
key: '$ref',
|
|
109
|
-
obj: obj,
|
|
110
|
-
path: path.slice(0),
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
if (typeof obj.$recursiveRef === 'string' && typeof obj.__$recursiveRefResolved === 'undefined') {
|
|
114
|
-
results.push({
|
|
115
|
-
ref: resolveReference(scope[scope.length - 1], obj.$recursiveRef),
|
|
116
|
-
key: '$recursiveRef',
|
|
117
|
-
obj: obj,
|
|
118
|
-
path: path.slice(0),
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
if (typeof obj.$dynamicRef === 'string' && typeof obj.__$dynamicRefResolved === 'undefined') {
|
|
122
|
-
results.push({
|
|
123
|
-
ref: resolveReference(scope[scope.length - 1], obj.$dynamicRef),
|
|
124
|
-
key: '$dynamicRef',
|
|
125
|
-
obj: obj,
|
|
126
|
-
path: path.slice(0),
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
if (typeof obj.$schema === 'string' && typeof obj.__$schemaResolved === 'undefined') {
|
|
130
|
-
results.push({
|
|
131
|
-
ref: resolveReference(scope[scope.length - 1], obj.$schema),
|
|
132
|
-
key: '$schema',
|
|
133
|
-
obj: obj,
|
|
134
|
-
path: path.slice(0),
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
if (Array.isArray(obj)) {
|
|
138
|
-
for (let i = 0; i < obj.length; i++) {
|
|
139
|
-
path.push(i);
|
|
140
|
-
collectReferences(obj[i], results, scope, path, options, maxDepth, _depth + 1);
|
|
141
|
-
path.pop();
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
const keys = Object.keys(obj);
|
|
146
|
-
for (const key of keys) {
|
|
147
|
-
// do not recurse through resolved references and other z-schema props
|
|
148
|
-
if (isInternalKey(key) || NON_SCHEMA_KEYWORDS.includes(key)) {
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
path.push(key);
|
|
152
|
-
collectReferences(obj[key], results, scope, path, options, maxDepth, _depth + 1);
|
|
153
|
-
path.pop();
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (addedScope) {
|
|
157
|
-
scope.pop();
|
|
158
|
-
}
|
|
159
|
-
return results;
|
|
160
|
-
};
|
|
161
|
-
const resolveReference = (base, ref) => {
|
|
162
|
-
if (isAbsoluteUri(ref)) {
|
|
163
|
-
return ref;
|
|
164
|
-
}
|
|
165
|
-
const baseStr = base ?? '';
|
|
166
|
-
if (ref[0] === '#') {
|
|
167
|
-
const hashIndex = baseStr.indexOf('#');
|
|
168
|
-
const baseNoFrag = hashIndex === -1 ? baseStr : baseStr.slice(0, hashIndex);
|
|
169
|
-
return baseNoFrag + ref;
|
|
170
|
-
}
|
|
171
|
-
if (!baseStr) {
|
|
172
|
-
return ref;
|
|
173
|
-
}
|
|
174
|
-
const hashIndex = baseStr.indexOf('#');
|
|
175
|
-
const baseNoFrag = hashIndex === -1 ? baseStr : baseStr.slice(0, hashIndex);
|
|
176
|
-
if (isAbsoluteUri(baseNoFrag)) {
|
|
177
|
-
try {
|
|
178
|
-
return new URL(ref, baseNoFrag).toString();
|
|
179
|
-
}
|
|
180
|
-
catch {
|
|
181
|
-
// fall back to manual resolution below
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
let baseDir = baseNoFrag;
|
|
185
|
-
if (!baseDir.endsWith('/')) {
|
|
186
|
-
const lastSlash = baseDir.lastIndexOf('/');
|
|
187
|
-
baseDir = lastSlash === -1 ? '' : baseDir.slice(0, lastSlash + 1);
|
|
188
|
-
}
|
|
189
|
-
return baseDir + ref;
|
|
190
|
-
};
|
|
191
|
-
const isSimpleIdentifier = (id) => id[0] !== '#' && !id.includes('/') && !id.includes('.') && !id.includes('#');
|
|
192
|
-
const resolveIdScope = (base, id) => {
|
|
193
|
-
if (isAbsoluteUri(id)) {
|
|
194
|
-
return id;
|
|
195
|
-
}
|
|
196
|
-
const baseStr = base ?? '';
|
|
197
|
-
// Treat simple identifiers (no '/', '.', or '#') as same-document fragment ids
|
|
198
|
-
if (isSimpleIdentifier(id)) {
|
|
199
|
-
const hashIndex = baseStr.indexOf('#');
|
|
200
|
-
const baseNoFrag = hashIndex === -1 ? baseStr : baseStr.slice(0, hashIndex);
|
|
201
|
-
return baseNoFrag + '#' + id;
|
|
202
|
-
}
|
|
203
|
-
return resolveReference(base, id);
|
|
204
|
-
};
|
|
205
|
-
const resolveSchemaScopeId = (base, schema, id) => {
|
|
206
|
-
if (typeof schema.$id === 'string') {
|
|
207
|
-
return resolveReference(base, id);
|
|
208
|
-
}
|
|
209
|
-
return resolveIdScope(base, id);
|
|
210
|
-
};
|
|
211
|
-
export class SchemaCompiler {
|
|
212
|
-
validator;
|
|
213
|
-
constructor(validator) {
|
|
214
|
-
this.validator = validator;
|
|
215
|
-
}
|
|
216
|
-
collectAndCacheIds(schema) {
|
|
217
|
-
const ids = collectIds(schema, this.validator.options.maxRecursionDepth);
|
|
218
|
-
for (const item of ids) {
|
|
219
|
-
if (item.absoluteUri) {
|
|
220
|
-
this.validator.scache.cacheSchemaByUri(item.absoluteUri, item.obj);
|
|
221
|
-
if (item.type === 'relative' && item.absoluteParent && isSimpleIdentifier(item.id)) {
|
|
222
|
-
const parentUri = item.absoluteParent.absoluteUri || item.absoluteParent.id;
|
|
223
|
-
const altAbsoluteUri = resolveReference(parentUri, item.id);
|
|
224
|
-
this.validator.scache.cacheSchemaByUri(altAbsoluteUri, item.obj);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
else if (item.type === 'root') {
|
|
228
|
-
this.validator.scache.cacheSchemaByUri(item.id, item.obj);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
compileSchema(report, schema, options) {
|
|
233
|
-
report.commonErrorMessage = 'SCHEMA_COMPILATION_FAILED';
|
|
234
|
-
// if schema is a string, assume it's a uri
|
|
235
|
-
if (typeof schema === 'string') {
|
|
236
|
-
const loadedSchema = this.validator.scache.getSchemaByUri(report, schema);
|
|
237
|
-
if (typeof loadedSchema === 'undefined') {
|
|
238
|
-
report.addError('SCHEMA_NOT_REACHABLE', [schema]);
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
schema = loadedSchema;
|
|
242
|
-
}
|
|
243
|
-
// if schema is an array, assume it's an array of schemas
|
|
244
|
-
if (Array.isArray(schema)) {
|
|
245
|
-
if (!options?.noCache) {
|
|
246
|
-
schema.forEach((s) => this.collectAndCacheIds(s));
|
|
247
|
-
}
|
|
248
|
-
return this.compileArrayOfSchemas(report, schema);
|
|
249
|
-
}
|
|
250
|
-
else if (typeof schema === 'boolean') {
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
if (!options?.noCache) {
|
|
255
|
-
this.collectAndCacheIds(schema);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
const canMutateSchemaObject = schema !== Object.prototype &&
|
|
259
|
-
schema !== Function.prototype &&
|
|
260
|
-
schema !== Array.prototype;
|
|
261
|
-
// if we have an id than it should be cached already (if this instance has compiled it)
|
|
262
|
-
if (canMutateSchemaObject &&
|
|
263
|
-
schema.__$compiled &&
|
|
264
|
-
schema.id &&
|
|
265
|
-
this.validator.scache.checkCacheForUri(schema.id) === false) {
|
|
266
|
-
schema.__$compiled = undefined;
|
|
267
|
-
}
|
|
268
|
-
// do not re-compile schemas
|
|
269
|
-
if (schema.__$compiled) {
|
|
270
|
-
return true;
|
|
271
|
-
}
|
|
272
|
-
// v8 - if $schema is not present, set $schema to default
|
|
273
|
-
if (canMutateSchemaObject && !schema.$schema && this.validator.options.version !== 'none') {
|
|
274
|
-
schema.$schema = this.validator.getDefaultSchemaId();
|
|
275
|
-
}
|
|
276
|
-
if (schema.id && typeof schema.id === 'string' && !options?.noCache) {
|
|
277
|
-
// add this to our schemaCache (before compilation in case we have references including id)
|
|
278
|
-
this.validator.scache.cacheSchemaByUri(schema.id, schema);
|
|
279
|
-
}
|
|
280
|
-
// this method can be called recursively, so we need to remember our root
|
|
281
|
-
let isRoot = false;
|
|
282
|
-
if (!report.rootSchema) {
|
|
283
|
-
report.rootSchema = schema;
|
|
284
|
-
isRoot = true;
|
|
285
|
-
}
|
|
286
|
-
// delete all __$missingReferences from previous compilation attempts
|
|
287
|
-
const isValidExceptReferences = report.isValid();
|
|
288
|
-
if (canMutateSchemaObject) {
|
|
289
|
-
delete schema.__$missingReferences;
|
|
290
|
-
}
|
|
291
|
-
// collect all references that need to be resolved - $ref and $schema
|
|
292
|
-
const useRefObjectScope = this.validator.options.version === 'draft2019-09' || this.validator.options.version === 'draft2020-12';
|
|
293
|
-
const refs = collectReferences(schema, undefined, undefined, undefined, { useRefObjectScope }, this.validator.options.maxRecursionDepth);
|
|
294
|
-
for (const refObj of refs) {
|
|
295
|
-
// resolve all the collected references into __xxxResolved pointer
|
|
296
|
-
let response = this.validator.scache.getSchemaByUri(report, refObj.ref, schema);
|
|
297
|
-
// we can try to use custom schemaReader if available
|
|
298
|
-
if (typeof response === 'undefined') {
|
|
299
|
-
const schemaReader = getSchemaReader();
|
|
300
|
-
if (schemaReader) {
|
|
301
|
-
const remotePath = getRemotePath(refObj.ref);
|
|
302
|
-
// it's supposed to return a valid schema
|
|
303
|
-
const s = schemaReader(remotePath);
|
|
304
|
-
if (s) {
|
|
305
|
-
// it needs to have the id (cast: schemaReader returns JsonSchema, but
|
|
306
|
-
// at this pre-compilation stage we treat it as an internal object)
|
|
307
|
-
s.id = remotePath;
|
|
308
|
-
// try to compile the schema
|
|
309
|
-
const subreport = new Report(report);
|
|
310
|
-
if (!this.compileSchema(subreport, s)) {
|
|
311
|
-
// copy errors to report
|
|
312
|
-
report.errors = report.errors.concat(subreport.errors);
|
|
313
|
-
}
|
|
314
|
-
else {
|
|
315
|
-
response = this.validator.scache.getSchemaByUri(report, refObj.ref, schema);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
if (typeof response === 'undefined') {
|
|
321
|
-
const hasNotValid = report.hasError('REMOTE_NOT_VALID', [refObj.ref]);
|
|
322
|
-
const isAbsolute = isAbsoluteUri(refObj.ref);
|
|
323
|
-
let isDownloaded = false;
|
|
324
|
-
const ignoreUnresolvableRemotes = this.validator.options.ignoreUnresolvableReferences === true;
|
|
325
|
-
if (isAbsolute) {
|
|
326
|
-
// we shouldn't add UNRESOLVABLE_REFERENCE for schemas we already have downloaded
|
|
327
|
-
// and set through setRemoteReference method
|
|
328
|
-
isDownloaded = this.validator.scache.checkCacheForUri(refObj.ref);
|
|
329
|
-
}
|
|
330
|
-
if (hasNotValid) {
|
|
331
|
-
// already has REMOTE_NOT_VALID error for this one
|
|
332
|
-
}
|
|
333
|
-
else if (ignoreUnresolvableRemotes && isAbsolute) {
|
|
334
|
-
// ignoreUnresolvableRemotes is on and remote isAbsolute
|
|
335
|
-
}
|
|
336
|
-
else if (isDownloaded) {
|
|
337
|
-
// remote is downloaded, so no UNRESOLVABLE_REFERENCE
|
|
338
|
-
}
|
|
339
|
-
else {
|
|
340
|
-
report.path.push(...refObj.path);
|
|
341
|
-
report.addError('UNRESOLVABLE_REFERENCE', [refObj.ref]);
|
|
342
|
-
report.path = report.path.slice(0, -refObj.path.length);
|
|
343
|
-
// pusblish unresolved references out
|
|
344
|
-
if (isValidExceptReferences && canMutateSchemaObject) {
|
|
345
|
-
schema.__$missingReferences = schema.__$missingReferences || [];
|
|
346
|
-
schema.__$missingReferences.push(refObj);
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
// this might create circular references
|
|
351
|
-
safeSetProperty(refObj.obj, `__${refObj.key}Resolved`, response);
|
|
352
|
-
}
|
|
353
|
-
const isValid = report.isValid();
|
|
354
|
-
if (isValid && canMutateSchemaObject) {
|
|
355
|
-
schema.__$compiled = true;
|
|
356
|
-
}
|
|
357
|
-
// else {
|
|
358
|
-
// if (schema.id && typeof schema.id === 'string') {
|
|
359
|
-
// console.log(report.errors);
|
|
360
|
-
// // remove this schema from schemaCache because it failed to compile
|
|
361
|
-
// this.validator.scache.removeFromCacheByUri(schema.id);
|
|
362
|
-
// }
|
|
363
|
-
// }
|
|
364
|
-
// we don't need the root pointer anymore
|
|
365
|
-
if (isRoot) {
|
|
366
|
-
report.rootSchema = undefined;
|
|
367
|
-
}
|
|
368
|
-
return isValid;
|
|
369
|
-
}
|
|
370
|
-
compileArrayOfSchemas(report, arr) {
|
|
371
|
-
let compiled = 0, lastLoopCompiled;
|
|
372
|
-
do {
|
|
373
|
-
// remove all UNRESOLVABLE_REFERENCE errors before compiling array again
|
|
374
|
-
report.errors = report.errors.filter((e) => e.code !== 'UNRESOLVABLE_REFERENCE');
|
|
375
|
-
// remember how many were compiled in the last loop
|
|
376
|
-
lastLoopCompiled = compiled;
|
|
377
|
-
// count how many are compiled now
|
|
378
|
-
compiled = this.compileArrayOfSchemasLoop(report, arr);
|
|
379
|
-
// fix __$missingReferences if possible
|
|
380
|
-
for (const sch of arr) {
|
|
381
|
-
if (sch.__$missingReferences) {
|
|
382
|
-
for (let idx2 = sch.__$missingReferences.length - 1; idx2 >= 0; idx2--) {
|
|
383
|
-
const refObj = sch.__$missingReferences[idx2];
|
|
384
|
-
const response = arr.find((x) => x.id === refObj.ref);
|
|
385
|
-
if (response) {
|
|
386
|
-
// this might create circular references
|
|
387
|
-
safeSetProperty(refObj.obj, `__${refObj.key}Resolved`, response);
|
|
388
|
-
// it's resolved now so delete it
|
|
389
|
-
sch.__$missingReferences.splice(idx2, 1);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
if (sch.__$missingReferences.length === 0) {
|
|
393
|
-
delete sch.__$missingReferences;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
// keep repeating if not all compiled and at least one more was compiled in the last loop
|
|
398
|
-
} while (compiled !== arr.length && compiled !== lastLoopCompiled);
|
|
399
|
-
return report.isValid();
|
|
400
|
-
}
|
|
401
|
-
compileArrayOfSchemasLoop(mainReport, arr) {
|
|
402
|
-
let compiledCount = 0;
|
|
403
|
-
for (const schema of arr) {
|
|
404
|
-
// try to compile each schema separately
|
|
405
|
-
const report = new Report(mainReport);
|
|
406
|
-
const isValid = this.compileSchema(report, schema);
|
|
407
|
-
if (isValid) {
|
|
408
|
-
compiledCount++;
|
|
409
|
-
}
|
|
410
|
-
// copy errors to report
|
|
411
|
-
mainReport.errors = mainReport.errors.concat(report.errors);
|
|
412
|
-
}
|
|
413
|
-
return compiledCount;
|
|
414
|
-
}
|
|
415
|
-
}
|