JsonSchemaValidationError data schema_error ai_generated true

JSON Schema validation fails: additional properties not allowed when schema has 'additionalProperties: false'

ID: data/json-schema-additional-properties-true

Also available as: JSON · Markdown · 中文
90%Fix Rate
92%Confidence
1Evidence
2023-09-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
JSON Schema Draft-07+ active
ajv 8.12+ active
jsonschema 4.20+ active
OpenAPI 3.0+ active

Root Cause

A JSON Schema explicitly sets 'additionalProperties: false' to restrict the object to only defined properties, but the input JSON contains properties not listed in the schema's 'properties' keyword.

generic

中文

JSON Schema显式设置'additionalProperties: false'以将对象限制为仅定义的属性,但输入JSON包含模式'properties'关键字中未列出的属性。

Official Documentation

https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties

Workarounds

  1. 95% success Modify the input JSON to remove the extra properties before validation. For Python: `import json; data = json.loads(input_string); for key in list(data.keys()): if key not in schema['properties']: del data[key];`
    Modify the input JSON to remove the extra properties before validation. For Python: `import json; data = json.loads(input_string); for key in list(data.keys()): if key not in schema['properties']: del data[key];`
  2. 85% success Use 'unevaluatedProperties: false' instead of 'additionalProperties: false' if using JSON Schema 2019-09 or later. This allows properties defined via $ref or allOf to be evaluated correctly.
    Use 'unevaluatedProperties: false' instead of 'additionalProperties: false' if using JSON Schema 2019-09 or later. This allows properties defined via $ref or allOf to be evaluated correctly.
  3. 80% success Add the specific extra property to the schema with an appropriate type and description, or use 'additionalProperties' with a type constraint: `"additionalProperties": {"type": "string"}` to allow extra properties but enforce their types.
    Add the specific extra property to the schema with an appropriate type and description, or use 'additionalProperties' with a type constraint: `"additionalProperties": {"type": "string"}` to allow extra properties but enforce their types.

中文步骤

  1. 在验证之前修改输入JSON以删除多余属性。对于Python:`import json; data = json.loads(input_string); for key in list(data.keys()): if key not in schema['properties']: del data[key];`
  2. 如果使用JSON Schema 2019-09或更高版本,使用'unevaluatedProperties: false'代替'additionalProperties: false'。这允许通过$ref或allOf定义的属性被正确评估。
  3. 将特定的额外属性添加到模式中,附带适当的类型和描述,或使用带类型约束的'additionalProperties':`"additionalProperties": {"type": "string"}` 以允许额外属性但强制其类型。

Dead Ends

Common approaches that don't work:

  1. 50% fail

    This undermines the purpose of strict validation and may allow malicious or malformed data through.

  2. 60% fail

    This is impractical for dynamic or extensible APIs; also, it does not catch typos or unintended properties.

  3. 40% fail

    PatternProperties are evaluated independently of additionalProperties; if both are present, additionalProperties applies to properties not matching any pattern.