diff --git a/zk/Pydantic.md b/zk/Pydantic.md index 432917c..29d934c 100644 --- a/zk/Pydantic.md +++ b/zk/Pydantic.md @@ -52,6 +52,21 @@ class VariantProperties(CommonProperties): > Note that the `BaseModel` must "drill-down"; it must be inherited at each > stage. +## Transform incoming values (before validation) + +In the following example I convert an incoming `str` value to a list of strings: + +```py +class MySchema(BaseModel) + sub_genres: Optional[List[str]] = None) + @field_validator("sub_genres", mode="before") + @classmethod + def string_to_list(cls, v): + if isinstance(v, str): + return [v] + return v +``` + ## Aliasing Sometimes the incoming data that you wish to validate will have field names diff --git a/zk/Pytest_fixtures.md b/zk/Pytest_fixtures.md index c9cb8ff..00d5a76 100644 --- a/zk/Pytest_fixtures.md +++ b/zk/Pytest_fixtures.md @@ -86,3 +86,7 @@ Then to specify the units that will run as the `yield` we just inject `setup`: def some_function(setup_function): pass ``` + +## Example: setting up mocks before every test + +We can combine the