JSON Schema to check date of YYYY-MM-DD

user6543599 picture user6543599 · Apr 11, 2017 · Viewed 18k times · Source

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

My sample JSON:

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

Answer

Ram Babu S picture Ram Babu S · Feb 7, 2018

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.

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

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

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