Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 77ff5e1

Browse files
jodallovelydinosaur
authored andcommitted
Document strict validation (#81)
1 parent 94b0fe1 commit 77ff5e1

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

docs/schemas.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,34 @@ Note that data validation is not applied when instantiating a schema instance
147147
directly from an instance or dictionary. This should be used when creating
148148
instances against a data source that is already known to be validated, such as
149149
when loading existing instances from a database.
150+
151+
## Using strict validation
152+
153+
By default, additional properties in the incoming user data is ignored.
154+
155+
```python
156+
data = {
157+
'title': 'Double Negative',
158+
'release_date': '2018-09-14',
159+
'artist': {'name': 'Low'},
160+
'num_tracks': 11
161+
}
162+
```
163+
164+
After validating against the schema, the `num_tracks` property is not present
165+
on the `album` instance.
166+
167+
```python
168+
album = Album.validate(data)
169+
album.num_tracks
170+
# AttributeError: 'Album' object has no attribute 'num_tracks'
171+
```
172+
173+
If you use strict validation, additional properties becomes an error instead.
174+
175+
```python
176+
album, error = Album.validate_or_error(data, strict=True)
177+
178+
print(dict(error))
179+
# {'num_tracks': 'Invalid property name.'}
180+
```

0 commit comments

Comments
 (0)