【问题标题】:JSON schema: date greater than an otherJSON模式:日期大于另一个
【发布时间】:2014-11-13 10:07:12
【问题描述】:

我有一个这样的 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

【问题讨论】:

    标签: node.js jsonschema json-schema-validator


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您无法在 JSON 架构级别上执行此操作。您必须为您的 Operation 对象单独验证。一般来说,JSON-Schema 只提供一种“格式良好”的健全性检查:关于属性是数字、日期或匹配正则表达式的字符串;或关于具有某些嵌套属性结构的对象。应该在其他地方控制更高级的业务规则,例如您的示例中的规则。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-17
        • 1970-01-01
        • 1970-01-01
        • 2012-05-26
        • 2014-10-12
        • 1970-01-01
        • 2016-11-21
        • 2016-06-12
        相关资源
        最近更新 更多