根据一组灵活的规则验证 JSON 对象?

Validate JSON object against a flexible set of rules?

我正在使用 metosin/scjsv 验证 POST 请求的 json 正文。

我想根据以下规则进行验证:

这是一个有效 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]}}}}})

我应该使用 patternPropertiesadditionalProperties

(def schema {:type       "object"
     :properties {:mapping {:type    "object"
                            :patternProperties {"^[a-z_][a-z\d_]*$" {:type       "object"
                                     :properties {:attribute {:type "string"}}
                                     :required   [:attribute]}}}}})