-
-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should parse
allow arguments to be passed through to schema.load
and/or dict2schema
?
#480
Comments
1/ I agree This code from the docs would have to be reworked by creating a from webargs import fields
from webargs.flaskparser import parser
argmap = {"age": fields.Int(), "years_employed": fields.Int()}
# ...
result = parser.parse(
argmap, validate=lambda args: args["years_employed"] < args["age"]
) It's only a pity for people using dicts rather than Schemas, with a validation involving several fields, and simple enough to be expressed in a lambda. If the validation must be expressed in a function, the code is not that compact already, and creating a Schema doesn't make it much worse IMHO. 2/ Maybe. There's a trade-off between simplicity here in webargs code and API, and concision in client code. I'm defining many Schemas in my app code and it is not really an issue. It is very declarative and simple. In practice, I don't have much redundancy, thanks to inheritance. YMMV. Wondering how this would fit with auto-documentation à la apispec. |
I'm not sure, but it sounds like you might be suggesting simply dropping We could change the docs around to use more explicit schema classes in the examples, and shift to preferring schemas over dicts throughout the documentation. Let's maybe just drop (2) from discussion -- it's only really relevant for the "dict schema" usage, and I think it's fine to just say that that usage is less flexible and "you should use real schemas if you want more features".
I haven't used apispec, but I think this is another reason not to bend over backwards to handle "dict schemas" better. Users can always (on marshmallow3) just use |
Sorry to jump into this discussion and especially into the topic (2), but I think that the single additional parameter On the other hand I see the argument "if you want to do something non-trivial, just use a full schema definition, it's not bad / not an issue..." again and again, and I see the point that class SubscribeSchema(ma.Schema):
name = fields.String(required=True)
email = fields.Email(required=True) is neither much more work to write nor less intuitive or understandable than subscribe_params = {
name = fields.String(required=True)
email = fields.Email(required=True)
} so if we more and more reduce the number of cases when the second variant works, why not depreciate it and move towards a consistent usage of the first variant? |
I ran into another case where passing
I think this is one of our options. It's either that, or enhance dict-schema support to bring it closer to parity with "real" schemas. If we don't at least let users specify For example, @use_args({"myheader": fields.String(data_key="X-My-Header")}, location="header") will fail, but I'd rather the rewrite be @use_args({"myheader": fields.String(data_key="X-My-Header")}, location="header", unknown=EXCLUDE) than something where the user has to stop using the dict -- why let them use the dict in the first place, if that's going to be the case? I'm going to try to carve out some time for webargs work soon. I can at least look at how well it would go to add |
Maybe it is better to set unknown=EXCLUDE as the default behaviour. As in some cases, args are not handled by web_args, rather in other libraries. Such as page query args including cur_page, per_page etc. Explicately set those parameters in every query schemas wouldn't be an elegant solution. As a results, current implication would cause many broken changes from version 5 to version 6. |
I opened #507 to discuss |
We have #507 which tracks the |
I'm looking at the features of marshmallow and
Parser.parse
(and, by extension,use_args
too) and thinking about whether or not this will expose more marshmallow features while not adding too much code (or even, my real goal, simplifying things?).I have two basic ideas I'm toying with:
validate
argument toparse
is basically the same as a schema withvalidates_schema
methodsvalidate
errors have slightly differentmessages
-- they don't show the schema name (is that good for generated schemas? can avalidates_schema
method do that?)validate=...
, and remove in 7.0 or later?parse
to passunknown
toschema.load
-- it would be nice to be able to sayparse(..., unknown=ma.EXCLUDE)
schema.load
arguments, likeparse(..., load_params=dict(unknown=ma.INCLUDE, partial=True))
? It's a less nice interface to use but more flexibleMeta
?This is really low priority. You can do everything today in 6 by using full schema definitions. Supporting
unknown=...
orload_params=...
is really just a convenience feature.The text was updated successfully, but these errors were encountered: