假设我有这样一个JSON对象,它有一个包含各种对象的数组,比如:
{
"array": [
{
"type": "type_1",
"value": 5
},
{
"type": "type_2",
"kind": "person"
}
]
}
根据JSON模式验证,我可以使用以下JSOM模式定义来验证此模式:
{
"type": "object",
"properties": {
"array": {
"type": "array",
"items": {
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"type_1"
]
},
"value": {
"type": "integer",
"enum": [
5
]
}
}
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"type_2"
]
},
"kind": {
"type": "string",
"enum": [
"person"
]
}
}
}
]
}
}
}
}
如何使用dry-schema gem验证输入JSON?你有什么想法吗?
1条答案
按热度按时间5jvtdoz21#
要解决这个问题,请尝试以下代码:
这里的关键是方法
#schema
。Documentation