Skip to content

Default values don't get applied to objects inside of an array #417

Closed as not planned
@pcjacobse

Description

@pcjacobse

Hi,

It seems that the default values don't get applied to objects inside of an array.
It looks like that only the top level object gets their default values applied.

The validation in the sub-object does work (like type checking and required fields).

I'm running version 5.2.0

Example:
I expect the 'test' property to be filled with the default value if not present.

$data = [
    (object)[
        "key" => 1,
        "test" => 1
    ],(object)[
        "key" => 1,
    ]
];
$validator = new \JsonSchema\Validator();

var_dump($validator->validate(
    $data,
    json_decode('
    {
        "type": "array",
        "items": [
            {
                "type": "object",
                "properties": {
                    "key": {
                        "type": "integer"
                    },
                    "test": {
                        "type": "integer",
                        "default": 0
                    }
                },
                "required": [
                    "key"
                ]
            }
        ]
    }
    '), \JsonSchema\Constraints\Constraint::CHECK_MODE_APPLY_DEFAULTS
));
var_dump($data);

Gives:

int(0)

array(2) {
  [0]=>
  object(stdClass)#3 (2) {
    ["key"]=>
    int(1)
    ["test"]=>
    int(1)
  }
  [1]=>
  object(stdClass)#2 (1) {
    ["key"]=>
    int(1)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions