UNPKG

6.42 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var JsonPointer = tslib_1.__importStar(require("../jsonPointer"));
5var schemaId_1 = tslib_1.__importDefault(require("./schemaId"));
6function parseSchema(content, url) {
7 var _a = selectSchemaType(content), type = _a.type, openApiVersion = _a.openApiVersion;
8 if (url != null) {
9 setId(type, content, url);
10 }
11 var id = getId(type, content);
12 return {
13 type: type,
14 openApiVersion: openApiVersion,
15 id: id ? new schemaId_1.default(id) : schemaId_1.default.empty,
16 content: content,
17 };
18}
19exports.parseSchema = parseSchema;
20function getSubSchema(rootSchema, pointer, id) {
21 var content = JsonPointer.get(rootSchema.content, JsonPointer.parse(pointer));
22 if (id == null) {
23 var subId = getId(rootSchema.type, content);
24 var getParentIds_1 = function (s, result) {
25 result.push(s.id.getAbsoluteId());
26 return s.rootSchema == null ? result : getParentIds_1(s.rootSchema, result);
27 };
28 if (subId) {
29 id = new schemaId_1.default(subId, getParentIds_1(rootSchema, []));
30 }
31 else {
32 id = new schemaId_1.default(pointer, getParentIds_1(rootSchema, []));
33 }
34 }
35 return {
36 type: rootSchema.type,
37 id: id,
38 content: content,
39 rootSchema: rootSchema,
40 };
41}
42exports.getSubSchema = getSubSchema;
43function getId(type, content) {
44 return content[getIdPropertyName(type)];
45}
46exports.getId = getId;
47function setId(type, content, id) {
48 var key = getIdPropertyName(type);
49 if (content[key] == null) {
50 content[key] = id;
51 }
52}
53function getIdPropertyName(type) {
54 switch (type) {
55 case 'Draft04': return 'id';
56 case 'Draft07': return '$id';
57 }
58}
59function searchAllSubSchema(schema, onFoundSchema, onFoundReference) {
60 var walkArray = function (array, paths, parentIds) {
61 if (array == null) {
62 return;
63 }
64 array.forEach(function (item, index) {
65 walk(item, paths.concat(index.toString()), parentIds);
66 });
67 };
68 var walkObject = function (obj, paths, parentIds) {
69 if (obj == null) {
70 return;
71 }
72 Object.keys(obj).forEach(function (key) {
73 var sub = obj[key];
74 if (sub != null) {
75 walk(sub, paths.concat(key), parentIds);
76 }
77 });
78 };
79 var walkMaybeArray = function (item, paths, parentIds) {
80 if (Array.isArray(item)) {
81 walkArray(item, paths, parentIds);
82 }
83 else {
84 walk(item, paths, parentIds);
85 }
86 };
87 var walk = function (s, paths, parentIds) {
88 if (s == null || typeof s !== 'object') {
89 return;
90 }
91 var id = getId(schema.type, s);
92 if (id && typeof id === 'string') {
93 var schemaId = new schemaId_1.default(id, parentIds);
94 var subSchema = {
95 type: schema.type,
96 id: schemaId,
97 content: s,
98 rootSchema: schema,
99 };
100 onFoundSchema(subSchema);
101 parentIds = parentIds.concat([schemaId.getAbsoluteId()]);
102 }
103 if (typeof s.$ref === 'string') {
104 var schemaId = new schemaId_1.default(s.$ref, parentIds);
105 s.$ref = schemaId.getAbsoluteId();
106 onFoundReference(schemaId);
107 }
108 walkArray(s.allOf, paths.concat('allOf'), parentIds);
109 walkArray(s.anyOf, paths.concat('anyOf'), parentIds);
110 walkArray(s.oneOf, paths.concat('oneOf'), parentIds);
111 walk(s.not, paths.concat('not'), parentIds);
112 walkMaybeArray(s.items, paths.concat('items'), parentIds);
113 walk(s.additionalItems, paths.concat('additionalItems'), parentIds);
114 walk(s.additionalProperties, paths.concat('additionalProperties'), parentIds);
115 walkObject(s.definitions, paths.concat('definitions'), parentIds);
116 walkObject(s.properties, paths.concat('properties'), parentIds);
117 walkObject(s.patternProperties, paths.concat('patternProperties'), parentIds);
118 walkMaybeArray(s.dependencies, paths.concat('dependencies'), parentIds);
119 if (schema.type === 'Draft07') {
120 if ('propertyNames' in s) {
121 walk(s.propertyNames, paths.concat('propertyNames'), parentIds);
122 walk(s.contains, paths.concat('contains'), parentIds);
123 walk(s.if, paths.concat('if'), parentIds);
124 walk(s.then, paths.concat('then'), parentIds);
125 walk(s.else, paths.concat('else'), parentIds);
126 }
127 }
128 if (schema.openApiVersion === 3) {
129 var obj = s;
130 if (obj.components && obj.components.schemas) {
131 walkObject(obj.components.schemas, paths.concat('components', 'schemas'), parentIds);
132 }
133 }
134 };
135 walk(schema.content, ['#'], []);
136}
137exports.searchAllSubSchema = searchAllSubSchema;
138function selectSchemaType(content) {
139 if (content.$schema) {
140 var schema = content.$schema;
141 var match = schema.match(/http\:\/\/json-schema\.org\/draft-(\d+)\/schema#?/);
142 if (match) {
143 var version = Number(match[1]);
144 if (version <= 4) {
145 return { type: 'Draft04' };
146 }
147 else {
148 return { type: 'Draft07' };
149 }
150 }
151 }
152 if (content.swagger === '2.0') {
153 if (content.definitions) {
154 setSubIds(content.definitions, 'Draft04', 'definitions');
155 }
156 return {
157 type: 'Draft04',
158 openApiVersion: 2,
159 };
160 }
161 if (content.openapi) {
162 var openapi = content.openapi;
163 if (/^3\.\d+\.\d+$/.test(openapi)) {
164 if (content.components && content.components.schemas) {
165 setSubIds(content.components.schemas, 'Draft07', 'components/schemas');
166 }
167 return {
168 type: 'Draft07',
169 openApiVersion: 3,
170 };
171 }
172 }
173 return { type: 'Draft04' };
174}
175function setSubIds(obj, type, prefix) {
176 Object.keys(obj).forEach(function (key) {
177 var sub = obj[key];
178 if (sub != null) {
179 setId(type, sub, "#/" + prefix + "/" + key);
180 }
181 });
182}