Skip to content

NOT ENUM can unexpectedly fail when "CHECK_MODE_EXCEPTIONS" is set #552

Closed as not planned
@asaliev

Description

@asaliev

Version: 5.2.7

What happened
Unexpected behavior when using the CHECK_MODE_EXCEPTIONS mode combined with a "not": { "enum": ["bar"] } schema, will result in a thrown exception

Consider the following example:

<?php

require_once 'vendor/autoload.php';

$schema = [
    '$schema' => "http://json-schema.org/draft-04/schema#",
    "type" => "object",
    "properties" => [
        "foo" => [
            "type" => "string",
            "not" => [
                "enum" => ["bar"]
            ]
        ]
    ]
];

$data = (object)[
    "foo" => "baz"
];

$validator = new JsonSchema\Validator;
$result = $validator->validate($data, $schema, JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS);

This will fail unexpectedly with the error message PHP Fatal error: Uncaught JsonSchema\Exception\ValidationException: Error validating /foo/foo: Does not have a value in the enumeration ["bar"] in /path/to/json-schema/src/JsonSchema/Constraints/BaseConstraint.php:57 which is not the expected behavior, since if you run the validator without the CHECK_MODE_EXCEPTIONS constraint, there will be no error.

What I expected to happen
I think the expected behavior should be when using CHECK_MODE_EXCEPTIONS mode, the not constraint should not fail instantly if the child constraint failed, since not has to negate the results first.

Possible solution
Looks like it's because of UndefinedConstraint is not aware of the CHECK_MODE_EXCEPTIONS during checking the not construct and when it attempts to match bar vs an enum of [baz] further down the chain it fails with an exception which propagates all the way out of the validator.

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