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

AJV严格模式警告触发原因与修复方法咨询

AJV严格模式警告修复方案

现有表单字段配置JSON Schema校验逻辑完全符合业务预期,但开启AJV严格模式后会抛出两类警告:strictTypes(使用对象专属关键字但未显式声明object类型)、strictRequired(required声明的属性未在当前schema节点定义)。

AJV严格模式对应判定规则

  • 所有使用properties、required、additionalProperties、patternProperties等仅对对象类型生效的校验关键字的schema节点,必须显式声明type: "object",否则触发strictTypes警告
  • 任意schema节点中通过required声明为必填的属性,必须在当前节点(或当前节点通过allOf/anyOf/oneOf合并的子schema范围内)的properties中明确定义,否则触发strictRequired警告

修复后的完整Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "title": "Form Field configuration",
  "properties": {
    "title": {
      "type": "string",
      "title": "The field label"
    },
    "description": {
      "type": "string",
      "title": "The field description"
    },
    "presentation": {
      "additionalProperties": false,
      "type": "object",
      "title": "Configuration for the field visual UI",
      "properties": {
        "inputType": {
          "title": "The recommend way to represent the field visually.",
          "anyOf": [
            {
              "const": "text",
              "title": "Similar to the HTML `<input>` with text type."
            },
            {
              "const": "number",
              "title": "Similar to the HTML `<input>` with number type."
            },
            {
              "const": "money",
              "title": "Similar to 'number', dedicated to monetary values."
            }
          ]
        },
        "currency": {
          "title": "For inputType=money. Monetary Currency Code (3 digits)",
          "maxLength": 3,
          "minLength": 3,
          "type": "string"
        },
        "description": {
          "type": "string",
          "title": "The field description with HTML"
        }
      }
    },
    "$template": {
      "type": "string",
      "title": "Pass the template URL to a given field"
    }
  },
  "required": ["presentation"],
  "allOf": [
    {
      "$comment": "Extend the base Draft-07 configuration",
      "$ref": "http://json-schema.org/draft-07/schema#"
    },
    {
      "$comment": "Add presentation validations only when $template is not present.",
      "if": {
        "type": "object",
        "required": ["$template"],
        "properties": {
          "$template": {}
        }
      },
      "else": {
        "type": "object",
        "properties": {
          "presentation": {
            "$ref": "#/$defs/presentation_validations"
          }
        }
      }
    },
    {
      "type": "object",
      "if": {
        "$comment": "If it contains presentation.description, then [root].description is also required.",
        "type": "object",
        "properties": {
          "presentation": {
            "type": "object",
            "required": ["description"],
            "properties": {
              "description": {}
            }
          }
        }
      },
      "then": {
        "type": "object",
        "required": ["description"],
        "properties": {
          "description": {}
        }
      }
    }
  ],
  "$defs": {
    "presentation_validations": {
      "type": "object",
      "allOf": [
        {
          "type": "object",
          "required": ["inputType"],
          "properties": {
            "inputType": {}
          }
        },
        {
          "type": "object",
          "if": {
            "type": "object",
            "properties": {
              "inputType": { "const": "money" }
            },
            "required": ["inputType"]
          },
          "then": {
            "type": "object",
            "required": ["currency"],
            "properties": {
              "currency": {}
            }
          },
          "else": {
            "type": "object",
            "properties": { "currency": false }
          }
        }
      ]
    }
  }
}

修改点说明

所有修改均不改变原有校验逻辑,仅补充严格模式要求的显式声明:

  • 给所有使用required/properties关键字的子schema节点补充type: "object"声明,解决所有strictTypes警告
  • 给所有required关键字涉及的属性,在对应schema节点的properties中补充空对象声明(空对象不新增额外校验规则,仅告知AJV该属性在当前节点存在),解决所有strictRequired警告
  • 原有条件校验、必填规则、字段约束完全保留,校验行为和修改前完全一致

内容的提问来源于stack exchange,提问作者sandrina-p

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 23:06:23