【问题标题】:jsonschema validation: value for a specific field should be limited to the values entered in another fieldjsonschema 验证:特定字段的值应限制为在另一个字段中输入的值
【发布时间】:2021-07-05 21:15:43
【问题描述】:

我想知道 jsonschema 是否可以验证特定字段的值,该字段仅限于在另一个字段中输入的值。例如:

{
 "type": "object",
 "properties": {
  "someStrings": {
   "type": "array",
   "title": "some strings",
   "items": {
    "type": "string"
   }
  },
  "chooseOneOfSomeStrings": {
   "type": "string",
   "limitedTo": "someStrings" // or whatever the verbiage to implement this
  }
 }
}

这样

如果我为“someStrings”输入“red”、“blue”、“green”,“chooseOneOfSomeStrings”如果为“red”则有效,如果为“yellow”则无效。

【问题讨论】:

    标签: jsonschema json-schema-validator ajv react-jsonschema-forms


    【解决方案1】:

    您要查找的关键字是“枚举”。但是这个允许值列表需要在架构本身中——它不能在您正在验证的数据中。

    ...
      "chooseOneOfSomeStrings": {
       "type": "string",
       "enum": [ "red", "blue", "green" ]
      }
    

    【讨论】:

    • 我不能使用enum,因为它取决于someStrings 上的动态选择。看起来我需要制作一个自定义验证器。
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 2011-09-20
    • 2012-02-09
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多