Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

provide a way to configure Validation Exceptions to be contextual #175

Open
PKgotso opened this issue Oct 5, 2022 · 5 comments
Open

provide a way to configure Validation Exceptions to be contextual #175

PKgotso opened this issue Oct 5, 2022 · 5 comments

Comments

@PKgotso
Copy link

PKgotso commented Oct 5, 2022

I am looking for a way to add context to RequestValidationError. Instead of it returning the error from being unable to cast to a type, I want to be able to give it the message and append the value.

Eg
Failed to cast hello to type integer

Allowing
The number hello is not a valid number

I think this will make it extremely reusable. If this is available already, I couldn't find it in the docs.

@zupo
Copy link
Collaborator

zupo commented Oct 6, 2022

Have you looked at https://github.com/teamniteo/pyramid-realworld-example-app yet? There is some basic error overriding there, might serve as an inspiration for what you are trying to achieve: https://github.com/teamniteo/pyramid-realworld-example-app/blob/master/src/conduit/openapi.py#L71

@PKgotso
Copy link
Author

PKgotso commented Oct 6, 2022

@zupo Thank you for those, however this is not what I mean.

Lets use this: https://github.com/teamniteo/pyramid-realworld-example-app/blob/master/src/conduit/comment/views.py

@view_config(route_name="comments", renderer="json", request_method="GET", openapi=True)
def comments(request: Request) -> MultipleCommentsResponse:
    """Get recent comments on the given article."""
    article = object_or_404(
        Article.by_slug(
            request.openapi_validated.parameters["path"]["slug"], db=request.db
        )
    )
    return {"comments": article.comments}

On the above, if the slug was a requirement and meant to be an integer and the user supplied a string, the RequestValidationError would be Failed to cast slug to type integer.

What I want to be able to do is to change this error message to something meaningful e.g `Invalid slug string provided.

I am aware you can override the the response using this config:
@exception_view_config(RequestValidationError)
however, this does not allow individual views to define their own custom error message.

@zupo
Copy link
Collaborator

zupo commented Oct 6, 2022

Ah, I see. Hmm ...

What about custom formatters?

class CustomFormattersTests(unittest.TestCase):

@PKgotso
Copy link
Author

PKgotso commented Oct 6, 2022

These are actually cool, I can try that, but I still think the feature might be a neat feature especially on large projects.

@zupo
Copy link
Collaborator

zupo commented Oct 6, 2022

Yep, I agree!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants