Ruby JSON 架构验证器未返回预期结果
Ruby JSON Schema Validator Not Returning Expected Result
我正在使用 JSON 模式验证器 gem 来验证响应是否返回预期的键和值类型。我的数据应该以以下 JSON 格式返回:
{
"hierarchies": [
{
"hierarchyId": "123ABC456DEF789",
"depth": 1,
"hierarchyNodes": [
{
"nodeId": "ANID123456FORNODE789",
"id": "12345678"
}
]
}
]
}
- "hierarchies"可以是0..N个元素的数组
- "hierarchyNodes"也可以是0..N个元素的数组
我当前的架构如下(我目前缺少节点数组):
schema = {
'$schema' => 'http://json-schema.org/draft-04/schema#',
type: 'object',
properties: {
hierarchies: {
type: 'array',
properties: {
object_items: {
type: 'object',
properties: {
hierarchyId: { type: 'string' },
depth: { type: 'integer' }
},
required: [:hierarchyId, :depth],
additionalProperties: false
}
},
required: [:object_items]
}
},
required: [:hierarchies]
}
出于某种原因,以下 returns 为真,而所有这些的预期结果都为假:
0> JSON::Validator.validate(schema, {hierarchies: [{}] })
=> true
0> JSON::Validator.validate(schema, {hierarchies: [{hierarchyId: 'hi', depth: 1, something: 'hey'}] })
=> true
0> JSON::Validator.validate(schema, {x: [{a: 'something'}] })
=> true
预期的 True 结果将具有以下架构,并且只有此架构:
JSON::Validator.validate(schema, {hierarchies: [{hierarchyId: 'id',
depth: 1}] })
谁能告诉我我在设置验证架构时哪里出了问题?
谢谢!
根据@tadman 提供的建议,我想到了这个解决方案:
schema = {
'$schema': 'http://json-schema.org/draft-06/schema#',
'properties': {
'hierarchies': {
'additionalItems': false,
'items': {
'additionalItems': false,
'properties': {
'depth': { 'type': 'integer' },
'hierarchyId': { 'type': 'string' },
'hierarchyNodes': {
'additionalItems': false,
'items': {
'additionalItems': false,
'properties': {
'id': { 'type': 'string' },
'nodeId': { 'type': 'string' }
},
'required': ['nodeId', 'id'],
'type': 'object'
},
'type': 'array'
}
},
'required': ['hierarchyId', 'depth', 'hierarchyNodes'],
'type': 'object'
},
'type': 'array'
}
},
'required': ['hierarchies'],
'type': 'object'
}
我正在使用 JSON 模式验证器 gem 来验证响应是否返回预期的键和值类型。我的数据应该以以下 JSON 格式返回:
{
"hierarchies": [
{
"hierarchyId": "123ABC456DEF789",
"depth": 1,
"hierarchyNodes": [
{
"nodeId": "ANID123456FORNODE789",
"id": "12345678"
}
]
}
]
}
- "hierarchies"可以是0..N个元素的数组
- "hierarchyNodes"也可以是0..N个元素的数组
我当前的架构如下(我目前缺少节点数组):
schema = {
'$schema' => 'http://json-schema.org/draft-04/schema#',
type: 'object',
properties: {
hierarchies: {
type: 'array',
properties: {
object_items: {
type: 'object',
properties: {
hierarchyId: { type: 'string' },
depth: { type: 'integer' }
},
required: [:hierarchyId, :depth],
additionalProperties: false
}
},
required: [:object_items]
}
},
required: [:hierarchies]
}
出于某种原因,以下 returns 为真,而所有这些的预期结果都为假:
0> JSON::Validator.validate(schema, {hierarchies: [{}] }) => true
0> JSON::Validator.validate(schema, {hierarchies: [{hierarchyId: 'hi', depth: 1, something: 'hey'}] }) => true
0> JSON::Validator.validate(schema, {x: [{a: 'something'}] }) => true
预期的 True 结果将具有以下架构,并且只有此架构:
JSON::Validator.validate(schema, {hierarchies: [{hierarchyId: 'id', depth: 1}] })
谁能告诉我我在设置验证架构时哪里出了问题?
谢谢!
根据@tadman 提供的建议,我想到了这个解决方案:
schema = {
'$schema': 'http://json-schema.org/draft-06/schema#',
'properties': {
'hierarchies': {
'additionalItems': false,
'items': {
'additionalItems': false,
'properties': {
'depth': { 'type': 'integer' },
'hierarchyId': { 'type': 'string' },
'hierarchyNodes': {
'additionalItems': false,
'items': {
'additionalItems': false,
'properties': {
'id': { 'type': 'string' },
'nodeId': { 'type': 'string' }
},
'required': ['nodeId', 'id'],
'type': 'object'
},
'type': 'array'
}
},
'required': ['hierarchyId', 'depth', 'hierarchyNodes'],
'type': 'object'
},
'type': 'array'
}
},
'required': ['hierarchies'],
'type': 'object'
}