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

Validation stops on first error #107

Open
dontub opened this issue Jun 13, 2022 · 6 comments
Open

Validation stops on first error #107

dontub opened this issue Jun 13, 2022 · 6 comments

Comments

@dontub
Copy link
Contributor

dontub commented Jun 13, 2022

Currently the validation stops if an error occurs.


So if for example a required property is missing, the available properties are not validated at all. Actually we need to have all object properties/array items to be validated.
Maybe it would make sense to have different maximum error limits:

  • Per leaf data (e.g. type is number, but a string is given. Then it makes sense to stop the validation for this data without checking further keywords like minimum.)
  • Per object properties/array items (this is already possible)
  • Overall
@dontub
Copy link
Contributor Author

dontub commented Aug 9, 2022

For our project we are depending on this. (Not on the suggested error limits, though.) We'd like to avoid a custom fork so we would highly appreciate if a solution for this issue can be found.

Ping @sorinsarca :-)

@dontub
Copy link
Contributor Author

dontub commented Aug 23, 2022

This code in ObjectSchema would solve the issue:

protected function applyKeywords(array $keywords, ValidationContext $context): ?ValidationError
{
    $errors = [];
    foreach ($keywords as $keyword) {
        if (null !== ($error = $keyword->validate($context, $this))) {
            $errors[] = $error;
        }
    }

    if ([] === $errors) {
        return null;
    }

    if (1 === \count($errors)) {
        return $errors[0];
    }

    /** @var Schema $schema */
    $schema = $context->schema();

    return new ValidationError(
        'schema',
        $schema,
        DataInfo::fromContext($context),
        'The data does not match the schema',
        ['data' => $context->currentData()],
        $errors
    );
}

The part

if (1 === \count($errors)) {
    return $errors[0];
}

isn't necessary, though if there's only one error the behavior is as before.

Edit: Changed error message to not contain {data} because Opis\JsonSchema\Errors\ErrorFormatter doesn't support \stdClass:

return (string) $value;

(Showing the whole JSON in the error message probably makes no sense in most cases...)

@matapatos
Copy link

I agree with @dontub.

Tried code and works as a breeze! :D

orakili added a commit to UN-OCHA/json-schema that referenced this issue Jul 4, 2024
@aryojamousi
Copy link

We have tried using the fork from @orakili and the code resolves the mentioned issue. It would be also highly appreciated if the fix get merged to master so we can also avoid using forks! :)

@sunanzhi
Copy link

sunanzhi commented Sep 4, 2024

Or this document may solve your problem?

https://opis.io/json-schema/2.x/php-validator.html#max-errors

image
image

@aryojamousi
Copy link

aryojamousi commented Sep 4, 2024

We have actually set the max Errors to 100 in this case :( so I don't think that will have an impact. But I will give it a try with a lower number such as 5.

Edit: I just tried it with a lower number and the behaviour is still the unwanted one.

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

4 participants