根据一组灵活的规则验证 JSON 对象?
Validate JSON object against a flexible set of rules?
我正在使用 metosin/scjsv
验证 POST 请求的 json 正文。
我想根据以下规则进行验证:
- 映射对象中的元素键可以使用任何值,只要它符合以下正则表达式:
^[a-z_][a-z\d_]*$
- 只有对象:
{:attribute : {:type "string"}}
应被允许作为映射对象中元素的值。
这是一个有效 json 对象的示例:
{
"mapping" : {
"attr_a" : {
"attribute" : "a"
},
"attr_b" : {
"attribute" : "b"
}
}
}
这是我到目前为止定义的模式,它无法验证我想要的方式:
(def schema {:type "object"
:properties {:mapping {:type "object"
:pattern "^[a-z_][a-z\d_]*$"
{:$attr {:type "object"
:properties {:attribute {:type "string"}}
:required [:attribute]}}}}})
我应该使用 patternProperties
和 additionalProperties
。
(def schema {:type "object"
:properties {:mapping {:type "object"
:patternProperties {"^[a-z_][a-z\d_]*$" {:type "object"
:properties {:attribute {:type "string"}}
:required [:attribute]}}}}})
我正在使用 metosin/scjsv
验证 POST 请求的 json 正文。
我想根据以下规则进行验证:
- 映射对象中的元素键可以使用任何值,只要它符合以下正则表达式:
^[a-z_][a-z\d_]*$
- 只有对象:
{:attribute : {:type "string"}}
应被允许作为映射对象中元素的值。
这是一个有效 json 对象的示例:
{
"mapping" : {
"attr_a" : {
"attribute" : "a"
},
"attr_b" : {
"attribute" : "b"
}
}
}
这是我到目前为止定义的模式,它无法验证我想要的方式:
(def schema {:type "object"
:properties {:mapping {:type "object"
:pattern "^[a-z_][a-z\d_]*$"
{:$attr {:type "object"
:properties {:attribute {:type "string"}}
:required [:attribute]}}}}})
我应该使用 patternProperties
和 additionalProperties
。
(def schema {:type "object"
:properties {:mapping {:type "object"
:patternProperties {"^[a-z_][a-z\d_]*$" {:type "object"
:properties {:attribute {:type "string"}}
:required [:attribute]}}}}})