зеркало из https://github.com/Azure/sway.git
check for type of the payload and presence of the discriminator in the payload while validating the discriminator
This commit is contained in:
Родитель
bed0b058a0
Коммит
d535b41dc0
|
@ -126,22 +126,22 @@ function oneOf (report, schema, json) {
|
|||
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
|
||||
return false;
|
||||
}
|
||||
var discriminatorPropertyName = basePolymorphicSchema.__$refResolved.discriminator
|
||||
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 basePolymorphicSchemaDiscriminatorValue =
|
||||
basePolymorphicSchema.__$refResolved.properties[discriminatorPropertyName].enum[0]
|
||||
basePolymorphicSchema.__$refResolved.properties[discriminatorPropertyName].enum[0];
|
||||
|
||||
var jsonDiscriminatorValue =
|
||||
var jsonDiscriminatorValue =
|
||||
json[discriminatorPropertyName] ||
|
||||
basePolymorphicSchemaDiscriminatorValue
|
||||
basePolymorphicSchemaDiscriminatorValue;
|
||||
|
||||
var schemaToValidate =
|
||||
schema.oneOf.find(
|
||||
|
@ -149,14 +149,22 @@ function validateDiscriminator (report, schema, json) {
|
|||
s.__$refResolved &&
|
||||
s.__$refResolved.properties[discriminatorPropertyName].enum[0] ===
|
||||
jsonDiscriminatorValue
|
||||
) || basePolymorphicSchema
|
||||
) || basePolymorphicSchema;
|
||||
|
||||
var isJsonObject = typeof json === 'object' && json !== null && !Array.isArray(json);
|
||||
|
||||
// if the schema to validate is the base schema, we dont need to validate the discriminator enum value.
|
||||
if (schemaToValidate === basePolymorphicSchema) {
|
||||
json[discriminatorPropertyName] = basePolymorphicSchemaDiscriminatorValue
|
||||
// if the schema to validate is the base schema and the payload is of type object and if the
|
||||
// discriminator property is actually present in the payload then, we do not need to validate
|
||||
// the discriminator enum value.
|
||||
if (
|
||||
schemaToValidate === basePolymorphicSchema &&
|
||||
isJsonObject &&
|
||||
json[discriminatorPropertyName] != undefined
|
||||
) {
|
||||
json[discriminatorPropertyName] = basePolymorphicSchemaDiscriminatorValue;
|
||||
}
|
||||
ZSchemaValidator.validate.call(this, report, schemaToValidate, json)
|
||||
return true
|
||||
ZSchemaValidator.validate.call(this, report, schemaToValidate, json);
|
||||
return true;
|
||||
}
|
||||
|
||||
function whatIs (what) {
|
||||
|
@ -190,11 +198,11 @@ function whatIs (what) {
|
|||
|
||||
function shouldSkipValidate (options, errors) {
|
||||
return options &&
|
||||
Array.isArray(options.includeErrors) &&
|
||||
options.includeErrors.length > 0 &&
|
||||
!errors.some(function (err) {
|
||||
return options.includeErrors.includes(err);
|
||||
});
|
||||
Array.isArray(options.includeErrors) &&
|
||||
options.includeErrors.length > 0 &&
|
||||
!errors.some(function (err) {
|
||||
return options.includeErrors.includes(err);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports.shouldSkipValidate = shouldSkipValidate;
|
||||
|
|
Загрузка…
Ссылка в новой задаче