Skip to content

Commit 6badcc2

Browse files
committed
docs and improvements
1 parent 74b942e commit 6badcc2

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

docs/05-dataclasses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ being applied to the class.
299299

300300
## Reject unknown fields
301301

302-
Per default, validataclass just ignores any unknown fields in the input dictionary when validating an object.
302+
By default, validataclass just ignores any unknown fields in the input dictionary when validating an object.
303303
This makes sense for normal APIs, as additional fields are just filtered out, and it makes validataclass more robust to
304304
changes in the API. There might be situations where one needs to have a strict validation of additional fields,
305305
for example, to match an OpenAPI validation.

src/validataclass/dataclasses/validataclass.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,27 @@ class ExampleDataclass:
8686
8787
Optional parameters to the decorator will be passed directly to the `@dataclass` decorator. In most cases no
8888
parameters are necessary. By default, the argument `kw_only=True` will be used for validataclasses.
89+
90+
Additionally, the following validataclass-specific parameter is supported:
91+
92+
`reject_unknown_fields`: If set to `True`, the `DataclassValidator` will reject any input fields that are not
93+
defined in the validataclass, raising a validation error for each unknown field.
94+
95+
Example with `reject_unknown_fields`:
96+
97+
```
98+
@validataclass(reject_unknown_fields=True)
99+
class StrictDataclass:
100+
example_field1: str = StringValidator()
101+
```
102+
103+
In this example, validating `{'example_field1': 'cookie', 'unknown': 'value'}` would raise a
104+
`DictFieldsValidationError` with an error for the `unknown` field.
89105
"""
90106

91107
def decorator(_cls: type[_T]) -> type[_T]:
92108
# Pop validataclass-specific options before passing kwargs to @dataclass
93-
reject_unknown_fields = kwargs.pop('reject_unknown_fields', False)
109+
reject_unknown_fields = kwargs.pop('reject_unknown_fields', None)
94110

95111
# Set kw_only=True as the default to allow required and optional fields in any order
96112
kwargs.setdefault('kw_only', True)

src/validataclass/validators/dataclass_validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def __init__(
143143

144144
# Use the explicit parameter if given, otherwise fall back to the dataclass setting
145145
if reject_unknown_fields is None:
146-
reject_unknown_fields = getattr(dataclass_cls, '__reject_unknown_fields__', None)
146+
reject_unknown_fields = getattr(dataclass_cls, '__reject_unknown_fields__', False)
147147
self.field_defaults = {}
148148

149149
# Collect field validators and required fields for the DictValidator by examining the dataclass fields

0 commit comments

Comments
 (0)