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

How to access the Ninja Schema from which the ninja.errors.ValidationError raised. #64

Open
wf-yamaday opened this issue Apr 30, 2023 · 4 comments

Comments

@wf-yamaday
Copy link

wf-yamaday commented Apr 30, 2023

Thanks for this great project!

We are using Django Ninja Extra to develop our application.

In our application, we are trying to return 400 (Bad Request) on input schema input value validation failure.
The input schema is defined using the Ninja schema (https://eadwincode.github.io/django-ninja-extra/tutorial/schema/ ).

And, define a custom error handler that overrides ValidationError.

from ninja.errors import ValidationError

@api.exception_handler(ValidationError)
def validation_errors(request, exc):
    return HttpResponse("Invalid input", status=400)

This feature is useful, but I have one problem.
It is that I cannot access the Ninja Schema from which the ValidationError raised.
In our use case, we want to customize error messages using Schema information.

But,ninja.errors.ValidationError cannot refer to Schema object.

Is there a way to handle these cases?

@eadwinCode
Copy link
Owner

@wf-yamaday I will look at this and get back to you. Sorry for not attending to this earlier.

@eadwinCode
Copy link
Owner

@wf-yamaday I have looked at this and seen that the problem is that ValidationError is initiated by pydantic library. And I have checked if there is a way to config exception type for a pydantic schema but that doesnt exist.

@wf-yamaday
Copy link
Author

@eadwinCode Thank you for looking into the issue.

I see, I understand that it would be difficult to refer to Schema object.

I would like to know if there is a simple way to customize ValidationError error messages…?

@eadwinCode
Copy link
Owner

@wf-yamaday Did you eventually have this issue resolved?

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