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

如何使JSON Schema枚举选择器支持输入自定义非枚举值?

实现支持自定义输入的枚举选择器

要让你的选择器既能选预设的fox、duck,又能允许用户输入horse这类自定义值,有两种实用方案:

方法1:纯JSON Schema兼容写法

这种方案能让所有支持Draft-07的校验工具都认可,同时兼容枚举值和自定义字符串输入:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "",
  "type": "object",
  "required": [],
  "properties": {
    "Animals": {
      "title": "Animals",
      "span": 6,
      "oneOf": [
        {
          "type": "string",
          "enum": [
            "fox",
            "duck"
          ]
        },
        {
          "type": "string",
          "not": {
            "enum": [
              "fox",
              "duck"
            ]
          }
        }
      ]
    }
  }
}

方法2:UI工具扩展配置(更推荐)

如果你用的是react-jsonschema-form这类表单渲染工具,直接添加UI配置就能把下拉框改成可输入的组合框:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "",
  "type": "object",
  "required": [],
  "properties": {
    "Animals": {
      "type": "string",
      "enum": [
        "fox",
        "duck"
      ],
      "title": "Animals",
      "span": 6,
      "ui:widget": "combobox"
    }
  }
}

补充说明

  • 方法1是纯Schema层面的逻辑,校验规则通用,但部分UI工具可能不会自动渲染为可输入下拉框,需额外调整UI设置。
  • 方法2依赖具体表单库,配置简洁直接,用户可直接在下拉框中选择预设选项或输入自定义内容,体验更顺畅。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 12:42:48