Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
asaliev opened this issue Dec 5, 2018 · 0 comments
Open

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

asaliev opened this issue Dec 5, 2018 · 0 comments

Comments

@asaliev
Copy link

asaliev commented Dec 5, 2018

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.

@asaliev asaliev changed the title Not-enum will fail when "CHECK_MODE_EXCEPTIONS" NOT ENUM can unexpectedly fail when "CHECK_MODE_EXCEPTIONS" is set Dec 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant