Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema to check date of YYYY-MM-DD

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"}
like image 343
user6543599 Avatar asked Apr 11 '17 13:04

user6543599


People also ask

How do you specify date format in JSON Schema?

It should only accept YYYY-MM-DD. "startdate": { "type":"string", "format": "date", "required":true }, Like. Answer.

Is date valid type in JSON?

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.

What is JSON Schema example?

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.

Is there a schema for JSON?

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.


3 Answers

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

like image 109
Ram Babu Avatar answered Oct 22 '22 00:10

Ram Babu


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])$"
}
like image 3
Alekhya Avatar answered Oct 22 '22 02:10

Alekhya


Use built-in support for date validation:

{"type": "string", "format": "date"}
like image 3
medusasoftware Avatar answered Oct 22 '22 01:10

medusasoftware