首页 文章

JSON Schema - 根据输入属性指定字符串长度

提问于
浏览
3

是否可以使用JSON Schema指定一个字符串长度,该长度取决于正在验证的项目中的属性值?

例如,我有一个带有值为3的“foo”属性的文档 . 我想确保“bar”属性是一个至少大小为3的字符串

Sample JSON

{
"foo": 3,
"bar": "111"
}

JSON Schema

{
   "properties" : {
      "foo": {
         "type": "integer",
         "minimum": 1
       }
      "bar": {
         "type": "string",
         "minLength": "{$foo}"
       }
   }
}

1 回答

  • 2

    有一个v5 proposal for a $data keyword将“允许模式使用数据中的值,使用JSON指针或相对JSON指针指定” .

    使用你的例子:

    {
       "properties" : {
          "foo": {
             "type": "integer",
             "minimum": 1
           }
          "bar": {
             "type": "string",
             "minLength": { "$data": "1/foo" }
           }
       }
    }
    

    $data 关键字的支持显然取决于您使用的验证器 . 一些验证器确实支持v5提案 .

相关问题