根据一组灵活的规则验证JSON对象?(Validate JSON object against a flexible set of rules?)
我使用metosin/scjsv来验证POST请求的json正文。
我想验证以下规则:
只要符合这个正则表达式,映射对象中的元素键就应该被允许: ^[a-z_][az\\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]}}}}})I am use metosin/scjsv to validate the json body of a POST request.
I would like to validate against the following rules:
Any value should be allowed for the element key within the mapping object so long as it conforms to this regex: ^[a-z_][a-z\\d_]*$ Only the object: {:attribute : {:type "string"}} should be allowed as the value of an element within the mapping object.This is an example of a valid json object:
{ "mapping" : { "attr_a" : { "attribute" : "a" }, "attr_b" : { "attribute" : "b" } } }Here is the schema I have defined so far, it fails to valid how I would like it to:
(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]}}}}})I should have used patternProperties and additionalProperties.
(def schema {:type "object" :properties {:mapping {:type "object" :patternProperties {"^[a-z_][a-z\\d_]*$" {:type "object" :properties {:attribute {:type "string"}} :required [:attribute]}}}}})更多推荐
发布评论