2

我有一个 JSON 模式验证器,我需要在其中检查特定字段email以查看它是否是 4 封可能的电子邮件之一。让我们称之为可能性['test1', 'test2', 'test3', 'test4']。有时电子邮件包含一个\n新的行分隔符,所以我也需要考虑这一点。是否可以在 JSON Schema 中执行字符串包含方法?

这是我没有电子邮件检查的架构:

{
  "type": "object",
  "properties": {
    "data": {
        "type":"object",
        "properties": {
            "email": {
                "type": "string"
            }
        },
    "required": ["email"]
    }
  }
}

我的输入有效载荷是:

{
  "data": {
      "email": "test3\njunktext"
      }
}

我需要以下有效负载来通过验证,因为它包含test3在其中。谢谢!

4

1 回答 1

0

我可以想到两种方法:

使用enum您可以定义有效电子邮件的列表:

{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "email": {
          "enum": [
            "test1",
            "test2",
            "test3"
          ]
        }
      },
      "required": [
        "email"
      ]
    }
  }
}

或者使用允许您使用正则表达式来匹配有效电子邮件的模式:

{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "email": {
          "pattern": "test"
        }
      },
      "required": [
        "email"
      ]
    }
  }
}
于 2018-12-03T20:49:08.750 回答