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

Custom data type validation does not support custom error generation #619

Open
weshouman opened this issue Apr 18, 2024 · 0 comments
Open

Comments

@weshouman
Copy link

weshouman commented Apr 18, 2024

Brief

Generating a custom error requires the field to be known, however defining a custom datatype only captures the value, and not the field.

Current Behaviour

The following works

    def _validate_type_ymd_date(self, value):
        """ Define custom validator for date format YYYY-MM-DD """
        try:
            datetime.strptime(value, '%Y-%m-%d')
            return True
        except ValueError:
            # self._error(field, f"Date '{value}' is not in YYYY-MM-DD format")
            return False

But adding a field argument to propagate an error is not supported

    def _validate_type_ymd_date(self, field, value):
        """ Define custom validator for date format YYYY-MM-DD """
        try:
            datetime.strptime(value, '%Y-%m-%d')
            return True
        except ValueError:
            self._error(field, f"Date '{value}' is not in YYYY-MM-DD format")
            return False

Showing the error

Traceback (most recent call last):
  File "/path/to/project/tests/test_validator.py", line 29, in test_ymd_date_valid
    self.assertTrue(self.validator.validate(document))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/cerberus/validator.py", line 1040, in validate
    self.__validate_definitions(definitions, field)
  File "/path/to/project/venv/lib/python3.11/site-packages/cerberus/validator.py", line 1110, in __validate_definitions
    result = validate_rule(rule)
             ^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/cerberus/validator.py", line 1085, in validate_rule
    return validator(definitions.get(rule, None), field, value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/path/to/project/venv/lib/python3.11/site-packages/cerberus/validator.py", line 1548, in _validate_type
    matched = type_handler(value)
              ^^^^^^^^^^^^^^^^^^^
TypeError: MyValidator._validate_type_ymd_date() missing 1 required positional argument: 'value'

Expected Behaviour

Support the field argument for the type validators, for example

    def _validate_type_ymd_date(self, field, value):

beside

    def _validate_type_ymd_date(self, value):

Test Case

class TestYMDDate(unittest.TestCase):
    def setUp(self):
        self.validator = MyValidator({'start_date': {'type': 'ymd_date'}})

    def test_ymd_date_valid(self):
        document = {'start_date': '2023-01-01'}
        self.assertTrue(self.validator.validate(document))

Passes for 2 arg based validator but not for 3 arg based

Workaround

Stick to checking the default error generated when a type is not matched, in this case it would be must be of ymd_date type

@weshouman weshouman changed the title Custom data type validation does not support error generation Custom data type validation does not support custom error generation Apr 18, 2024
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

1 participant