зеркало из https://github.com/Azure/sway.git
Support polymorphic anyof (#69)
This commit is contained in:
Родитель
9a9c01386e
Коммит
9cedce73b8
|
@ -112,6 +112,7 @@ function createJSONValidator () {
|
|||
ZSchemaValidator.JsonValidators.enum = customValidators.enumValidator;
|
||||
ZSchemaValidator.JsonValidators.type = customValidators.typeValidator;
|
||||
ZSchemaValidator.JsonValidators.required = customValidators.requiredPropertyValidator;
|
||||
ZSchemaValidator.JsonValidators.oneOf = customValidators.oneOf;
|
||||
|
||||
var validator = new ZSchema({
|
||||
breakOnFirstError: false,
|
||||
|
@ -195,7 +196,7 @@ var convertValue = module.exports.convertValue = function (schema, options, valu
|
|||
(Array.isArray(type) && type.some(function (t) {
|
||||
return types.indexOf(t) === -1;
|
||||
}))))) {
|
||||
throw new TypeError('Invalid \'type\' value: ' + type);
|
||||
throw new TypeError('Invalid \'type\' value: ' + type);
|
||||
}
|
||||
|
||||
// Since JSON Schema allows you to not specify a type and it is treated as a wildcard of sorts, we should not do any
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
|
||||
'use strict';
|
||||
var Report = require('z-schema/src/Report');
|
||||
var ZSchemaValidator = require('z-schema/src/JsonValidation');
|
||||
|
||||
function enumValidator (report, schema, json) {
|
||||
var match = false;
|
||||
|
@ -45,7 +47,7 @@ function requiredPropertyValidator (report, schema, json) {
|
|||
var idx = schema.required.length;
|
||||
var requiredPropertyName;
|
||||
var xMsMutability;
|
||||
|
||||
|
||||
while (idx--) {
|
||||
requiredPropertyName = schema.required[idx];
|
||||
xMsMutability = (schema.properties && schema.properties[`${requiredPropertyName}`]) && schema.properties[`${requiredPropertyName}`]['x-ms-mutability'];
|
||||
|
@ -86,6 +88,61 @@ function typeValidator (report, schema, json) {
|
|||
}
|
||||
}
|
||||
|
||||
function oneOf (report, schema, json) {
|
||||
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.5.2
|
||||
var passes = 0,
|
||||
subReports = [],
|
||||
idx = schema.oneOf.length
|
||||
var subReport
|
||||
|
||||
// first check and handle the case of polymporhic oneOf.
|
||||
if (validateDiscriminator.call(this, report, schema, json)) {
|
||||
return
|
||||
}
|
||||
|
||||
while (idx--) {
|
||||
subReport = new Report(report, {maxErrors: 1})
|
||||
subReports.push(subReport)
|
||||
if (ZSchemaValidator.validate.call(this, subReport, schema.oneOf[idx], json) === true) {
|
||||
passes++
|
||||
}
|
||||
}
|
||||
if (passes === 0) {
|
||||
report.addError('ONE_OF_MISSING', undefined, subReports, schema)
|
||||
} else if (passes > 1) {
|
||||
report.addError('ONE_OF_MULTIPLE', null, null, schema)
|
||||
}
|
||||
}
|
||||
|
||||
function validateDiscriminator (report, schema, json) {
|
||||
var basePolymorphicSchema = schema.oneOf.find(
|
||||
s => s.__$refResolved && s.__$refResolved.discriminator !== undefined
|
||||
)
|
||||
|
||||
// if none of the oneOf subschemas has a discriminator, we are not in a polymorphic oneOf.
|
||||
if (!basePolymorphicSchema) {
|
||||
return false
|
||||
}
|
||||
var discriminatorPropertyName = basePolymorphicSchema.__$refResolved.discriminator
|
||||
|
||||
// to conform to the Azure specs, we accept a lenient discriminator. if the type is missing in the
|
||||
// payload we use the base class. Also if the type doesn't match anything, we use the base class.
|
||||
var jsonDiscriminatorValue =
|
||||
json[discriminatorPropertyName] ||
|
||||
basePolymorphicSchema.__$refResolved.properties[discriminatorPropertyName].enum[0]
|
||||
|
||||
var schemaToValidate =
|
||||
schema.oneOf.find(
|
||||
s =>
|
||||
s.__$refResolved &&
|
||||
s.__$refResolved.properties[discriminatorPropertyName].enum[0] ===
|
||||
jsonDiscriminatorValue
|
||||
) || basePolymorphicSchema
|
||||
|
||||
ZSchemaValidator.validate.call(this, report, schemaToValidate, json)
|
||||
return true
|
||||
}
|
||||
|
||||
function whatIs (what) {
|
||||
var to = typeof what;
|
||||
|
||||
|
@ -118,3 +175,4 @@ function whatIs (what) {
|
|||
module.exports.enumValidator = enumValidator;
|
||||
module.exports.requiredPropertyValidator = requiredPropertyValidator;
|
||||
module.exports.typeValidator = typeValidator;
|
||||
module.exports.oneOf = oneOf;
|
||||
|
|
14
package.json
14
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "yasway",
|
||||
"version": "1.5.14",
|
||||
"version": "1.6.0",
|
||||
"description": "A library that simplifies Swagger integrations.",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
|
@ -13,18 +13,18 @@
|
|||
"swagger"
|
||||
],
|
||||
"author": {
|
||||
"name": "Amar Zavery",
|
||||
"email": "amzavery@microsoft.com",
|
||||
"url": "https://github.com/amarzavery"
|
||||
"name": "Microsoft Corporation",
|
||||
"email": "azsdkteam@microsoft.com",
|
||||
"url": "https://github.com/Azure/sway"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/amarzavery/sway/issues"
|
||||
"url": "https://github.com/Azure/sway/issues"
|
||||
},
|
||||
"homepage": "https://github.com/amarzavery/sway",
|
||||
"homepage": "https://github.com/Azure/sway",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/amarzavery/sway.git"
|
||||
"url": "git://github.com/Azure/sway.git"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
|
|
Загрузка…
Ссылка в новой задаче