@@ -6,6 +6,88 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66and this project adheres to [ Semantic Versioning] ( https://semver.org/spec/v2.0.0.html ) .
77
88
9+ ## [ 0.7.0] ( https://github.com/binary-butterfly/validataclass/releases/tag/0.7.0 ) - 2022-09-22
10+
11+ [ Full changelog] ( https://github.com/binary-butterfly/validataclass/compare/0.6.2...0.7.0 )
12+
13+ This release mainly introduces ** context-sensitive (post-)validation** . There are some more additions planned for this
14+ feature (e.g. dependencies between dataclass fields), but for now we have a solid base.
15+
16+ It also features a handful of smaller additions and changes, see below.
17+
18+ There is a potential ** breaking change** (which probably won't affect anybody), and a sort of ** deprecation** that
19+ ** will** affect most custom validators in the future, so make sure to upgrade your validators as explained below.
20+
21+ ### Added
22+
23+ - Basic support for ** context-sensitive validation** . [ #77 ]
24+ - All built-in validators now support arbitrary keyword arguments (so called ** context arguments** ) in the ` validate() `
25+ method. These can be used to change the behavior of a validator at validation time based on some kind of application
26+ context. Most validators don't do anything with them except passing them down to child validators (e.g. to the item
27+ validators in a ` ListValidator ` , etc.), but you can implement custom validators that use them.
28+ - To keep compatibility with custom validators that do not accept context arguments yet, a ** provisional** helper
29+ method ` validate_with_context() ` was added to the base ` Validator ` class. This method simply calls the ` validate() `
30+ method, but checks whether it supports context arguments. If yes, context arguments are passed to ` validate() ` ,
31+ else ` validate() ` is called without any extra arguments. This method will become obsolete and eventually removed in
32+ the future, and should only be used in cases where it is unsure whether a validator supports context arguments.
33+ - Context-sensitive ** post-validation in dataclasses** . [ #77 ]
34+ - The ` DataclassValidator ` will now call the method ` __post_validate__() ` on your dataclass instances if you have
35+ defined this method. Additionally, if the method accepts arbitrary keyword arguments (i.e. ` **kwargs ` , although the
36+ name of this parameter doesn't matter), all context arguments will be passed to it.
37+ - You can use this to implement context-sensitive post-validation. For example, if you implement a ` PATCH ` endpoint in
38+ a REST API, you could have fields that are sometimes optional and sometimes required, depending on a property of the
39+ object the user wants to update. You can now fetch the object before calling ` validate() ` , then pass the object (or
40+ just the property) as a context argument to ` validate() ` , and then access it in ` __post_validate__() ` to implement a
41+ context-sensitive field requirement check.
42+ - The ` __post_init__() ` method of regular dataclasses can still be used for post-validation as before. It cannot be
43+ used context-sensitively, though, because we cannot pass arbitrary keyword arguments to it.
44+ - ` DateTimeValidator ` : Add parameter ` discard_milliseconds ` to discard the milli- and microseconds of datetimes. [ #79 ]
45+ - ` AnyOfValidator ` and ` EnumValidator ` : Add parameter ` case_insensitive ` for case-insensitive string matching. [ #81 ]
46+ - New helper function ` unset_to_none() ` (returns ` None ` if value is ` UnsetValue ` ). [ #76 ]
47+
48+ ### Changed
49+
50+ - All built-in validators now support context arguments in the ` validate() ` method. See above. [ #77 ]
51+ - The ` validate() ` method of the ` DataclassValidator ` was restructured a bit for easier extendability. (This may be
52+ subject of additional changes in the future, though.) [ #77 ]
53+ - The ` @validataclass ` decorator is now marked as a "data class transform" using the ` @dataclass_transform ` decorator
54+ as specified in [ PEP 681] ( https://peps.python.org/pep-0681/ ) . [ #78 ]
55+ - Since this decorator was only introduced in Python 3.11, we use the [ typing-extensions] ( https://pypi.org/project/typing-extensions/ )
56+ library which backports new typing features like this.
57+ - ` ListValidator ` and ` EnumValidator ` are now defined as generic classes for better type hinting. [ #80 ]
58+ - ` AnyOfValidator ` and ` EnumValidator ` now accept ` allowed_values ` and ` allowed_types ` as any iterable. [ #80 ]
59+ - ` AnyOfValidator ` and ` EnumValidator ` : The ` ValueNotAllowedError ` now lists all allowed values (unless they are more than 20). [ #81 ]
60+
61+ ### Deprecated
62+
63+ - Validator classes that do not accept context arguments are now deprecated. [ #77 ]
64+ - When defining a ` Validator ` subclass, the ` __init_subclass__() ` method will check whether your ` validate() ` method
65+ accepts arbitrary keyword arguments. If not, a ` DeprecationWarning ` will be issued.
66+ - Existing custom validators will keep working for now, but this compatibility will be removed in version 1.0.
67+ - To update your custom validators, simply add ` **kwargs ` to the parameter list of ` validate() ` . If your validator
68+ class is based on an existing validator, make sure to pass the context arguments down to the ` validate() ` of your
69+ base class as well, i.e. ` super().validate(input_data, **kwargs) ` .
70+
71+ ### Removed
72+
73+ - ** Breaking change:** The ` post_validate() ` method of the ` DataclassValidator ` was ** removed** . [ #77 ]
74+ - (This should not to be confused with the new ` __post_validate__() ` , however, which is a method of dataclasses, not
75+ of the validator itself.)
76+ - This method was removed because a) post-validation using either ` __post_init__() ` or the new ` __post_validate__() `
77+ method in the dataclass should be preferred, b) the validator was restructured in a way that makes this method
78+ redundant, and c) it was probably never used anyway.
79+ - If you do need post-validation as part of a subclassed ` DataclassValidator ` , you can extend either ` validate() ` or
80+ the new ` _post_validate() ` private method (but make sure to extend, not override it, since it also handles the call
81+ of the dataclass's ` __post_validate__() ` method).
82+
83+ [ #76 ] : https://github.com/binary-butterfly/validataclass/pull/76
84+ [ #77 ] : https://github.com/binary-butterfly/validataclass/pull/77
85+ [ #78 ] : https://github.com/binary-butterfly/validataclass/pull/78
86+ [ #79 ] : https://github.com/binary-butterfly/validataclass/pull/79
87+ [ #80 ] : https://github.com/binary-butterfly/validataclass/pull/80
88+ [ #81 ] : https://github.com/binary-butterfly/validataclass/pull/81
89+
90+
991## [ 0.6.2] ( https://github.com/binary-butterfly/validataclass/releases/tag/0.6.2 ) - 2022-07-11
1092
1193[ Full changelog] ( https://github.com/binary-butterfly/validataclass/compare/0.6.1...0.6.2 )
0 commit comments