Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema: date greater than an other

I've a json schema like this:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Operation",
  "description": "The schema of an operation",
  "type": "object",
  "properties": {
    "id":{
      "description": "Unique identifier of the service",
      "type": "string"
    },
    "description":{
      "type": "string"
    },
    "dateDebut":{
      "type": "string",
      "format": "date-time"
    },
    "dateFin":{
      "type": "string",
      "format": "date-time"
    }
  }
}

How can I say in my schema that the dateFin must be greater than the dateDebut ?

like image 693
Djiby Thiaw Avatar asked Nov 13 '14 10:11

Djiby Thiaw


2 Answers

This library supports it https://github.com/epoberezkin/ajv#features

var ajv = Ajv({v5:true,allErrors: true})

{
    "startDate": {
        "format": "date",
        "message": "Please Enter correct date format YYYY-MM-DD"
    },
    "endDate": {
        "format": "date",
        "message": "Please Enter correct date format YYYY-MM-DD",
        "formatMinimum": {
            "$data": "1/startDate"
        }
    }
}
like image 83
Bolarinwa Avatar answered Nov 30 '22 07:11

Bolarinwa


You can't do that on the JSON-Schema level. You'd have to validate that separately for your Operation objects. In general, JSON-Schema only provides a kind of "well-formed-ness" sanity checks: about a property being a number, date, or a string matching a regexp; or about an object having certain nested structure of properties. More advanced business rules like the one from your example should be controlled elsewhere.

like image 28
Ivan Krechetov Avatar answered Nov 30 '22 06:11

Ivan Krechetov