根据一组灵活的规则验证JSON对象?(Validate JSON object against a flexible set of rules?)

编程入门 行业动态 更新时间:2024-10-27 08:37:34
根据一组灵活的规则验证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]}}}}})

更多推荐

本文发布于:2023-08-06 17:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:灵活   对象   规则   JSON   Validate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!