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
Add a special validator where you can specify multiple allowed types and validators associated with them. The validator would first check the type, then call the specified validator, or raise an InvalidTypeError if no validator was specified for this type.
For example, you could build a validator that accepts numbers in different formats (integer 42, float 1.234, decimal string "1.234") like this:
validator=MultiTypeValidator({
int: IntegerValidator(),
float: FloatValidator(),
str: DecimalValidator(),
})
validator.validate(42) # returns 42 (integer)validator.validate(1.234) # returns 1.234 (float)validator.validate("1.234") # returns Decimal("1.234")# Error cases:validator.validate(False) # raises InvalidTypeError since no validator for bools was definedvalidator.validate("banana") # decides to use the DecimalValidator, which raises an InvalidDecimalError
Additionally, allow multiple validators to be specified for the same type (e.g. as a list). In that case, if the first validator raises a validation error, the next one is tried. If no validator successfully validates the input, the validation error of the last validator is used.
Add a special validator where you can specify multiple allowed types and validators associated with them. The validator would first check the type, then call the specified validator, or raise an InvalidTypeError if no validator was specified for this type.
For example, you could build a validator that accepts numbers in different formats (integer
42
, float1.234
, decimal string"1.234"
) like this:Additionally, allow multiple validators to be specified for the same type (e.g. as a list). In that case, if the first validator raises a validation error, the next one is tried. If no validator successfully validates the input, the validation error of the last validator is used.
For example:
The text was updated successfully, but these errors were encountered: