java 用于检查 YYYY-MM-DD 日期的 JSON 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43347014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:21:43  来源:igfitidea点击:

JSON Schema to check date of YYYY-MM-DD

javajsonjsonschema

提问by user6543599

Could someone tell me JSON Schema Validation for accepting date of YYYY-MM-DDformat alone?

有人能告诉我 JSON Schema ValidationYYYY-MM-DD仅接受格式日期吗?

My sample JSON:

我的示例 JSON:

{"data1" : "foo", "date" :"2016-11-24"}

回答by Ram Babu S

JSON Schema already have defined format for date, time, datetime, email, hostname, IP address. You can prefer this easier and recommended method rather than writing your own regex.

JSON Schema 已经定义了日期、时间、日期时间、电子邮件、主机名、IP 地址的格式。您可以更喜欢这种更简单和推荐的方法,而不是编写自己的正则表达式。

"date": {
  "type": "string",
  "format": "date"
}

Date and time format names are derived from RFC 3339, section 5.6 [RFC3339].

日期和时间格式名称源自 RFC 3339,第 5.6 节 [RFC3339]。

Reference: http://json-schema.org/latest/json-schema-validation.html#rfc.section.7.3

参考:http: //json-schema.org/latest/json-schema-validation.html#rfc.section.7.3

回答by Alekhya

add regex to json schema in schema use the following.

将正则表达式添加到架构中的 json 架构使用以下内容。

{
   "type": "string",
   "pattern": "^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$"
}