1
|
/* SPDX-License-Identifier: MIT
|
2
|
*
|
3
|
* jsonschema is licensed under MIT license.
|
4
|
*
|
5
|
* Copyright (C) 2012-2015 Tom de Grunt <tom@degrunt.nl>
|
6
|
*
|
7
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
8
|
* this software and associated documentation files (the "Software"), to deal in
|
9
|
* the Software without restriction, including without limitation the rights to
|
10
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
11
|
* of the Software, and to permit persons to whom the Software is furnished to do
|
12
|
* so, subject to the following conditions:
|
13
|
*
|
14
|
* The above copyright notice and this permission notice shall be included in all
|
15
|
* copies or substantial portions of the Software.
|
16
|
*
|
17
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
* SOFTWARE.
|
24
|
*/
|
25
|
|
26
|
#IMPORT common/jsonschema/urllib_mock.js AS urilib
|
27
|
#IMPORT common/jsonschema/helpers.js
|
28
|
|
29
|
function SchemaScanResult(found, ref){
|
30
|
this.id = found;
|
31
|
this.ref = ref;
|
32
|
}
|
33
|
#EXPORT SchemaScanResult
|
34
|
|
35
|
/**
|
36
|
* Adds a schema with a certain urn to the Validator instance.
|
37
|
* @param string uri
|
38
|
* @param object schema
|
39
|
* @return {Object}
|
40
|
*/
|
41
|
function scan(base, schema){
|
42
|
function scanSchema(baseuri, schema){
|
43
|
if(!schema || typeof schema!='object') return;
|
44
|
// Mark all referenced schemas so we can tell later which schemas are referred to, but never defined
|
45
|
if(schema.$ref){
|
46
|
var resolvedUri = urilib.resolve(baseuri, schema.$ref);
|
47
|
ref[resolvedUri] = ref[resolvedUri] ? ref[resolvedUri]+1 : 0;
|
48
|
return;
|
49
|
}
|
50
|
var id = schema.$id || schema.id;
|
51
|
var ourBase = id ? urilib.resolve(baseuri, id) : baseuri;
|
52
|
if (ourBase) {
|
53
|
// If there's no fragment, append an empty one
|
54
|
if(ourBase.indexOf('#')<0) ourBase += '#';
|
55
|
if(found[ourBase]){
|
56
|
if(!helpers.deepCompareStrict(found[ourBase], schema)){
|
57
|
throw new Error('Schema <'+ourBase+'> already exists with different definition');
|
58
|
}
|
59
|
return found[ourBase];
|
60
|
}
|
61
|
found[ourBase] = schema;
|
62
|
// strip trailing fragment
|
63
|
if(ourBase[ourBase.length-1]=='#'){
|
64
|
found[ourBase.substring(0, ourBase.length-1)] = schema;
|
65
|
}
|
66
|
}
|
67
|
scanArray(ourBase+'/items', (Array.isArray(schema.items)?schema.items:[schema.items]));
|
68
|
scanArray(ourBase+'/extends', (Array.isArray(schema.extends)?schema.extends:[schema.extends]));
|
69
|
scanSchema(ourBase+'/additionalItems', schema.additionalItems);
|
70
|
scanObject(ourBase+'/properties', schema.properties);
|
71
|
scanSchema(ourBase+'/additionalProperties', schema.additionalProperties);
|
72
|
scanObject(ourBase+'/definitions', schema.definitions);
|
73
|
scanObject(ourBase+'/patternProperties', schema.patternProperties);
|
74
|
scanObject(ourBase+'/dependencies', schema.dependencies);
|
75
|
scanArray(ourBase+'/disallow', schema.disallow);
|
76
|
scanArray(ourBase+'/allOf', schema.allOf);
|
77
|
scanArray(ourBase+'/anyOf', schema.anyOf);
|
78
|
scanArray(ourBase+'/oneOf', schema.oneOf);
|
79
|
scanSchema(ourBase+'/not', schema.not);
|
80
|
}
|
81
|
function scanArray(baseuri, schemas){
|
82
|
if(!Array.isArray(schemas)) return;
|
83
|
for(var i=0; i<schemas.length; i++){
|
84
|
scanSchema(baseuri+'/'+i, schemas[i]);
|
85
|
}
|
86
|
}
|
87
|
function scanObject(baseuri, schemas){
|
88
|
if(!schemas || typeof schemas!='object') return;
|
89
|
for(var p in schemas){
|
90
|
scanSchema(baseuri+'/'+p, schemas[p]);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
var found = {};
|
95
|
var ref = {};
|
96
|
scanSchema(base, schema);
|
97
|
return new SchemaScanResult(found, ref);
|
98
|
};
|
99
|
#EXPORT scan
|