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 · 3 comments
Open

Validation stops on first error #107

dontub opened this issue Jun 13, 2022 · 3 comments

Comments

@dontub
Copy link

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
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
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
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

2 participants