add support to compare type as string or as an array

This commit is contained in:
Amar Zavery 2017-04-12 13:51:55 -07:00
Родитель ca12f42f73
Коммит 4621d4cb5d
1 изменённых файлов: 21 добавлений и 3 удалений

Просмотреть файл

@ -541,6 +541,24 @@ function getObject(ref, source, isNotRef) {
return result;
}
/**
* It is used to compare whether type === value. It could be possible that type is not a string
* but an array of primitve types ['array', 'boolean', 'number', 'object', 'string', 'null'].
* If it is an array then we need to find whether value is one of the valid values.
* @param {array|string} type The json schema primitive type or an array of primitive types.
* @param {*} value The value to be checked. i.e whether it is the same as the primitive
* type or one of the primitive types.
*/
function compareTypeValue(type, value) {
let result = false;
if (Array.isArray(type)) {
result = type.some((item) => { return item === value; });
} else if (type && typeof type.valueOf() === 'string') {
result = type === value;
}
return result;
}
function processDiscriminator(searchObj, searchValue, swaggerSpec, parentObj, arrayofStacks) {
if (searchObj && searchObj.discriminator) {
//When the discriminator is found, we search for that property in searchValue(example) and find out the value
@ -606,7 +624,7 @@ function searchDiscriminator(searchObj, searchValue, swaggerSpec, parentObj, arr
});
if (propertyValue['$ref']) {
searchDiscriminator(getObject(propertyValue['$ref'], swaggerSpec), searchValue, swaggerSpec, searchObj.properties, arrayofStacks);
} else if (propertyValue.type && propertyValue.type.toLowerCase() === 'array' && propertyValue.items) {
} else if (propertyValue.type && compareTypeValue(propertyValue.type, 'array') && propertyValue.items) {
if (propertyValue.items['$ref']) {
let accessKey = `['${arrayofStacks[0].join("']['")}']`;
let arrayVal = getObject(accessKey, searchValue, true);
@ -629,12 +647,12 @@ function searchDiscriminator(searchObj, searchValue, swaggerSpec, parentObj, arr
} else if (propertyValue.items.properties) { // inline object inside the items object of an array type
searchDiscriminator(propertyValue.items.properties, swaggerSpec, propertyValue.items, arrayofStacks);
}
} else if (propertyValue.type && propertyValue.type.toLowerCase() === 'object' && propertyValue.properties) { //inline object
} else if (propertyValue.type && compareTypeValue(propertyValue.type, 'object') && propertyValue.properties) { //inline object
searchDiscriminator(propertyValue.properties, searchValue, swaggerSpec, searchObj.properties.properties, arrayofStacks);
}
arrayofStacks.forEach((stck) => { stck.pop(); }); //done traversing the prop and it's children if any
}
} else if (searchObj.type && searchObj.type.toLowerCase() === 'array' && searchObj.items) {
} else if (searchObj.type && compareTypeValue(searchObj.type, 'array') && searchObj.items) {
if (searchObj.items['$ref']) {
//no need to push to stack over here. The property that references this type should have
//been pushed into the stack before called here.