@@ -128,6 +128,122 @@ When you need more flexibility in defining input schemas, you can pass a marshma
128128 # ...
129129
130130
131+ Setting `unknown `
132+ -----------------
133+
134+ webargs supports several ways of setting and passing the `unknown ` parameter
135+ for `handling unknown fields <https://marshmallow.readthedocs.io/en/stable/quickstart.html#handling-unknown-fields >`_.
136+
137+ You can pass `unknown=... ` as a parameter to any of
138+ `Parser.parse <webargs.core.Parser.parse> `,
139+ `Parser.use_args <webargs.core.Parser.use_args> `, and
140+ `Parser.use_kwargs <webargs.core.Parser.use_kwargs> `.
141+
142+
143+ .. note ::
144+
145+ The `unknown ` value is passed to the schema's `load() ` call. It therefore
146+ only applies to the top layer when nesting is used. To control `unknown ` at
147+ multiple layers of a nested schema, you must use other mechanisms, like
148+ the `unknown ` argument to `fields.Nested `.
149+
150+ Default `unknown `
151+ +++++++++++++++++
152+
153+ By default, webargs will pass `unknown=marshmallow.EXCLUDE ` except when the
154+ location is `json `, `form `, `json_or_form `, `path `, or `path `. In those cases,
155+ it uses `unknown=marshmallow.RAISE ` instead.
156+
157+ You can change these defaults by overriding `DEFAULT_UNKNOWN_BY_LOCATION `.
158+ This is a mapping of locations to values to pass.
159+
160+ For example,
161+
162+ .. code-block :: python
163+
164+ from flask import Flask
165+ from marshmallow import EXCLUDE , fields
166+ from webargs.flaskparser import FlaskParser
167+
168+ app = Flask(__name__ )
169+
170+
171+ class Parser (FlaskParser ):
172+ DEFAULT_UNKNOWN_BY_LOCATION = {" query" : EXCLUDE }
173+
174+
175+ parser = Parser()
176+
177+
178+ # location is "query", which is listed in DEFAULT_UNKNOWN_BY_LOCATION,
179+ # so EXCLUDE will be used
180+ @app.route (" /" , methods = [" GET" ])
181+ @parser.use_args ({" foo" : fields.Int()}, location = " query" )
182+ def get (self , args ):
183+ return f " foo x 2 = { args[' foo' ] * 2 } "
184+
185+
186+ # location is "json", which is not in DEFAULT_UNKNOWN_BY_LOCATION,
187+ # so no value will be passed for `unknown`
188+ @app.route (" /" , methods = [" POST" ])
189+ @parser.use_args ({" foo" : fields.Int(), " bar" : fields.Int()}, location = " json" )
190+ def post (self , args ):
191+ return f " foo x bar = { args[' foo' ] * args[' bar' ]} "
192+
193+
194+ You can also define a default at parser instantiation, which will take
195+ precedence over these defaults, as in
196+
197+ .. code-block :: python
198+
199+ from marshmallow import INCLUDE
200+
201+ parser = Parser(unknown = INCLUDE )
202+
203+ # because `unknown` is set on the parser, `DEFAULT_UNKNOWN_BY_LOCATION` has
204+ # effect and `INCLUDE` will always be used
205+ @app.route (" /" , methods = [" POST" ])
206+ @parser.use_args ({" foo" : fields.Int(), " bar" : fields.Int()}, location = " json" )
207+ def post (self , args ):
208+ unexpected_args = [k for k in args.keys() if k not in (" foo" , " bar" )]
209+ return f " foo x bar = { args[' foo' ] * args[' bar' ]} ; unexpected args= { unexpected_args} "
210+
211+ Using Schema-Specfied `unknown `
212+ +++++++++++++++++++++++++++++++
213+
214+ If you wish to use the value of `unknown ` specified by a schema, simply pass
215+ ``unknown=None ``. This will disable webargs' automatic passing of values for
216+ ``unknown ``. For example,
217+
218+ .. code-block :: python
219+
220+ from flask import Flask
221+ from marshmallow import Schema, fields, EXCLUDE , missing
222+ from webargs.flaskparser import use_args
223+
224+
225+ class RectangleSchema (Schema ):
226+ length = fields.Float()
227+ width = fields.Float()
228+
229+ class Meta :
230+ unknown = EXCLUDE
231+
232+
233+ app = Flask(__name__ )
234+
235+ # because unknown=None was passed, no value is passed during schema loading
236+ # as a result, the schema's behavior (EXCLUDE) is used
237+ @app.route (" /" , methods = [" POST" ])
238+ @use_args (RectangleSchema(), location = " json" , unknown = None )
239+ def get (self , args ):
240+ return f " area = { args[' length' ] * args[' width' ]} "
241+
242+
243+ You can also set ``unknown=None `` when instantiating a parser to make this
244+ behavior the default for a parser.
245+
246+
131247When to avoid `use_kwargs `
132248--------------------------
133249
0 commit comments