Skip to content

AdditionalPropertiesConstraint reports incorrect error path when additionalProperties is a schema #919

@DannyvdSluijs

Description

@DannyvdSluijs

Description

When additionalProperties is a schema object (rather than false), each additional property value is validated against that schema — but the path is not incremented with the property key. Any validation errors from the sub-schema check are reported at the parent object's path instead of the specific property's path.

Affected Files

  • src/JsonSchema/Constraints/Drafts/Draft06/AdditionalPropertiesConstraint.php
  • src/JsonSchema/Constraints/Drafts/Draft07/AdditionalPropertiesConstraint.php
  • src/JsonSchema/Constraints/Drafts/Draft2019/AdditionalPropertiesConstraint.php

All three have a @todo comment acknowledging this:

$schemaConstraint->check($additionalPropertiesValue, $schema->additionalProperties, $path, $i); // @todo increment path

Steps to Reproduce

{
  "properties": { "foo": {} },
  "additionalProperties": { "type": "integer" }
}

Validating {"foo": "ok", "bar": "not-an-integer"} produces a type error at path `` (root) instead of /bar.

Expected Behaviour

The error path should point to the specific additional property that failed — e.g. /bar — not to the parent object.

Suggested Fix

Increment the path with the property key before passing it to the sub-schema check:

$schemaConstraint->check($additionalPropertiesValue, $schema->additionalProperties, $path->withAppendedPath($key), $i);

This depends on a path utility being available on JsonPointer — see issue #918 which tracks the same missing utility in RequiredConstraint.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions