Skip to content

additionalProperties scope into $dynamicRefs #163

@dustinwilson

Description

@dustinwilson
$schema = <<<'JSON'
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "additionalProperties": false,
    "type": "object",
    "properties": {
        "foo": {
            "$ref": "#/$defs/nested"
        },
        "version": {
            "type": "integer"
        }
    },
    "$defs": {
        "nested": {
            "$dynamicAnchor": "nested",
            "type": "object",
            "properties": {
                "+bar": { "type": "string" }
            },
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "$dynamicRef": "#nested"
                }
            }
        }
    }
}
JSON;

$data = (object)[
    'version' => 1,
    'foo' => (object)[
        'ook' => (object)[
            '+bar' => 'eek'
        ]
    ]
];

$validator = new Validator();
$result = $validator->validate($data, $schema);
$errors = [];
if (!$result->isValid()) {
    $formatter = new ErrorFormatter();
    foreach ($formatter->format($result->error(), true) as $path => $messages) {
        $m = json_encode($messages);
        echo "$path: $m\n";
    }
}

Output:

/foo/ook: ["Additional object properties are not allowed: +bar"]

According to the JSON Schema 2020-12 specification (section 10.3.2.3), additionalProperties behavior depends on the annotation results of properties and patternProperties within the same schema object.

That means in this example above when additionalProperties is set in the root, it shouldn't apply to the def, but in this library it does. The same schema passes in tools like JSON Schema Validator.

It seems from a passing glance at the code in SchemaParser.php, it's only checking for $anchors and not $dynamicAnchors. I'm sure there's more to it than that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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