JSON模式:date大于其他

我有这样的JSON模式:

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

我怎样才能在我的模式中说dateFin必须大于dateDebut

你不能在JSON模式级别上做到这一点。 您必须分别validation您的Operation对象。 一般来说,JSON-Schema只提供一种“格式良好”的合理性检查:关于属性是数字,date或匹配正则expression式的string; 或关于具有特定嵌套结构属性的对象。 更高级的业务规则,比如你的例子中的那个,应该在其他地方进行控

这个库支持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" } }