Allow passing a Field instance to unknown option#2911
Open
Br1an67 wants to merge 1 commit intomarshmallow-code:devfrom
Open
Allow passing a Field instance to unknown option#2911Br1an67 wants to merge 1 commit intomarshmallow-code:devfrom
unknown option#2911Br1an67 wants to merge 1 commit intomarshmallow-code:devfrom
Conversation
When `unknown` is set to a Field instance (e.g. `fields.Int()`), unknown field values are deserialized through that field instead of being included as-is. This enables validation and type coercion of unknown fields. The Field can be passed via Schema.__init__, class Meta, the `load()` keyword argument, or Nested field's `unknown` parameter. Closes marshmallow-code#853
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Allow a
Fieldinstance to be passed as theunknownoption (in addition to the existingRAISE,INCLUDE,EXCLUDEconstants). When aFieldis provided, unknown field values are deserialized through that field, enabling validation and type coercion.Closes #853
Example
The
Fieldcan be set via:Schema(unknown=fields.Int())class Meta: unknown = fields.Int()schema.load(data, unknown=fields.Int())fields.Nested(ChildSchema, unknown=fields.Int())Changes
src/marshmallow/types.py: UpdatedUnknownOptiontype alias to includeField(underTYPE_CHECKINGto avoid circular imports)src/marshmallow/schema.py: Addedisinstance(unknown, Field)branch in_deserializethat routes unknown values through the field'sdeserialize()method, with proper error collectiontests/test_schema.py: Added 7 tests covering: basic deserialization, validation errors, Meta config, many=True, load() kwarg, nested schemas, and passthrough withField()