JSON схема, через которую должен проходить объект, который должен быть проверен на
{
"components": {
"schemas": {
"Item": {
"type": "object",
"required": [
"externalId",
"name",
"products",
"effectiveFrom"
],
"properties": {
"id": {
"type": "integer",
"nullable": true
},
"externalId": {
"type": "string",
"nullable": false
},
"name": {
"type": "string",
"nullable": false
},
"nameExtended": {
"type": "string",
"nullable": true
},
"description": {
"type": "string",
"nullable": true
},
"products": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
},
"nullable": false
},
"measures": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Measure"
},
"nullable": true
},
"categories": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Category"
},
"nullable": true
},
"subCategories": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Category"
},
"nullable": true
},
"attributes": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Attribute"
},
"nullable": true
},
"effectiveFrom": {
"type": "string",
"nullable": false
},
"effectiveTo": {
"type": "string",
"nullable": true
}
}
},
"Product": {
"type": "object",
"required": [
"externalId",
"name",
"effectiveFrom"
],
"properties": {
"id": {
"type": "integer",
"nullable": true
},
"externalId": {
"type": "string",
"nullable": false
},
"name": {
"type": "string",
"nullable": false
},
"nameExtended": {
"type": "string",
"nullable": true
},
"description": {
"type": "string",
"nullable": true
},
"brand": {
"type": "string",
"nullable": true
},
"manufacturer": {
"type": "string",
"nullable": true
},
"measures": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Measure"
},
"nullable": true
},
"categories": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Category"
},
"nullable": true
},
"subCategories": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Category"
},
"nullable": true
},
"attributes": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Attribute"
},
"nullable": true
},
"effectiveFrom": {
"type": "string",
"nullable": false
},
"effectiveTo": {
"type": "string",
"nullable": true
}
}
},
"Measure": {
"type": "object",
"required": [
"measureCode",
"value",
"valueUnit"
],
"properties": {
"id": {
"type": "integer",
"nullable": true
},
"measureCode": {
"type": "string",
"nullable": false
},
"measureName": {
"type": "string",
"nullable": true
},
"value": {
"type": "number",
"nullable": false
},
"valueUnit": {
"type": "string",
"nullable": false
}
}
},
"Category": {
"type": "object",
"required": [
"code",
"name"
],
"properties": {
"id": {
"type": "integer",
"nullable": true
},
"code": {
"type": "string",
"nullable": false
},
"name": {
"type": "string",
"nullable": false
},
"description": {
"type": "string",
"nullable": true
}
}
},
"Attribute": {
"type": "object",
"required": [
"key",
"value"
],
"properties": {
"id": {
"type": "integer",
"nullable": true
},
"key": {
"type": "string",
"nullable": false
},
"value": {
"type": "string",
"nullable": false
}
}
}
}
}
}
метод ValidateSchema
export const validateSchema = (instance: any, schema: any) =>
new Promise(resolve => {
const validator = new Validator();
let response = validator.validate(instance, schema);
if (response.errors && response.errors.length) {
let errors = response.errors.map(e => e.property.replace('instance.', '') + ` ${e.message}`).join('\n');
resolve(errors);
} else {
resolve();
}
});
метод ValidatePayload, через который я на самом деле передаю свои неверные данные
export const validatePayload = async (payload: object): Promise<void> => new Promise((resolve, reject) => {
// Getting the JSON from swagger
let openAPI: any = swaggerService.getJson();
// Digging down into the schemas from the JSON
let schema = openAPI.components.schemas;
// Compares the payload against the schema, if not compatible, then it will error
validateSchema(payload, schema).then((errors: string) => {
if (errors) {
reject(new Error(errors));
} else {
resolve();
}
}).catch(err => {
reject(`Validator Error: ${err}`);
})
})
Я могу пройти через пустой объект, и он все еще проверяется, может кто-нибудь сказать мне, почему? Я также могу передавать объекты, в которых отсутствуют обязательные поля. Например, не должна ли эта полезная нагрузка быть недопустимой, поскольку в ней отсутствует обязательное поле имени?
{ externalId: '1' products: [ { externalId: '2', name: 'whatever tofu is made from', effectiveFrom: '2/09/2020' } ], effectiveFrom: '2/09/2020' }
Редактировать: моя схема находится в YAML, что на время выполнения настраивается на JSON