blob: 8c7d8cf9aa49528d09c5d37f5c17bb4f3f539fd1 [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21// import Ajv from 'ajv';
22import cloneDeep from 'lodash/cloneDeep.js';
23import JSONPointer from './JSONPointer.js';
24
25export default class JSONSchema {
26
27 setSchema(schema) {
28 this._schema = schema;
29 this._fragmentsCache = new Map();
30 // this._ajv = new Ajv({
31 // useDefaults: true,
32 // coerceTypes: true
33 // });
34 // this._validate = this._ajv.compile(schema);
35 }
36
37 processData(data) {
38 data = cloneDeep(data);
39 // this._validate(data);
40 return data;
41 }
42
43 getTitle(pointer) {
44 return this._getSchemaFragment(pointer).title;
45 }
46
47 exists(pointer) {
48 const fragment = this._getSchemaFragment(pointer);
49 return !!fragment;
50 }
51
52 getDefault(pointer) {
53 const fragment = this._getSchemaFragment(pointer);
54 return fragment && fragment.default;
55 }
56
57 getEnum(pointer) {
58 const fragment = this._getSchemaFragment(pointer);
59 return fragment && (fragment.type === 'array' ? fragment.items.enum : fragment.enum);
60 }
61
62 isRequired(pointer) {
63 const parentPointer = JSONPointer.extractParentPointer(pointer);
64 const lastPart = JSONPointer.extractLastPart(pointer);
65 let parentFragment = this._getSchemaFragment(parentPointer);
66 return parentFragment && parentFragment.required && parentFragment.required.includes(lastPart);
67 }
68
69 isNumber(pointer) {
70 const fragment = this._getSchemaFragment(pointer);
71 return fragment && fragment.type === 'number';
72 }
73
74 getMaxValue(pointer) {
75 const fragment = this._getSchemaFragment(pointer);
76 return fragment && fragment.maximum;
77 }
78
79 getMinValue(pointer) {
80 const fragment = this._getSchemaFragment(pointer);
81 return fragment && fragment.minimum;
82 }
83
84 isString(pointer) {
85 const fragment = this._getSchemaFragment(pointer);
86 return fragment && fragment.type === 'string';
87 }
88
89 getPattern(pointer) {
90 const fragment = this._getSchemaFragment(pointer);
91 return fragment && fragment.pattern;
92 }
93
94 getMaxLength(pointer) {
95 const fragment = this._getSchemaFragment(pointer);
96 return fragment && fragment.maxLength;
97 }
98
99 getMinLength(pointer) {
100 const fragment = this._getSchemaFragment(pointer);
101 return fragment && fragment.minLength;
102 }
103
104 isArray(pointer) {
105 const fragment = this._getSchemaFragment(pointer);
106 return fragment && fragment.type === 'array';
107 }
108
109 _getSchemaFragment(pointer) {
110 if (this._fragmentsCache.has(pointer)) {
111 return this._fragmentsCache.get(pointer);
112 }
113
114 let parts = JSONPointer.extractParts(pointer);
115
116 let fragment = parts.reduce((fragment, part) => {
117 if (fragment === undefined) {
118 return undefined;
119 }
120
121 if (fragment.$ref) {
122 fragment = this._getSchemaFragmentByRef(fragment.$ref);
123 }
124
125 switch (fragment.type) {
126 case 'object':
127 return fragment.properties && fragment.properties[part];
128
129 case 'array':
130 return fragment.enum && fragment.enum[part];
131
132 default:
133 // throw new Error(`Incorrect/unsupported JSONPointer "${pointer}" from "${part}"`);
134 return undefined;
135 }
136 }, this._schema);
137
138 while(fragment && fragment.$ref) {
139 fragment = this._getSchemaFragmentByRef(fragment.$ref);
140 }
141
142 this._fragmentsCache.set(pointer, fragment);
143 return fragment;
144 }
145
146 _getSchemaFragmentByRef($ref) {
147 let pointer = $ref.substr(1);
148 return JSONPointer.getValue(this._schema, pointer);
149 // let fragmentAjv = new Ajv();
150 // fragmentAjv.addSchema(this._schema);
151 // let compiledFragment = fragmentAjv.compile({$ref});
152 // let fragment = compiledFragment.refVal[compiledFragment.refs[$ref]];
153 // return fragment;
154 }
155};