question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define a schema with dynamic field names

See original GitHub issue

Hi all,

I have the need to define a schema that basically represents a dict object where I know that the keys are going to be strings and the values are all going to have the same object format.

I know that having this defined as a field of a schema can be done by using the fields.Dict just like

class ValueSchema(Schema):
    att_1 = fields.String()
    att_2 = fields.String()

class RootSchema(Schema):
    root = fields.Dict(keys=fields.String(), values=fields.Nested(ValueSchema))

# dict conforming to the schema
 {
    "root": {
        "id-1":{"att_1":"a", "att_2":"b"},
        "id-2":{"att_1":"c", "att_2":"d"}
    }
}

But what I want is to have a schema that represents just the root object, i.e. without the root field, just like

{
    "id-1": {"att_1":"a", "att_2":"b"},
    "id-2": {"att_1":"c", "att_2":"d"},
    (...)
    "id-N": {"att_1":"x", "att_2":"y"},
}

Is this something that can be done with marshmallow?

Thanks in advance

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Mar 15, 2022

You can use Schema.from_dict() after you receive the data, only use the generated schema for that data, then generate a new schema when you get new data.

0reactions
deckar01commented, May 16, 2022

The only remaining issue seems to be preferred syntax. I would make a new decorator for from_dict rather than trying to overload a schema class so that you can pass it into a decorator expecting a schema class. Feedback on how you ended up working around it would be appreciated.

I am closing this for now. If you have a proposal that wasn’t already discussed in #972 I am happy to reopen. I still promote unifying the load/dump schema interface with the field interface since it improves many other use cases, but the scope of that would probably justify a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Fields | Apache Solr Reference Guide 6.6
Dynamic fields allow Solr to index fields that you did not explicitly define in your schema. This is useful if you discover you...
Read more >
Can Avro schema have unknown / dynamic field names?
Show activity on this post. id and name fields are always known, but custom_fields names are dynamic and can have any key name....
Read more >
Dynamically adding fields in the schema doesn't permit to add ...
Hi, When I dynamically add a field to a schema and instantiate it to a model, mongoose is not ... Schema({ name: String,...
Read more >
How do I get a dynamic list of object field names to present in ...
You want to have an Apex Controller to get the field names and return a type that a Lightning Aura Component (LAC) component...
Read more >
How to use the dynamic field of SOLR - CodeLibs Project
dynamic fields and standard fields such as title and content can be freely defined field names are defined. The dynamic fields that are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found