15

我想根据另一个字段中的数据为一个字段指定一个正则表达式模式。这可能吗?我试过 switch 和 $data 但不知道如何使用它们。例如,如果数据看起来像:

{
   "contacts":[
      {
         "mode":"Email",
         "contact":"john.doe@abc.com"
      },
      {
         "mode":"Phone",
         "contact":"111-555-1234"
      }
   ]
}

和架构看起来像:

"$schema":"http://json-schema.org/draft-04/schema#",
   "type":"object",
   "properties":{
      "Contacts":{
         "type":"array",
         "minItems":1,
         "items":{
            "type":"object",
            "properties":{
               "mode":{
                  "type":"string",
                  "enum":[
                     "Email",
                     "Phone"
                  ]
               },
               "contact":{
                  "type":"string",
                  "pattern":"?????"
               }
            },
            "required":[
               "mode",
               "contact"
            ]
         }
      }
   }
}

如何根据模式中的数据设置联系模式,以便如果模式是电子邮件,它会根据电子邮件格式的正则表达式验证联系人,如果模式是电话,它会根据电话格式的正则表达式验证联系人?我有每个的正则表达式。我需要选择其中一个的逻辑。

4

1 回答 1

24

有几种方法可以做到这一点

anyOf优点:draft-04 兼容,缺点:错误报告有点冗长 - 如果没有匹配项,您将从两个子模式中得到错误):

{
   "type": "object",
   "properties": {
      "Contacts": {
         "type": "array",
         "minItems": 1,
         "items": {
            "type": "object",
            "anyOf": [
               {
                  "properties": {
                     "mode": {"enum": ["Email"]},
                     "contact": {
                        "type": "string",
                        "format": "email"
                     }
                  }
               },
               {
                  "properties": {
                     "mode": {"enum": ["Phone"]},
                     "contact": {
                        "type": "string",
                        "pattern": "phone_pattern"
                     }
                  }
               }
            ],
            "required": ["mode", "contact"]
         }
      }
   }
}

if/then/else在 ajv-keywords 包中可用优点:错误报告更有意义,接受包含在 draft-07中,缺点:目前不标准):

{
   "type": "object",
   "properties": {
      "Contacts": {
         "type": "array",
         "minItems": 1,
         "items": {
            "type": "object",
            "properties": {
               "mode": {"type": "string", "enum": ["Email", "Phone"]},
               "contact": {"type": "string"}
            },
            "if": {
               "properties": {
                  "mode": {"enum": ["Email"]}
               }
            },
            "then": {
               "properties": {
                  "contact": {"format": "email"}
               }
            },
            "else": {
               "properties": {
                  "contact":  {"pattern": "phone_pattern"}
               }
            }
            "required": ["mode", "contact"]
         }
      }
   }
}

select在 ajv-keywords 包中可用优点:比 if/then/else 更简洁,特别是如果有两个以上可能的值,缺点尚未在标准轨道上,但您可以支持它:),需要启用 $数据参考和 Ajv v5.xx):

{
   "type": "object",
   "properties": {
      "Contacts": {
         "type": "array",
         "minItems": 1,
         "items": {
            "type": "object",
            "properties": {
               "mode": {"type": "string"},
               "contact": {"type": "string"}
            },
            "select": { "$data": "0/mode" },
            "selectCases": {
               "Email": {
                  "properties": {
                     "contact": {"format": "email"}
                  }
               },
               "Phone": {
                  "properties": {
                     "contact": {"pattern": "phone_pattern"}
                  }
               }
            },
            "selectDefault": false,
            "required": ["mode", "contact"]
         }
      }
   }
}

我更喜欢最后一个选项。

于 2017-03-20T21:21:55.440 回答