diff options
author | AviZi <avi.ziv@amdocs.com> | 2017-06-09 02:39:56 +0300 |
---|---|---|
committer | AviZi <avi.ziv@amdocs.com> | 2017-06-09 02:39:56 +0300 |
commit | 280f8015d06af1f41a3ef12e8300801c7a5e0d54 (patch) | |
tree | 9c1d3978c04cd28068f02073038c936bb49ca9e0 /openecomp-ui/src/nfvo-utils/json/JSONSchema.js | |
parent | fd3821dad11780d33c5373d74c957c442489945e (diff) |
[SDC-29] Amdocs OnBoard 1707 initial commit.
Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370
Signed-off-by: AviZi <avi.ziv@amdocs.com>
Diffstat (limited to 'openecomp-ui/src/nfvo-utils/json/JSONSchema.js')
-rw-r--r-- | openecomp-ui/src/nfvo-utils/json/JSONSchema.js | 127 |
1 files changed, 112 insertions, 15 deletions
diff --git a/openecomp-ui/src/nfvo-utils/json/JSONSchema.js b/openecomp-ui/src/nfvo-utils/json/JSONSchema.js index 8c7d8cf9aa..3b3a9bf7b4 100644 --- a/openecomp-ui/src/nfvo-utils/json/JSONSchema.js +++ b/openecomp-ui/src/nfvo-utils/json/JSONSchema.js @@ -1,23 +1,18 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ +/*! * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. */ - // import Ajv from 'ajv'; import cloneDeep from 'lodash/cloneDeep.js'; import JSONPointer from './JSONPointer.js'; @@ -40,6 +35,108 @@ export default class JSONSchema { return data; } + // array of names of validation functions + setSupportedValidationFunctions(supportedValidationFunctions) { + this._supportedValidationFunctions = supportedValidationFunctions; + } + + /* FYI - I was going to support "required" but then found out that server never sends it in its schema (it was a business decision. so leaving the code commented for now */ + flattenSchema(supportedValidationFunctions) { + if (supportedValidationFunctions) { this.setSupportedValidationFunctions(supportedValidationFunctions); } + let genericFieldInfo = {}; + if (this._schema && this._schema.properties) { + this.travelProperties(this._schema.properties, genericFieldInfo/*, this._schema.required*/); + } + return {genericFieldInfo}; + } + + extractGenericFieldInfo(item) { + let validationsArr = []; + let additionalInfo = { isValid: true, errorText: ''}; + for (let value in item) { + if (this._supportedValidationFunctions.includes(value)) { + let validationItem = this.extractValidations(item, value); + validationsArr[validationsArr.length] = validationItem; + } else { + let enumResult = this.extractEnum(item, value); + if (enumResult !== null) { + additionalInfo.enum = enumResult; + } + else { + additionalInfo[value] = item[value]; + } + /*if (required.includes (property)) { + additionalInfo[value].isRequired = true ; + }*/ + } + } + + additionalInfo.validations = validationsArr; + return additionalInfo; + } + + extractValidations(item, value) { + let validationItem; + let data = item[value]; + if (value === 'maximum') { + if (item.exclusiveMaximum) { + value = 'maximumExclusive'; + } + } + if (value === 'minimum') { + if (item.exclusiveMinimum) { + value = 'minimumExclusive'; + } + } + validationItem = {type: value, data: data}; + return validationItem; + } + + extractEnum(item, value) { + let enumResult = null; + if (value === 'type' && item[value] === 'array') { + let items = item.items; + if (items && items.enum && items.enum.length > 0) { + let values = items.enum + .filter(value => value) + .map(value => ({enum: value, title: value})); + enumResult = values; + } + } + else if (value === 'enum') { + let items = item[value]; + if (items && items.length > 0) { + let values = items + .filter(value => value) + .map(value => ({enum: value, title: value})); + enumResult = values; + } + } + return enumResult; + } + + travelProperties(properties, genericFieldDefs, /*required = [],*/ pointer = ''){ + let newPointer = pointer; + for (let property in properties) { + newPointer = newPointer ? newPointer + '/' + property : property; + if (properties[property].properties) { + this.travelProperties(properties[property].properties, genericFieldDefs /*, properties[property].required*/, newPointer); + } + else if (properties[property].$ref){ + let fragment = this._getSchemaFragmentByRef(properties[property].$ref); + if (fragment.properties) { + this.travelProperties(fragment.properties, genericFieldDefs /*, properties[property].required*/, newPointer); + } else { + genericFieldDefs[newPointer] = this.extractGenericFieldInfo(fragment.properties); + } + } + else { + genericFieldDefs[newPointer] = this.extractGenericFieldInfo(properties[property]); + } + newPointer = pointer; + } + } + getTitle(pointer) { return this._getSchemaFragment(pointer).title; } @@ -73,12 +170,12 @@ export default class JSONSchema { getMaxValue(pointer) { const fragment = this._getSchemaFragment(pointer); - return fragment && fragment.maximum; + return fragment && fragment.exclusiveMaximum ? fragment.maximum - 1 : fragment.maximum; } getMinValue(pointer) { const fragment = this._getSchemaFragment(pointer); - return fragment && fragment.minimum; + return fragment && fragment.exclusiveMinimum ? fragment.minimum : fragment.minimum; } isString(pointer) { |