Skip to content

Validation of [] for type map with all fields optional will fail #17

@LoogleCZ

Description

@LoogleCZ

Description

Data type map does not work as expected when all fields are optional and I submit empty array for validation. See steps to reproduce for more details. I expected that this will be valid.

Steps to reproduce

If I will use following schema for validator:

$schema = [
    'type' => 'map',
    'properties' => [
        'test' => [
            'enum' => [
                "val1",
                true,
                "415646",
            ],
            'optional' => true,
        ],
    ],
];

$validator = new Validator($schema);

and then call

$res = $schema->validate([]);
var_dump($res->getErrors());

I will get "Wrong data type in '/'; expected 'map'; got 'array'"

Suggested fix

  1. Update Validator::validate to handle empty array as map as well as array
  2. use oneOf directive over schema (have not tried).

First approach makes more sence for me.

PS: I can fix this issue and propose PR to schematicon/validator-php repo. This PR will come probably in next week if approved

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