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

Field validators are called multiple times #590

Open
jps-ob opened this issue Mar 9, 2019 · 0 comments
Open

Field validators are called multiple times #590

jps-ob opened this issue Mar 9, 2019 · 0 comments

Comments

@jps-ob
Copy link

jps-ob commented Mar 9, 2019

In the below example (ignoring programming logic) field validators are called multiple times. Is this bug or not?

In [201]: def is_names_empty(value):
     ...:     print("Is Names Empty?")
     ...:     return value

In [202]: def is_duplicate(value):
     ...:     print("Is Duplicate?")
     ...:     return value

In [203]: class A(Model):
     ...:     id=StringType()
     ...:     names=ListType(StringType, validators=[is_names_empty])
     ...:     black_list=ListType(StringType, validators=[is_duplicate])
     ...:
     ...:     def validate_names(self, validated, new_data):
     ...:         print(f"New Data: {new_data}")
     ...:         print(f"Validated: {validated}")
     ...:         if any([n in validated["black_list"] for n in new_data]):
     ...:             raise ValidationError("Blacklisted name found")

In [204]: a=A();a.id=1;a.names=["kk"];a.black_list=["jj"];a.validate()
Is Names Empty?
Is Names Empty?
Is Duplicate?
Is Duplicate?
New Data: ['kk']
Validated: {'id': '1', 'names': ['kk'], 'black_list': ['jj']}

In [205]: a=A();a.id=1;a.names=["kk"];a.black_list=["jj","kk"];a.validate()
Is Names Empty?
Is Names Empty?
Is Duplicate?
Is Duplicate?
Is Duplicate?
New Data: ['kk']
Validated: {'id': '1', 'names': ['kk'], 'black_list': ['jj', 'kk']}
---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
<< Truncated trackeback>>
DataError: {"names": ["Blacklisted name found"]}

Even if model validator has to call field validator, why is_duplicate is called 3 times ?

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