flask-rebar icon indicating copy to clipboard operation
flask-rebar copied to clipboard

Referring to fields in rebar.validated_body without hardcoded str

Open andrewzu opened this issue 5 years ago • 4 comments

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!

andrewzu avatar Feb 07 '20 15:02 andrewzu

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.

Sytten avatar Feb 07 '20 18:02 Sytten

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 😄

airstandley avatar Mar 06 '20 23:03 airstandley

Note: Let's test this once we are on Marshmallow 3 (#37) and close this out. Also may relate to #131

airstandley avatar Jul 01 '20 20:07 airstandley

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?

RookieRick avatar Oct 23 '20 22:10 RookieRick