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

如何在JSON Schema中用patternProperties限定键为URL格式?

JSON Schema 实现额外属性键为URL格式的验证

你可以通过以下方式修改JSON Schema,利用patternProperties来限制额外属性的键必须符合URL格式:

修改后的完整Schema

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "patternProperties": {
        "^https?://.+$": {
            "type": "string"
        }
    },
    "additionalProperties": false
}

关键说明

  • patternProperties:通过正则表达式^https?://.+$匹配属性键,这个正则会匹配以http://或https://开头的任意URL格式字符串。
  • additionalProperties: false:这一步是核心,它会拒绝所有不匹配patternProperties规则的属性键,确保只有符合URL格式的键能通过验证。

验证效果

  • 符合要求的JSON(键为URL)会通过验证:
{
    "https://google.com": "google"
}
  • 不符合要求的JSON(键不是URL)会被拒绝:
{
    "google": "website"
}

内容的提问来源于stack exchange,提问作者Tristan F.-R.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 12:40:52