使用 allOf 和附加属性进行 Swagger 数据验证
Swagger data validation with allOf and additional properties
需要帮助解决使用 allOf 和 additionalProperties 的 swagger 定义模式:false
这是我的 JSON 架构
"deliveryContact": {
"allOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"maxLength": 1024
},
"phone": {
"type": "string",
"maxLength": 24
}
}
},
{
"$ref": "#/definitions/address"
}
]
},
"address": {
"type": "object",
"additionalProperties": false,
"properties": {
"address": {
"type": "string",
"maxLength": 1024
},
"postalCode": {
"type": "string",
"maxLength": 12
},
"city": {
"type": "string",
"maxLength": 512
},
"state": {
"type": "string",
"maxLength": 512
}
}
},
示例数据
delivery: {
address: 'my address',
postalCode: 'my postalCode',
city: 'my city',
state: 'my state',
name: 'my name',
phone: 'my phone'
},
我使用 AJV 6.10.0 来验证我的数据,但我认为我的架构定义有误。
使用 Ajv 选项:
ajv = require('ajv')({
allErrors: true,
verbose: true,
removeAdditional: false,
});
实际上,我有 6 个错误警告每个 属性
的附加属性
在验证 allOf (name and phone) 中的第一个对象时,验证发现 (address, postalCode, city and state) 错误
如果我删除第一个 allOf 对象(名称,phone)的附加属性,在验证地址模式期间,验证发现错误(名称和 phone)
如何解决我的模式定义
我设法让它工作了,我已经更新了你的数据结构以使其更符合逻辑,见下文:
JSON 架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"delivery"
],
"properties": {
"delivery": {
"type": "object",
"additionalProperties": false,
"required": [
"name",
"phone",
"address"
],
"properties": {
"name": {
"type": "string",
"maxLength": 1024
},
"phone": {
"type": "string",
"maxLength": 24
},
"address": {
"$ref": "#/definitions/address"
}
}
}
},
"definitions": {
"address": {
"type": "object",
"additionalProperties": false,
"properties": {
"address": {
"type": "string",
"maxLength": 1024
},
"postalCode": {
"type": "string",
"maxLength": 12
},
"city": {
"type": "string",
"maxLength": 512
},
"state": {
"type": "string",
"maxLength": 512
}
}
}
}
}
示例数据
{
"delivery": {
"address": {
"address": "myaddress",
"postalCode": "mypostalCode",
"city": "mycity",
"state": "mystate"
},
"name": "myname",
"phone": "myphone"
}
}
如果你想测试它,你可以在这里做:https://www.jsonschemavalidator.net/
需要帮助解决使用 allOf 和 additionalProperties 的 swagger 定义模式:false
这是我的 JSON 架构
"deliveryContact": {
"allOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string",
"maxLength": 1024
},
"phone": {
"type": "string",
"maxLength": 24
}
}
},
{
"$ref": "#/definitions/address"
}
]
},
"address": {
"type": "object",
"additionalProperties": false,
"properties": {
"address": {
"type": "string",
"maxLength": 1024
},
"postalCode": {
"type": "string",
"maxLength": 12
},
"city": {
"type": "string",
"maxLength": 512
},
"state": {
"type": "string",
"maxLength": 512
}
}
},
示例数据
delivery: {
address: 'my address',
postalCode: 'my postalCode',
city: 'my city',
state: 'my state',
name: 'my name',
phone: 'my phone'
},
我使用 AJV 6.10.0 来验证我的数据,但我认为我的架构定义有误。 使用 Ajv 选项:
ajv = require('ajv')({
allErrors: true,
verbose: true,
removeAdditional: false,
});
实际上,我有 6 个错误警告每个 属性
的附加属性在验证 allOf (name and phone) 中的第一个对象时,验证发现 (address, postalCode, city and state) 错误
如果我删除第一个 allOf 对象(名称,phone)的附加属性,在验证地址模式期间,验证发现错误(名称和 phone)
如何解决我的模式定义
我设法让它工作了,我已经更新了你的数据结构以使其更符合逻辑,见下文:
JSON 架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"delivery"
],
"properties": {
"delivery": {
"type": "object",
"additionalProperties": false,
"required": [
"name",
"phone",
"address"
],
"properties": {
"name": {
"type": "string",
"maxLength": 1024
},
"phone": {
"type": "string",
"maxLength": 24
},
"address": {
"$ref": "#/definitions/address"
}
}
}
},
"definitions": {
"address": {
"type": "object",
"additionalProperties": false,
"properties": {
"address": {
"type": "string",
"maxLength": 1024
},
"postalCode": {
"type": "string",
"maxLength": 12
},
"city": {
"type": "string",
"maxLength": 512
},
"state": {
"type": "string",
"maxLength": 512
}
}
}
}
}
示例数据
{
"delivery": {
"address": {
"address": "myaddress",
"postalCode": "mypostalCode",
"city": "mycity",
"state": "mystate"
},
"name": "myname",
"phone": "myphone"
}
}
如果你想测试它,你可以在这里做:https://www.jsonschemavalidator.net/