Skip to content

oneOf never validates when one item is array type the other is scalar or null when checkMode = Constraint::CHECK_MODE_COERCE_TYPES #770

Closed
@jozefbriss

Description

@jozefbriss

It seems that coercing scalar to array causes than oneOf never validates when one item is array type and the other is scalar or null.

The reason is that any scalar or null can be coerced to array.

Maybe there should not be toArray coercion.

$jsonSchema = <<<'JSON'
{
    "type": "object",
    "properties": {
        "data": {
            "oneOf": [{ "type": "string" }, { "type": "array" }]
        }
    },
    "required": ["data"]
}
JSON;

$jsonSchemaObject = json_decode($jsonSchema);

$schemaStorage = new SchemaStorage();
$schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);

$jsonValidator = new Validator(new Factory($schemaStorage));

$jsonToValidateObject = json_decode('{"data":"ABC"}');

$jsonValidator->validate($jsonToValidateObject, $jsonSchemaObject, Constraint::CHECK_MODE_COERCE_TYPES);

print_r($jsonValidator->getErrors());

results in:

Array
(
    [0] => Array
        (
            [property] => data
            [pointer] => /data
            [message] => Failed to match exactly one schema
            [constraint] => Array
                (
                    [name] => oneOf
                    [params] => Array
                        (
                        )

                )

            [context] => 1
        )

)

version: 6.0.0

Maybe the issues is more generic than only related to array and "oneOf" and Constraint::CHECK_MODE_COERCE_TYPES may not work together.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions