From 9c1c698c95dfb292f98d344c5b6964fc4d8e6ee0 Mon Sep 17 00:00:00 2001 From: Samuel Eklund Date: Wed, 3 Jun 2020 00:42:54 -0700 Subject: [PATCH 1/3] add description of subclass each_item scenario --- docs/usage/validators.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/usage/validators.md b/docs/usage/validators.md index 26b588e4ec..ecb72317ef 100644 --- a/docs/usage/validators.md +++ b/docs/usage/validators.md @@ -50,6 +50,10 @@ A few more things to note: * passing `each_item=True` will result in the validator being applied to individual values (e.g. of `List`, `Dict`, `Set`, etc.), rather than the whole object +## Subclass Validators and `each_item` + +If using a validator with a subclass that references a `List` type field on a parent class, using `each_item=True` will cause the validator not to run; instead, the list must be iterated over programatically. + ## Validate Always For performance reasons, by default validators are not called for fields when a value is not supplied. From 5ad26b9beda21ad864d71d57143116f1b87aee1d Mon Sep 17 00:00:00 2001 From: Samuel Eklund Date: Wed, 3 Jun 2020 01:06:45 -0700 Subject: [PATCH 2/3] add example of subclass validator with each_item --- .../examples/validators_subclass_each_item.py | 36 +++++++++++++++++++ docs/usage/validators.md | 8 ++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 docs/examples/validators_subclass_each_item.py diff --git a/docs/examples/validators_subclass_each_item.py b/docs/examples/validators_subclass_each_item.py new file mode 100644 index 0000000000..4b050950d9 --- /dev/null +++ b/docs/examples/validators_subclass_each_item.py @@ -0,0 +1,36 @@ +from typing import List +from pydantic import BaseModel, ValidationError, validator + + +class ParentModel(BaseModel): + names: List[str] + + +class ChildModel(ParentModel): + @validator('names', each_item=True) + def check_names_not_empty(cls, v): + assert v != '', 'Empty strings are not allowed.' + return v + + +# This will NOT raise a ValidationError because the validator was not called +try: + child = ChildModel(names=['Alice', 'Bob', 'Eve', '']) +except ValidationError as e: + print(e) +else: + print('No ValidationError caught.') + + +class ChildModel2(ParentModel): + @validator('names') + def check_names_not_empty(cls, v): + for name in v: + assert name != '', 'Empty strings are not allowed.' + return v + + +try: + child = ChildModel2(names=['Alice', 'Bob', 'Eve', '']) +except ValidationError as e: + print(e) diff --git a/docs/usage/validators.md b/docs/usage/validators.md index ecb72317ef..c135124ecb 100644 --- a/docs/usage/validators.md +++ b/docs/usage/validators.md @@ -52,7 +52,13 @@ A few more things to note: ## Subclass Validators and `each_item` -If using a validator with a subclass that references a `List` type field on a parent class, using `each_item=True` will cause the validator not to run; instead, the list must be iterated over programatically. +If using a validator with a subclass that references a `List` type field on a parent class, using `each_item=True` will +cause the validator not to run; instead, the list must be iterated over programatically. + +```py +{!.tmp_examples/validators_subclass_each_item.py!} +``` +_(This script is complete, it should run "as is")_ ## Validate Always From 37e2a1ad300f7e852ce6a36016dea2dd1586f094 Mon Sep 17 00:00:00 2001 From: Samuel Eklund Date: Wed, 3 Jun 2020 01:15:41 -0700 Subject: [PATCH 3/3] each_item causes validator to not run when parent class has List field #1566 --- changes/1566-samueldeklund.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changes/1566-samueldeklund.md diff --git a/changes/1566-samueldeklund.md b/changes/1566-samueldeklund.md new file mode 100644 index 0000000000..bd35c65d5e --- /dev/null +++ b/changes/1566-samueldeklund.md @@ -0,0 +1 @@ +Subclass validators do not run when referencing a `List` field defined in a parent class when `each_item=True`. Added an example to the docs illustrating this.