JSON schema: date greater than an other

天涯浪子 提交于 2019-12-10 14:47:31

问题


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 ?


回答1:


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.




回答2:


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"
        }
    }
}


来源:https://stackoverflow.com/questions/26906193/json-schema-date-greater-than-an-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!