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

[Feature]: Iter over errors, not raise them #656

Open
itsMGA opened this issue Aug 25, 2023 · 3 comments
Open

[Feature]: Iter over errors, not raise them #656

itsMGA opened this issue Aug 25, 2023 · 3 comments

Comments

@itsMGA
Copy link

itsMGA commented Aug 25, 2023

Suggested Behavior

In the depreciated version there was:

from openapi_core import openapi_request_validator
#allows u to iter through all the errors found, in this way, you could use the error information for reporting and such
openapi_request_validator.iter_errors 

Existing fuctionality:
validate_request(request=request, spec=spec)
Only raises errors, no way of iterating through them

Why is this needed?

Errors do not need to be raised necessarily, there are cases when you would want to iterate and do actions for certain errors, gather information and so on

References

No response

Would you like to implement a feature?

No

@p1c2u
Copy link
Collaborator

p1c2u commented Sep 15, 2023

hi @itsMGA

validate_request is just a shortcut for validation. Iteration over errors withiter_errors is still available (although not documented) under validator class

from openapi_cote import V30RequestValidator

V30RequestValidator(spec).iter_errors(request)

@rabbl
Copy link

rabbl commented Mar 7, 2024

Dear @p1c2u, thank you for this lib and responding to the question from @itsMGA.
In our project we are iterating over the errors and want to return a list of validation errors with the response:

    ...
    spec = Spec.from_file_path(settings.OPENAPI_LOCAL_SPEC_FILE)
    openapi_request = FlaskOpenAPIRequest(flask.request)
    errors = [str(error) for error in list(V31RequestValidator(spec).iter_errors(openapi_request))]
    if len(errors) == 0:
        return f(*args, **kwargs)

    raise SchemaValidationException('Schema Validation Error:', errors)
    ...

Unfortunately the response has only one general entry:

{
    "error": "Request body validation error"
}

Is it possible to get the list of specific errors, to give more details to the API-consumer?

Best regards!
Ralf

@runekaagaard
Copy link

It also seems like it only shows the first error!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants