flask-rebar
flask-rebar copied to clipboard
Referring to fields in rebar.validated_body without hardcoded str
When getting values from rebar.validated_body, is there a way to avoid hardcoded field names? Instead of rebar.validated_body["my_field"]
, I'm wondering if there's a way to do sth like rebar.validated_body[MySchema.my_field.name]
. Thanks!
Looking quickly at the marshmallow v2 code, it doesn't seem possible to do that. It looks like v3 has a name
field though (bounded at runtime). So we might be able to support this when we finally ship support for marshmallow v3. This is also why I am a big supporter of the library marshmallow-objects and I want to add support in rebar.
We could write our own Metaclass for RequestSchema to bind a 'name' field at class creation time (runtime) to replicate what Marshmallow 3 is doing to support this, but updating to Marshmallow 3 seems like the saner approach 😄
Note: Let's test this once we are on Marshmallow 3 (#37) and close this out. Also may relate to #131
So, I'm not sure if I'm just not doing it right, but I still don't see a way to refer to a field like this in Marshmallow 3. Closest I got (with an object I instantiated for my Schema subclass) was something like this, which obviously doesn't solve the problem at hand 😂
x.fields["id"].name
That said, it's entirely possible that I'm overlooking something obvious - Anybody else have better luck with this?