Is it possible to return a list of validation errors for a specific field, without having separate validators for the same field? It would be nice to get all errors back in 1 shot for the field, instead of having to get separate responses back for each failed validation.
@validator('password')
def check_password(cls, value):
password = value.get_secret_value()
failed = []
min_length = 8
if len(password) < min_length:
failed.append('Password must be at least 8 characters long.')
return value
if not any(character.islower() for character in password):
failed.append('Password missing lower case')
return value
if len(failed) > 0:
raise ValueError(failed)
Output:
{
"detail": [
{
"loc": [
"body",
"password"
],
"msg": "['Password must be at least 8 characters long.', 'Password missing lower case']",
"type": "assertion_error"
}
]
}
However, "msg" comes as a string list. Can we get that as a list?