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

如何使用AJV 6.12.6验证可为null且非空的URI字符串?

AJV 6.12.6 URI验证方案:允许null或非空合法URI,禁止空字符串

你当前的Schema存在两个问题:一是未限制空字符串,导致空串能通过string类型检查;二是then块里最后一个属性后多了冗余逗号,可能引发解析错误。

修正后的Schema如下:

agreementURL: {
  "type": ["string", "null"],
  "if": {
    "type": "string"
  },
  "then": {
    "format": "uri",
    "minLength": 1
  }
}

关键规则说明:

  • type: ["string", "null"]:明确值仅允许为字符串或null
  • 当值为字符串时,then块强制两个约束:
    • format: "uri":必须符合标准URI格式
    • minLength: 1:严格禁止空字符串

另外,要确保AJV实例启用了格式验证能力,初始化时可配置:

const ajv = new Ajv({ format: 'full' });

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 22:34:56