You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JSON Schema确保start时间始终小于等于end时间?

能否通过JSON Schema确保start时间≤end时间?

可以实现,但得用JSON Schema 2019-09及更新的版本——旧版本比如Draft 07及之前不支持跨属性的引用验证,做不到这一点。

你只需要在原Schema的基础上,给end字段的规则加上$data引用,把start的值设为它的最小值,就能强制end始终大于等于start。修改后的Schema如下:

"period": {
  "type": "object",
  "properties": {
    "start": {
      "description": "Unixtime",
      "type": "number",
      "minimum": 0
    },
    "end": {
      "description": "Unixtime",
      "type": "number",
      "minimum": { "$data": "1/start" }
    }
  },
  "required": ["start", "end"]
}

这里的"$data": "1/start"是核心:它会动态引用当前对象(层级1)下start字段的值,作为end的最小值限制。

如果你需要更灵活的逻辑(比如仅在某些条件下触发验证),也可以用if/then结构:

"period": {
  "type": "object",
  "properties": {
    "start": {
      "description": "Unixtime",
      "type": "number",
      "minimum": 0
    },
    "end": {
      "description": "Unixtime",
      "type": "number",
      "minimum": 0
    }
  },
  "required": ["start", "end"],
  "if": {
    "properties": { "start": { "type": "number" } }
  },
  "then": {
    "properties": {
      "end": { "minimum": { "$data": "1/start" } }
    }
  }
}

这种写法会在start是数字类型时,强制end不小于start,逻辑更清晰。

内容的提问来源于stack exchange,提问作者truf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:15:01