Skip to content

Forbid unknown fields - #210

Merged
jcrist merged 3 commits into
mainfrom
forbid-unknown-fields
Nov 25, 2022
Merged

jcrist merged 3 commits into
mainfrom
forbid-unknown-fields

Conversation

@jcrist

@jcrist jcrist commented Nov 25, 2022

Copy link
Copy Markdown
Member

If enabled, any unknown fields encountered when decoding a Struct type will result in an error, rather than just being silently ignored. This can be useful to catch typos in messages (useful for e.g. config files).

Most users should keep the default of False, as this results in more flexible systems and better supports schema evolution.

Example:

>>> class Example(msgspec.Struct, forbid_unknown_fields=True):
...     field_one: int
...     field_two: bool = False

>>> msgspec.json.decode(
...     b'{"field_one": 1, "field_twoo": true}',  # oops, a typo
...     type=Example
... )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.ValidationError: Object contains unknown field `field_twoo`

Fixes #208.

If enabled, any unknown fields encountered when decoding a Struct type
will result in an error, rather than just being silently ignored. This
can be useful to catch typos in messages (useful for e.g. config files).
Most users should keep the default of `False`, as this results in more
flexible systems and better supports schema evolution.
@jcrist
jcrist merged commit 67b5a81 into main Nov 25, 2022
@jcrist
jcrist deleted the forbid-unknown-fields branch November 25, 2022 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error on decoding unknown fields

1 participant