Skip to content

Commit 7a4019c

Browse files
committed
docs and improvements
1 parent 2fda270 commit 7a4019c

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
@@ -85,11 +85,27 @@ class ExampleDataclass:
8585
8686
Optional parameters to the decorator will be passed directly to the `@dataclass` decorator. In most cases no
8787
parameters are necessary. By default, the argument `kw_only=True` will be used for validataclasses.
88+
89+
Additionally, the following validataclass-specific parameter is supported:
90+
91+
`reject_unknown_fields`: If set to `True`, the `DataclassValidator` will reject any input fields that are not
92+
defined in the validataclass, raising a validation error for each unknown field.
93+
94+
Example with `reject_unknown_fields`:
95+
96+
```
97+
@validataclass(reject_unknown_fields=True)
98+
class StrictDataclass:
99+
example_field1: str = StringValidator()
100+
```
101+
102+
In this example, validating `{'example_field1': 'cookie', 'unknown': 'value'}` would raise a
103+
`DictFieldsValidationError` with an error for the `unknown` field.
88104
"""
89105

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

94110
# Set kw_only=True as the default to allow required and optional fields in any order
95111
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
@@ -138,7 +138,7 @@ def __init__(
138138

139139
# Use the explicit parameter if given, otherwise fall back to the dataclass setting
140140
if reject_unknown_fields is None:
141-
reject_unknown_fields = getattr(dataclass_cls, '__reject_unknown_fields__', None)
141+
reject_unknown_fields = getattr(dataclass_cls, '__reject_unknown_fields__', False)
142142
self.field_defaults = {}
143143

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

0 commit comments

Comments
 (0)