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"}
It should only accept YYYY-MM-DD. "startdate": { "type":"string", "format": "date", "required":true }, Like. Answer.
Format. The format keyword allows for basic semantic identification of certain kinds of string values that are commonly used. For example, because JSON doesn't have a “DateTime” type, dates need to be encoded as strings.
JSON Schema ExampleYou will use this to give a title to your schema. A little description of the schema. The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. Defines various keys and their value types, minimum and maximum values to be used in JSON file.
JSON Schema is an IETF standard providing a format for what JSON data is required for a given application and how to interact with it. Applying such standards for a JSON document lets you enforce consistency and data validity across similar JSON data.
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
add regex to json schema in schema use the following.
{
"type": "string",
"pattern": "^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$"
}
Use built-in support for date validation:
{"type": "string", "format": "date"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With