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

revertrisk: add pydantic validation #5

Merged
merged 3 commits into from
Jun 4, 2024
Merged

Conversation

isaranto
Copy link
Collaborator

@isaranto isaranto commented May 27, 2024

  • add validation in the parent model
  • add validation in revertrisk
  • define the LiftWing class request function (the parent function) and let
    child classes just call super() with the appropriate validation class in the decorator

An example of making a request where the parameter lang is missing:

Traceback (most recent call last):
  File "/Users/isaranto/repoz/liftwing-python/test.py", line 8, in <module>
    print(client.request(payload={"rev_id": 123456}))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/isaranto/repoz/liftwing-python/liftwing/utils/validator.py", line 13, in wrapper
    _ = model(**payload)
        ^^^^^^^^^^^^^^^^
  File "/Users/isaranto/repoz/liftwing-python/.venv/lib/python3.11/site-packages/pydantic/main.py", line 176, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for RevertRiskPayload
  Value error, 'lang' parameter is required in the payload. [type=value_error, input_value={'rev_id': 123456}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.7/v/value_error

Bug: T366015

* add a decorator that will be used for the payload argument validation
* add validation in revertrisk
* define the LiftWing class request function (the parent function) and let
child classes just call super() with the appropriate validation class in the decorator

Bug: T366015
Copy link

@AikoChou AikoChou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Ilias, thank you for working on this. I don't have much experience on pydantic, so not sure about the best practices here. But I checked their documentation and have some suggestions. :)

liftwing/models/revertrisk.py Outdated Show resolved Hide resolved
liftwing/models/revertrisk.py Outdated Show resolved Hide resolved
@isaranto isaranto requested a review from AikoChou June 3, 2024 14:51
AikoChou
AikoChou previously approved these changes Jun 3, 2024
Copy link

@AikoChou AikoChou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Copy link

@AikoChou AikoChou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this solution 😃

@isaranto isaranto merged commit 3961204 into main Jun 4, 2024
@isaranto isaranto deleted the revertrisk-validation branch June 4, 2024 07:26
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

Successfully merging this pull request may close these issues.

2 participants