-
Notifications
You must be signed in to change notification settings - Fork 59
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
Pydantic v2's error object contains a ctx field #86
Comments
@kakakikikeke-fork Hi there, thank you for the issue and also the PR. I did some investigation today and yesterday, it is indeed a valid issue and would occur on not only things like body_params but also things like query params since they are based on the same fundation. The PR #84 is currently failing the unit test on CI, I belive its caused by calling err.get("body_params") on err that does not have "body_params", so request that only has query_params if that makes sense. |
@yctomwang I tried supporting parameters other than body_params. I have locally run pytest I have also verified that locally all pytest tests are successful. Thanks. |
As a workaround for your custom validations you can use
|
Instead of removing ctx (or url, or input), it would be better to introduce some arguments to 'validate' decorator, similar to include_context|include_url|include_input and pass it down to ve.errors |
@nickzorin yep i have to agree with this, removing the ctx would not be the best idea. I am planning to spend some time eventually to deal with this issue, current we are storing the acutal objects hence the reason for the error apprearing. This will require a bit of rework to get all exist test cases to adapt. Also currently the CI is failing because of the pytest black plugin. For some reason I cannot merge to master to address the CI issue. |
Any updates on this? I'm planning to introduce flask-pydantic to my org but feel that with all this extra context we are exposing far more info than is desirable. |
Guys, any updates so far?( this is really really frustrating! This issue literally means that you cannot use all the pydantic class RequestBodyModel(BaseModel):
name: str
phone_number: Optional[str] = None
email: Optional[EmailStr] = None
@model_validator(mode='before')
@classmethod
def check_email_or_phone(cls, data: Any) -> Any:
email = data.get('email')
phone_number = data.get('phone_number')
assert bool(email) ^ bool(phone_number), 'Email or phone number must be provided, but not both'
return data And obviously because of
>>> repr(err['body_params'][0]['ctx']['error'])
"AssertionError('Email or phone number must be provided, but not both')" |
We literally got the ci fixed this week and can finally merge code into the master. After some careful thinking, i think the remove appraoch in #84 might not be the best. We are planning to get this issue addressed as soon as possible. any ideas are welcome |
@yctomwang I've made a PR #96 for addressing this issue. It adds arguments to |
If you intentionally raise ValueError, a field called ctx seems to be added. An example of pydantic v2 error object.
An error object is included in ctx and the error object cannot be serialized to dict, resulting in an error. The traceback is this.
I solved it by deleting ctx, is there anything else I can do? #84
Thanks.
The text was updated successfully, but these errors were encountered: