You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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')
returnTrueexceptValueError:
# self._error(field, f"Date '{value}' is not in YYYY-MM-DD format")returnFalse
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')
returnTrueexceptValueError:
self._error(field, f"Date '{value}' is not in YYYY-MM-DD format")
returnFalse
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
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
The text was updated successfully, but these errors were encountered:
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
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
But adding a field argument to propagate an error is not supported
Showing the error
Expected Behaviour
Support the field argument for the type validators, for example
beside
Test Case
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
The text was updated successfully, but these errors were encountered: