зеркало из https://github.com/Azure/sway.git
Merge branch 'polymorphic' of github.com:amarzavery/sway into validation
This commit is contained in:
Коммит
a7dbf6a610
|
@ -455,7 +455,7 @@ module.exports.validateAgainstSchema = function (validator, schema, value, schem
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
isValid = validator.validate(value, schema, { schemaPath: schemaPath });
|
||||
if (!isValid) {
|
||||
response.errors = _.map(validator.getLastErrors(), function (err) {
|
||||
|
@ -541,55 +541,62 @@ function getObject(ref, source, isNotRef) {
|
|||
return result;
|
||||
}
|
||||
|
||||
function searchDiscriminator(searchObj, searchValue, swaggerSpec, parentObj, arrayofStacks) {
|
||||
if (searchObj) {
|
||||
if (!arrayofStacks) arrayofStacks = [[]];
|
||||
if (searchObj.discriminator) {
|
||||
//When the discriminator is found, we search for that property in searchValue(example) and find out the value
|
||||
//property marked as discriminator. Then we search for that model definition in the definitions section of
|
||||
//the swagger spec. While searching for the model definition we take into account the "x-ms-discriminator-value"
|
||||
//extension as well. We modify the reference in the parentObject to that of the newly found model definition.
|
||||
//Now, our searchObj is the new model definition
|
||||
for (let i = 0; i < arrayofStacks.length; i++) {
|
||||
let stack = arrayofStacks[i];
|
||||
let entry = stack[stack.length - 1];
|
||||
let propertyToSet;
|
||||
//if last entry in the stack is an integer then we dont want to use that property for setting the
|
||||
//reference in the schema.
|
||||
if (isNaN(parseInt(entry))) propertyToSet = `['${stack[stack.length - 1]}']`
|
||||
let parentPath = `['${stack.join("']['")}']`;
|
||||
let disPath = parentPath ? `${parentPath}['${searchObj.discriminator}']` : `['${searchObj.discriminator}']`;
|
||||
let disValue = getObject(disPath, searchValue, true);
|
||||
let definitions = swaggerSpec.definitions;
|
||||
if (definitions) {
|
||||
for (let definitionName in definitions) {
|
||||
if (definitionName === disValue || (definitions[definitionName] &&
|
||||
definitions[definitionName]['x-ms-discriminator-value'] &&
|
||||
definitions[definitionName]['x-ms-discriminator-value'] === disValue)) {
|
||||
let referenceToSet = `#/definitions/${definitionName}`;
|
||||
try {
|
||||
//resetting the object to empty object irrespective of it being $ref or
|
||||
//an inline object definition because we know the reference to set.
|
||||
if (propertyToSet) {
|
||||
eval(`parentObj${propertyToSet} = {}`);
|
||||
eval(`parentObj${propertyToSet}['$ref'] = '${referenceToSet}'`);
|
||||
} else {
|
||||
eval(`parentObj['items'] = {}`);
|
||||
eval(`parentObj['items']['$ref'] = '${referenceToSet}'`);
|
||||
}
|
||||
//Once the above reference is set, then we need to iterate the properties
|
||||
//of the new object (child model) in the reference. Thus setting search
|
||||
//object to the new one.
|
||||
searchObj = getObject(referenceToSet, swaggerSpec);
|
||||
break;//no point searching further as we found the match.
|
||||
} catch (err) {
|
||||
//do something
|
||||
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
|
||||
//property marked as discriminator. Then we search for that model definition in the definitions section of
|
||||
//the swagger spec. While searching for the model definition we take into account the "x-ms-discriminator-value"
|
||||
//extension as well. We modify the reference in the parentObject to that of the newly found model definition.
|
||||
//Now, our searchObj is the new model definition
|
||||
for (let i = 0; i < arrayofStacks.length; i++) {
|
||||
let stack = arrayofStacks[i];
|
||||
let entry = stack[stack.length - 1];
|
||||
let propertyToSet;
|
||||
//if last entry in the stack is an integer then we dont want to use that property for setting the
|
||||
//reference in the schema.
|
||||
if (isNaN(parseInt(entry))) propertyToSet = `['${stack[stack.length - 1]}']`
|
||||
let parentPath = `['${stack.join("']['")}']`;
|
||||
let disPath = parentPath ? `${parentPath}['${searchObj.discriminator}']` : `['${searchObj.discriminator}']`;
|
||||
let disValue = getObject(disPath, searchValue, true);
|
||||
let definitions = swaggerSpec.definitions;
|
||||
if (definitions) {
|
||||
for (let definitionName in definitions) {
|
||||
if (definitionName === disValue || (definitions[definitionName] &&
|
||||
definitions[definitionName]['x-ms-discriminator-value'] &&
|
||||
definitions[definitionName]['x-ms-discriminator-value'] === disValue)) {
|
||||
let referenceToSet = `#/definitions/${definitionName}`;
|
||||
try {
|
||||
//resetting the object to empty object irrespective of it being $ref or
|
||||
//an inline object definition because we know the reference to set.
|
||||
if (propertyToSet) {
|
||||
eval(`parentObj${propertyToSet} = {}`);
|
||||
eval(`parentObj${propertyToSet}['$ref'] = '${referenceToSet}'`);
|
||||
} else {
|
||||
eval(`parentObj['items'] = {}`);
|
||||
eval(`parentObj['items']['$ref'] = '${referenceToSet}'`);
|
||||
}
|
||||
//Once the above reference is set, then we need to iterate the properties
|
||||
//of the new object (child model) in the reference. Thus setting search
|
||||
//object to the new one.
|
||||
searchObj = getObject(referenceToSet, swaggerSpec);
|
||||
break;//no point searching further as we found the match.
|
||||
} catch (err) {
|
||||
//do something
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return searchObj;
|
||||
}
|
||||
|
||||
function searchDiscriminator(searchObj, searchValue, swaggerSpec, parentObj, arrayofStacks) {
|
||||
if (searchObj) {
|
||||
if (!arrayofStacks) arrayofStacks = [[]];
|
||||
if (searchObj.discriminator) {
|
||||
searchObj = processDiscriminator(searchObj, searchValue, swaggerSpec, parentObj, arrayofStacks);
|
||||
}
|
||||
if (searchObj.properties) {
|
||||
for (let prop in searchObj.properties) {
|
||||
let propertyValue = searchObj.properties[prop];
|
||||
|
|
Загрузка…
Ссылка в новой задаче