z-schema 7.0.0 → 7.0.6
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 +7 -11
- package/bin/z-schema +0 -0
- package/cjs/index.d.ts +192 -117
- package/cjs/index.js +949 -998
- package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
- package/dist/index.js +1 -1
- package/dist/json-schema.js +40 -0
- package/dist/{JsonValidation.js → json-validation.js} +75 -69
- package/dist/{Report.js → report.js} +35 -45
- package/dist/schema-cache.js +109 -0
- package/dist/schema-compiler.js +255 -0
- package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
- package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
- package/dist/types/format-validators.d.ts +10 -0
- package/dist/types/index.d.ts +10 -1
- package/dist/types/json-schema.d.ts +50 -0
- package/dist/types/json-validation.d.ts +7 -0
- package/dist/types/{Report.d.ts → report.d.ts} +22 -23
- package/dist/types/schema-cache.d.ts +17 -0
- package/dist/types/schema-compiler.d.ts +16 -0
- package/dist/types/schema-validator.d.ts +10 -0
- package/dist/types/utils/array.d.ts +2 -0
- package/dist/types/utils/clone.d.ts +2 -0
- package/dist/types/utils/json.d.ts +7 -0
- package/dist/types/utils/symbols.d.ts +2 -0
- package/dist/types/utils/unicode.d.ts +14 -0
- package/dist/types/utils/uri.d.ts +4 -0
- package/dist/types/utils/what-is.d.ts +3 -0
- package/dist/types/z-schema.d.ts +75 -0
- package/dist/utils/array.js +27 -0
- package/dist/utils/clone.js +61 -0
- package/dist/utils/json.js +59 -0
- package/dist/utils/symbols.js +2 -0
- package/dist/utils/unicode.js +45 -0
- package/dist/utils/uri.js +15 -0
- package/dist/utils/what-is.js +29 -0
- package/dist/{ZSchema.js → z-schema.js} +66 -77
- package/package.json +8 -4
- package/src/{Errors.ts → errors.ts} +4 -0
- package/src/format-validators.ts +191 -0
- package/src/index.ts +12 -1
- package/src/json-schema.ts +97 -0
- package/src/{JsonValidation.ts → json-validation.ts} +137 -127
- package/src/{Report.ts → report.ts} +60 -70
- package/src/schema-cache.ts +122 -0
- package/src/schema-compiler.ts +300 -0
- package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
- package/src/utils/array.ts +29 -0
- package/src/utils/clone.ts +63 -0
- package/src/utils/json.ts +74 -0
- package/src/utils/symbols.ts +3 -0
- package/src/utils/unicode.ts +43 -0
- package/src/utils/uri.ts +18 -0
- package/src/utils/what-is.ts +46 -0
- package/src/{ZSchema.ts → z-schema.ts} +108 -113
- package/umd/ZSchema.js +949 -998
- package/umd/ZSchema.min.js +1 -1
- package/dist/FormatValidators.js +0 -136
- package/dist/SchemaCache.js +0 -173
- package/dist/SchemaCompilation.js +0 -259
- package/dist/Utils.js +0 -266
- package/dist/types/FormatValidators.d.ts +0 -12
- package/dist/types/JsonValidation.d.ts +0 -37
- package/dist/types/SchemaCache.d.ts +0 -26
- package/dist/types/SchemaCompilation.d.ts +0 -1
- package/dist/types/SchemaValidation.d.ts +0 -6
- package/dist/types/Utils.d.ts +0 -64
- package/dist/types/ZSchema.d.ts +0 -97
- package/src/SchemaCache.ts +0 -189
- package/src/SchemaCompilation.ts +0 -293
- package/src/Utils.ts +0 -286
- /package/dist/{Errors.js → errors.js} +0 -0
- /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
- /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/src/schemas/{schema.json → draft-04-schema.json} +0 -0
package/dist/FormatValidators.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import validator from 'validator';
|
|
2
|
-
export const FormatValidators = {
|
|
3
|
-
date: function (date) {
|
|
4
|
-
if (typeof date !== 'string') {
|
|
5
|
-
return true;
|
|
6
|
-
}
|
|
7
|
-
// full-date from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
8
|
-
const matches = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/.exec(date);
|
|
9
|
-
if (matches === null) {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
// var year = matches[1];
|
|
13
|
-
// var month = matches[2];
|
|
14
|
-
// var day = matches[3];
|
|
15
|
-
if (matches[2] < '01' || matches[2] > '12' || matches[3] < '01' || matches[3] > '31') {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
return true;
|
|
19
|
-
},
|
|
20
|
-
'date-time': function (dateTime) {
|
|
21
|
-
if (typeof dateTime !== 'string') {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
// date-time from http://tools.ietf.org/html/rfc3339#section-5.6
|
|
25
|
-
const s = dateTime.toLowerCase().split('t');
|
|
26
|
-
if (!FormatValidators.date(s[0])) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
const matches = /^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$/.exec(s[1]);
|
|
30
|
-
if (matches === null) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
// var hour = matches[1];
|
|
34
|
-
// var minute = matches[2];
|
|
35
|
-
// var second = matches[3];
|
|
36
|
-
// var fraction = matches[4];
|
|
37
|
-
// var timezone = matches[5];
|
|
38
|
-
if (matches[1] > '23' || matches[2] > '59' || matches[3] > '59') {
|
|
39
|
-
return false;
|
|
40
|
-
}
|
|
41
|
-
return true;
|
|
42
|
-
},
|
|
43
|
-
email: function (email) {
|
|
44
|
-
if (typeof email !== 'string') {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return validator.isEmail(email, { require_tld: true });
|
|
48
|
-
},
|
|
49
|
-
hostname: function (hostname) {
|
|
50
|
-
if (typeof hostname !== 'string') {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
/*
|
|
54
|
-
http://json-schema.org/latest/json-schema-validation.html#anchor114
|
|
55
|
-
A string instance is valid against this attribute if it is a valid
|
|
56
|
-
representation for an Internet host name, as defined by RFC 1034, section 3.1 [RFC1034].
|
|
57
|
-
|
|
58
|
-
http://tools.ietf.org/html/rfc1034#section-3.5
|
|
59
|
-
|
|
60
|
-
<digit> ::= any one of the ten digits 0 through 9
|
|
61
|
-
var digit = /[0-9]/;
|
|
62
|
-
|
|
63
|
-
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
|
|
64
|
-
var letter = /[a-zA-Z]/;
|
|
65
|
-
|
|
66
|
-
<let-dig> ::= <letter> | <digit>
|
|
67
|
-
var letDig = /[0-9a-zA-Z]/;
|
|
68
|
-
|
|
69
|
-
<let-dig-hyp> ::= <let-dig> | "-"
|
|
70
|
-
var letDigHyp = /[-0-9a-zA-Z]/;
|
|
71
|
-
|
|
72
|
-
<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
|
|
73
|
-
var ldhStr = /[-0-9a-zA-Z]+/;
|
|
74
|
-
|
|
75
|
-
<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
|
|
76
|
-
var label = /[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?/;
|
|
77
|
-
|
|
78
|
-
<subdomain> ::= <label> | <subdomain> "." <label>
|
|
79
|
-
var subdomain = /^[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?(\.[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?)*$/;
|
|
80
|
-
|
|
81
|
-
<domain> ::= <subdomain> | " "
|
|
82
|
-
var domain = null;
|
|
83
|
-
*/
|
|
84
|
-
const valid = /^[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?(\.[a-zA-Z](([-0-9a-zA-Z]+)?[0-9a-zA-Z])?)*$/.test(hostname);
|
|
85
|
-
if (valid) {
|
|
86
|
-
// the sum of all label octets and label lengths is limited to 255.
|
|
87
|
-
if (hostname.length > 255) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
// Each node has a label, which is zero to 63 octets in length
|
|
91
|
-
const labels = hostname.split('.');
|
|
92
|
-
for (let i = 0; i < labels.length; i++) {
|
|
93
|
-
if (labels[i].length > 63) {
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return valid;
|
|
99
|
-
},
|
|
100
|
-
'host-name': function (hostname) {
|
|
101
|
-
return FormatValidators.hostname.call(this, hostname);
|
|
102
|
-
},
|
|
103
|
-
ipv4: function (ipv4) {
|
|
104
|
-
if (typeof ipv4 !== 'string') {
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
return validator.isIP(ipv4, 4);
|
|
108
|
-
},
|
|
109
|
-
ipv6: function (ipv6) {
|
|
110
|
-
if (typeof ipv6 !== 'string') {
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
return validator.isIP(ipv6, 6);
|
|
114
|
-
},
|
|
115
|
-
regex: function (str) {
|
|
116
|
-
try {
|
|
117
|
-
RegExp(str);
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
catch (_e) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
uri: function (...args) {
|
|
125
|
-
if (this.options.strictUris) {
|
|
126
|
-
return FormatValidators['strict-uri'].apply(this, args);
|
|
127
|
-
}
|
|
128
|
-
const [uri] = args;
|
|
129
|
-
// https://github.com/zaggino/z-schema/issues/18
|
|
130
|
-
// RegExp from http://tools.ietf.org/html/rfc3986#appendix-B
|
|
131
|
-
return typeof uri !== 'string' || RegExp('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?').test(uri);
|
|
132
|
-
},
|
|
133
|
-
'strict-uri': function (uri) {
|
|
134
|
-
return typeof uri !== 'string' || validator.isURL(uri);
|
|
135
|
-
},
|
|
136
|
-
};
|
package/dist/SchemaCache.js
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import isequal from 'lodash.isequal';
|
|
2
|
-
import { Report } from './Report.js';
|
|
3
|
-
import { compileSchema } from './SchemaCompilation.js';
|
|
4
|
-
import * as SchemaValidation from './SchemaValidation.js';
|
|
5
|
-
import * as Utils from './Utils.js';
|
|
6
|
-
function decodeJSONPointer(str) {
|
|
7
|
-
// http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07#section-3
|
|
8
|
-
return decodeURIComponent(str).replace(/~[0-1]/g, function (x) {
|
|
9
|
-
return x === '~1' ? '/' : '~';
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
export function getRemotePath(uri) {
|
|
13
|
-
const io = uri.indexOf('#');
|
|
14
|
-
return io === -1 ? uri : uri.slice(0, io);
|
|
15
|
-
}
|
|
16
|
-
function getQueryPath(uri) {
|
|
17
|
-
const io = uri.indexOf('#');
|
|
18
|
-
const res = io === -1 ? undefined : uri.slice(io + 1);
|
|
19
|
-
// WARN: do not slice slash, #/ means take root and go down from it
|
|
20
|
-
// if (res && res[0] === "/") { res = res.slice(1); }
|
|
21
|
-
return res;
|
|
22
|
-
}
|
|
23
|
-
function findId(schema, id) {
|
|
24
|
-
// process only arrays and objects
|
|
25
|
-
if (typeof schema !== 'object' || schema === null) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
// no id means root so return itself
|
|
29
|
-
if (!id) {
|
|
30
|
-
return schema;
|
|
31
|
-
}
|
|
32
|
-
if (schema.id) {
|
|
33
|
-
if (schema.id === id || (schema.id[0] === '#' && schema.id.substring(1) === id)) {
|
|
34
|
-
return schema;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
let idx, result;
|
|
38
|
-
if (Array.isArray(schema)) {
|
|
39
|
-
idx = schema.length;
|
|
40
|
-
while (idx--) {
|
|
41
|
-
result = findId(schema[idx], id);
|
|
42
|
-
if (result) {
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
const keys = Object.keys(schema);
|
|
49
|
-
idx = keys.length;
|
|
50
|
-
while (idx--) {
|
|
51
|
-
const k = keys[idx];
|
|
52
|
-
if (k.indexOf('__$') === 0) {
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
result = findId(schema[k], id);
|
|
56
|
-
if (result) {
|
|
57
|
-
return result;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
*
|
|
64
|
-
* @param {*} uri
|
|
65
|
-
* @param {*} schema
|
|
66
|
-
*
|
|
67
|
-
* @returns {void}
|
|
68
|
-
*/
|
|
69
|
-
export function cacheSchemaByUri(uri, schema) {
|
|
70
|
-
const remotePath = getRemotePath(uri);
|
|
71
|
-
if (remotePath) {
|
|
72
|
-
this.cache[remotePath] = schema;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
*
|
|
77
|
-
* @param {*} uri
|
|
78
|
-
*
|
|
79
|
-
* @returns {void}
|
|
80
|
-
*/
|
|
81
|
-
export function removeFromCacheByUri(uri) {
|
|
82
|
-
const remotePath = getRemotePath(uri);
|
|
83
|
-
if (remotePath) {
|
|
84
|
-
delete this.cache[remotePath];
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
*
|
|
89
|
-
* @param {*} uri
|
|
90
|
-
*
|
|
91
|
-
* @returns {boolean}
|
|
92
|
-
*/
|
|
93
|
-
export function checkCacheForUri(uri) {
|
|
94
|
-
const remotePath = getRemotePath(uri);
|
|
95
|
-
return remotePath ? this.cache[remotePath] != null : false;
|
|
96
|
-
}
|
|
97
|
-
export function getSchema(report, schema) {
|
|
98
|
-
if (typeof schema === 'object') {
|
|
99
|
-
schema = getSchemaByReference.call(this, report, schema);
|
|
100
|
-
}
|
|
101
|
-
if (typeof schema === 'string') {
|
|
102
|
-
schema = getSchemaByUri.call(this, report, schema);
|
|
103
|
-
}
|
|
104
|
-
return schema;
|
|
105
|
-
}
|
|
106
|
-
export function getSchemaByReference(report, key) {
|
|
107
|
-
let i = this.referenceCache.length;
|
|
108
|
-
while (i--) {
|
|
109
|
-
if (isequal(this.referenceCache[i][0], key)) {
|
|
110
|
-
return this.referenceCache[i][1];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
// not found
|
|
114
|
-
const schema = Utils.cloneDeep(key);
|
|
115
|
-
this.referenceCache.push([key, schema]);
|
|
116
|
-
return schema;
|
|
117
|
-
}
|
|
118
|
-
export function getSchemaByUri(report, uri, root) {
|
|
119
|
-
const remotePath = getRemotePath(uri);
|
|
120
|
-
const queryPath = getQueryPath(uri);
|
|
121
|
-
let result = remotePath ? this.cache[remotePath] : root;
|
|
122
|
-
if (result && remotePath) {
|
|
123
|
-
// we need to avoid compiling schemas in a recursive loop
|
|
124
|
-
const compileRemote = result !== root;
|
|
125
|
-
// now we need to compile and validate resolved schema (in case it's not already)
|
|
126
|
-
if (compileRemote) {
|
|
127
|
-
report.path.push(remotePath);
|
|
128
|
-
let remoteReport;
|
|
129
|
-
const anscestorReport = report.getAncestor(result.id);
|
|
130
|
-
if (anscestorReport) {
|
|
131
|
-
remoteReport = anscestorReport;
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
remoteReport = new Report(report);
|
|
135
|
-
if (compileSchema.call(this, remoteReport, result)) {
|
|
136
|
-
const savedOptions = this.options;
|
|
137
|
-
try {
|
|
138
|
-
// If custom validationOptions were provided to setRemoteReference(),
|
|
139
|
-
// use them instead of the default options
|
|
140
|
-
this.options = result.__$validationOptions || this.options;
|
|
141
|
-
SchemaValidation.validateSchema.call(this, remoteReport, result);
|
|
142
|
-
}
|
|
143
|
-
finally {
|
|
144
|
-
this.options = savedOptions;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
const remoteReportIsValid = remoteReport.isValid();
|
|
149
|
-
if (!remoteReportIsValid) {
|
|
150
|
-
report.addError('REMOTE_NOT_VALID', [uri], remoteReport);
|
|
151
|
-
}
|
|
152
|
-
report.path.pop();
|
|
153
|
-
if (!remoteReportIsValid) {
|
|
154
|
-
return undefined;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
if (result && queryPath) {
|
|
159
|
-
const parts = queryPath.split('/');
|
|
160
|
-
for (let idx = 0, lim = parts.length; result && idx < lim; idx++) {
|
|
161
|
-
const key = decodeJSONPointer(parts[idx]);
|
|
162
|
-
if (idx === 0) {
|
|
163
|
-
// it's an id
|
|
164
|
-
result = findId(result, key);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
// it's a path behind id
|
|
168
|
-
result = result[key];
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return result;
|
|
173
|
-
}
|
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
import { Report } from './Report.js';
|
|
2
|
-
import { getSchemaByUri, checkCacheForUri, cacheSchemaByUri, removeFromCacheByUri } from './SchemaCache.js';
|
|
3
|
-
import * as Utils from './Utils.js';
|
|
4
|
-
function mergeReference(scope, ref) {
|
|
5
|
-
if (Utils.isAbsoluteUri(ref)) {
|
|
6
|
-
return ref;
|
|
7
|
-
}
|
|
8
|
-
let joinedScope = scope.join('');
|
|
9
|
-
const isScopeAbsolute = Utils.isAbsoluteUri(joinedScope);
|
|
10
|
-
const isScopeRelative = Utils.isRelativeUri(joinedScope);
|
|
11
|
-
const isRefRelative = Utils.isRelativeUri(ref);
|
|
12
|
-
let toRemove;
|
|
13
|
-
if (isScopeAbsolute && isRefRelative) {
|
|
14
|
-
toRemove = joinedScope.match(/\/[^/]*$/);
|
|
15
|
-
if (toRemove) {
|
|
16
|
-
joinedScope = joinedScope.slice(0, toRemove.index + 1);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
else if (isScopeRelative && isRefRelative) {
|
|
20
|
-
joinedScope = '';
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
toRemove = joinedScope.match(/[^#/]+$/);
|
|
24
|
-
if (toRemove) {
|
|
25
|
-
joinedScope = joinedScope.slice(0, toRemove.index);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
let res = joinedScope + ref;
|
|
29
|
-
res = res.replace(/##/, '#');
|
|
30
|
-
return res;
|
|
31
|
-
}
|
|
32
|
-
function collectReferences(obj, results, scope, path) {
|
|
33
|
-
results = results || [];
|
|
34
|
-
scope = scope || [];
|
|
35
|
-
path = path || [];
|
|
36
|
-
if (typeof obj !== 'object' || obj === null) {
|
|
37
|
-
return results;
|
|
38
|
-
}
|
|
39
|
-
if (typeof obj.id === 'string') {
|
|
40
|
-
scope.push(obj.id);
|
|
41
|
-
}
|
|
42
|
-
if (typeof obj.$ref === 'string' && typeof obj.__$refResolved === 'undefined') {
|
|
43
|
-
results.push({
|
|
44
|
-
ref: mergeReference(scope, obj.$ref),
|
|
45
|
-
key: '$ref',
|
|
46
|
-
obj: obj,
|
|
47
|
-
path: path.slice(0),
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
if (typeof obj.$schema === 'string' && typeof obj.__$schemaResolved === 'undefined') {
|
|
51
|
-
results.push({
|
|
52
|
-
ref: mergeReference(scope, obj.$schema),
|
|
53
|
-
key: '$schema',
|
|
54
|
-
obj: obj,
|
|
55
|
-
path: path.slice(0),
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
let idx;
|
|
59
|
-
if (Array.isArray(obj)) {
|
|
60
|
-
idx = obj.length;
|
|
61
|
-
while (idx--) {
|
|
62
|
-
path.push(idx.toString());
|
|
63
|
-
collectReferences(obj[idx], results, scope, path);
|
|
64
|
-
path.pop();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
const keys = Object.keys(obj);
|
|
69
|
-
idx = keys.length;
|
|
70
|
-
while (idx--) {
|
|
71
|
-
// do not recurse through resolved references and other z-schema props
|
|
72
|
-
if (keys[idx].indexOf('__$') === 0) {
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
path.push(keys[idx]);
|
|
76
|
-
collectReferences(obj[keys[idx]], results, scope, path);
|
|
77
|
-
path.pop();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (typeof obj.id === 'string') {
|
|
81
|
-
scope.pop();
|
|
82
|
-
}
|
|
83
|
-
return results;
|
|
84
|
-
}
|
|
85
|
-
const compileArrayOfSchemasLoop = function (mainReport, arr) {
|
|
86
|
-
let idx = arr.length, compiledCount = 0;
|
|
87
|
-
while (idx--) {
|
|
88
|
-
// try to compile each schema separately
|
|
89
|
-
const report = new Report(mainReport);
|
|
90
|
-
const isValid = compileSchema.call(this, report, arr[idx]);
|
|
91
|
-
if (isValid) {
|
|
92
|
-
compiledCount++;
|
|
93
|
-
}
|
|
94
|
-
// copy errors to report
|
|
95
|
-
mainReport.errors = mainReport.errors.concat(report.errors);
|
|
96
|
-
}
|
|
97
|
-
return compiledCount;
|
|
98
|
-
};
|
|
99
|
-
function findId(arr, id) {
|
|
100
|
-
let idx = arr.length;
|
|
101
|
-
while (idx--) {
|
|
102
|
-
if (arr[idx].id === id) {
|
|
103
|
-
return arr[idx];
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
const compileArrayOfSchemas = function (report, arr) {
|
|
109
|
-
let compiled = 0, lastLoopCompiled;
|
|
110
|
-
do {
|
|
111
|
-
// remove all UNRESOLVABLE_REFERENCE errors before compiling array again
|
|
112
|
-
let idx = report.errors.length;
|
|
113
|
-
while (idx--) {
|
|
114
|
-
if (report.errors[idx].code === 'UNRESOLVABLE_REFERENCE') {
|
|
115
|
-
report.errors.splice(idx, 1);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
// remember how many were compiled in the last loop
|
|
119
|
-
lastLoopCompiled = compiled;
|
|
120
|
-
// count how many are compiled now
|
|
121
|
-
compiled = compileArrayOfSchemasLoop.call(this, report, arr);
|
|
122
|
-
// fix __$missingReferences if possible
|
|
123
|
-
idx = arr.length;
|
|
124
|
-
while (idx--) {
|
|
125
|
-
const sch = arr[idx];
|
|
126
|
-
if (sch.__$missingReferences) {
|
|
127
|
-
let idx2 = sch.__$missingReferences.length;
|
|
128
|
-
while (idx2--) {
|
|
129
|
-
const refObj = sch.__$missingReferences[idx2];
|
|
130
|
-
const response = findId(arr, refObj.ref);
|
|
131
|
-
if (response) {
|
|
132
|
-
// this might create circular references
|
|
133
|
-
refObj.obj['__' + refObj.key + 'Resolved'] = response;
|
|
134
|
-
// it's resolved now so delete it
|
|
135
|
-
sch.__$missingReferences.splice(idx2, 1);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
if (sch.__$missingReferences.length === 0) {
|
|
139
|
-
delete sch.__$missingReferences;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
// keep repeating if not all compiled and at least one more was compiled in the last loop
|
|
144
|
-
} while (compiled !== arr.length && compiled !== lastLoopCompiled);
|
|
145
|
-
return report.isValid();
|
|
146
|
-
};
|
|
147
|
-
export function compileSchema(report, schema) {
|
|
148
|
-
report.commonErrorMessage = 'SCHEMA_COMPILATION_FAILED';
|
|
149
|
-
// if schema is a string, assume it's a uri
|
|
150
|
-
if (typeof schema === 'string') {
|
|
151
|
-
const loadedSchema = getSchemaByUri.call(this, report, schema);
|
|
152
|
-
if (!loadedSchema) {
|
|
153
|
-
report.addError('SCHEMA_NOT_REACHABLE', [schema]);
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
schema = loadedSchema;
|
|
157
|
-
}
|
|
158
|
-
// if schema is an array, assume it's an array of schemas
|
|
159
|
-
if (Array.isArray(schema)) {
|
|
160
|
-
return compileArrayOfSchemas.call(this, report, schema);
|
|
161
|
-
}
|
|
162
|
-
// if we have an id than it should be cached already (if this instance has compiled it)
|
|
163
|
-
if (schema.__$compiled && schema.id && checkCacheForUri.call(this, schema.id) === false) {
|
|
164
|
-
schema.__$compiled = undefined;
|
|
165
|
-
}
|
|
166
|
-
// do not re-compile schemas
|
|
167
|
-
if (schema.__$compiled) {
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
if (schema.id && typeof schema.id === 'string') {
|
|
171
|
-
// add this to our schemaCache (before compilation in case we have references including id)
|
|
172
|
-
cacheSchemaByUri.call(this, schema.id, schema);
|
|
173
|
-
}
|
|
174
|
-
// this method can be called recursively, so we need to remember our root
|
|
175
|
-
let isRoot = false;
|
|
176
|
-
if (!report.rootSchema) {
|
|
177
|
-
report.rootSchema = schema;
|
|
178
|
-
isRoot = true;
|
|
179
|
-
}
|
|
180
|
-
// delete all __$missingReferences from previous compilation attempts
|
|
181
|
-
const isValidExceptReferences = report.isValid();
|
|
182
|
-
delete schema.__$missingReferences;
|
|
183
|
-
// collect all references that need to be resolved - $ref and $schema
|
|
184
|
-
const refs = collectReferences.call(this, schema);
|
|
185
|
-
let idx = refs.length;
|
|
186
|
-
while (idx--) {
|
|
187
|
-
// resolve all the collected references into __xxxResolved pointer
|
|
188
|
-
const refObj = refs[idx];
|
|
189
|
-
let response = getSchemaByUri.call(this, report, refObj.ref, schema);
|
|
190
|
-
// we can try to use custom schemaReader if available
|
|
191
|
-
if (!response) {
|
|
192
|
-
const schemaReader = this.getSchemaReader();
|
|
193
|
-
if (schemaReader) {
|
|
194
|
-
// it's supposed to return a valid schema
|
|
195
|
-
const s = schemaReader(refObj.ref);
|
|
196
|
-
if (s) {
|
|
197
|
-
// it needs to have the id
|
|
198
|
-
s.id = refObj.ref;
|
|
199
|
-
// try to compile the schema
|
|
200
|
-
const subreport = new Report(report);
|
|
201
|
-
if (!compileSchema.call(this, subreport, s)) {
|
|
202
|
-
// copy errors to report
|
|
203
|
-
report.errors = report.errors.concat(subreport.errors);
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
response = getSchemaByUri.call(this, report, refObj.ref, schema);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
if (!response) {
|
|
212
|
-
const hasNotValid = report.hasError('REMOTE_NOT_VALID', [refObj.ref]);
|
|
213
|
-
const isAbsolute = Utils.isAbsoluteUri(refObj.ref);
|
|
214
|
-
let isDownloaded = false;
|
|
215
|
-
const ignoreUnresolvableRemotes = this.options.ignoreUnresolvableReferences === true;
|
|
216
|
-
if (isAbsolute) {
|
|
217
|
-
// we shouldn't add UNRESOLVABLE_REFERENCE for schemas we already have downloaded
|
|
218
|
-
// and set through setRemoteReference method
|
|
219
|
-
isDownloaded = checkCacheForUri.call(this, refObj.ref);
|
|
220
|
-
}
|
|
221
|
-
if (hasNotValid) {
|
|
222
|
-
// already has REMOTE_NOT_VALID error for this one
|
|
223
|
-
}
|
|
224
|
-
else if (ignoreUnresolvableRemotes && isAbsolute) {
|
|
225
|
-
// ignoreUnresolvableRemotes is on and remote isAbsolute
|
|
226
|
-
}
|
|
227
|
-
else if (isDownloaded) {
|
|
228
|
-
// remote is downloaded, so no UNRESOLVABLE_REFERENCE
|
|
229
|
-
}
|
|
230
|
-
else {
|
|
231
|
-
Array.prototype.push.apply(report.path, refObj.path);
|
|
232
|
-
report.addError('UNRESOLVABLE_REFERENCE', [refObj.ref]);
|
|
233
|
-
report.path = report.path.slice(0, -refObj.path.length);
|
|
234
|
-
// pusblish unresolved references out
|
|
235
|
-
if (isValidExceptReferences) {
|
|
236
|
-
schema.__$missingReferences = schema.__$missingReferences || [];
|
|
237
|
-
schema.__$missingReferences.push(refObj);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
// this might create circular references
|
|
242
|
-
refObj.obj['__' + refObj.key + 'Resolved'] = response;
|
|
243
|
-
}
|
|
244
|
-
const isValid = report.isValid();
|
|
245
|
-
if (isValid) {
|
|
246
|
-
schema.__$compiled = true;
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
if (schema.id && typeof schema.id === 'string') {
|
|
250
|
-
// remove this schema from schemaCache because it failed to compile
|
|
251
|
-
removeFromCacheByUri.call(this, schema.id);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
// we don't need the root pointer anymore
|
|
255
|
-
if (isRoot) {
|
|
256
|
-
report.rootSchema = undefined;
|
|
257
|
-
}
|
|
258
|
-
return isValid;
|
|
259
|
-
}
|