python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
302 stars 132 forks source link

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

Open itsMGA opened 1 year ago

itsMGA commented 1 year ago

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 commented 1 year ago

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 commented 8 months ago

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 commented 6 months ago

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