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

如何配置JSON Schema使Avatar仅为false或指定对象类型

Fixing JSON Schema for Avatar: Restrict to Only false or Valid Object

The issue with your original schema is that type: ["object", "boolean"] allows any boolean value (true or false), but you need to lock it down to only false when using the boolean option. Here's how to adjust your schema to enforce this requirement:

const avatarSchema = {
  "oneOf": [
    {
      "type": "boolean",
      "const": false
    },
    {
      "type": "object",
      "required": ["name", "image"], // Optional: uncomment if these fields are mandatory for the avatar object
      "additionalProperties": false,
      "properties": {
        "name": { "type": "string" },
        "image": { "type": "string", "format": "url" }
      }
    }
  ]
};

Key Changes Explained:

  • oneOf keyword: This tells the validator that the value must match exactly one of the subschemas defined inside it—perfect for your "either false or object" use case.
  • Boolean constraint: The first subschema uses const: false alongside type: "boolean" to explicitly restrict the boolean option to only false (no more allowing invalid true values).
  • Object validation: The second subschema retains your original object structure. I added a note about required: ["name", "image"]—if those fields are mandatory for a visible avatar, uncommenting this line will ensure they're always present (your original schema had an empty required array, so adjust based on your actual needs).

This setup will now correctly reject true as a valid Avatar value, while still accepting either false (to hide the avatar) or a properly structured object with name and image fields.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:38:28