openapi-core icon indicating copy to clipboard operation
openapi-core copied to clipboard

[Feature]: Iter over errors, not raise them

Open itsMGA opened this issue 10 months ago • 3 comments

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

itsMGA avatar Aug 25 '23 18:08 itsMGA

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)

p1c2u avatar Sep 15 '23 15:09 p1c2u

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

rabbl avatar Mar 07 '24 09:03 rabbl

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

runekaagaard avatar May 09 '24 15:05 runekaagaard