JSON Schema - 允许使用正则表达式模式的null

时间:2017-08-15 16:10:49

标签: json jsonschema

希望在使用正则表达式验证日期格式的可选日期属性上允许null。这甚至可能吗?

"dateOfRetirement": {
  "description": "Optional. Format: yyyy-MM-dd.",
  "type": ["string", "null"],
  "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
} 

1 个答案:

答案 0 :(得分:2)

要获得常规语法,您必须为正则表达式添加条件。

你的正则表达式会得到(假设你的正则表达式语法没有错误!):

^(\\d{4}-\\d{2}-\\d{2}|null)$

已完成的步骤:

  • 用括号(()
  • 封装普通正则表达式
  • -operator添加到正则表达式(|
  • null的第二个验证添加到or-operator
  • 之后的正则表达式中

最后,正则表达式将允许使用有效日期格式或null作为文本。