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 make schema for dict fields with variable names?

See original GitHub issue

For example, suppose we have the following JSON schema:

...
"metrics": {
  "type": "object",
  "patternProperties": {
    "^[a-zA-Z]+$": {
      "type": "object",
      "required": ["v", "date"],
      "properties": {
        "v": {"type": "number"},
        "date": {"type": "string"}
      },
      "additionalProperties": false
    }
  }
}
...

How to define marshmallow’s schema for it?

I can validate it like this:

@MetricsSchema.validator
def validate_metrics(schema, input_data):
    metric_key, metric_value = input_data['metrics'].items()[0]
    if not re.compile(r'^[a-zA-Z]+$').match(metric_key):
        raise ValidationError('Metric\'s name must be alphabetical symbols.')
    errors = PredefinedMetricSchema().validate(metric_value)
    if errors:
        raise ValidationError(errors)

But how to make schema for serializing? I think it should have clear decision, because this case can be occured in schemas in your new project named smore.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
sloriacommented, Mar 16, 2015

@vovanbo Apologies for the delayed response. Here’s a stab at a DictField that might meet your use case:

from marshmallow import Schema, fields, validate

class DictField(fields.Field):

    def __init__(self, key_field, nested_field, *args, **kwargs):
        fields.Field.__init__(self, *args, **kwargs)
        self.key_field = key_field
        self.nested_field = nested_field

    def _deserialize(self, value):
        ret = {}
        for key, val in value.items():
            k = self.key_field.deserialize(key)
            v = self.nested_field.deserialize(val)
            ret[k] = v
        return ret

    def _serialize(self, value, attr, obj):
        ret = {}
        for key, val in value.items():
            k = self.key_field._serialize(key, attr, obj)
            v = self.nested_field.serialize(key, self.get_value(attr, obj))
            ret[k] = v
        return ret


class MetricSchema(Schema):
    value = fields.Float()
    date = fields.DateTime()

class UserSchema(Schema):
    metrics = DictField(
        fields.Str(validate=validate.Regexp(r'^[a-zA-Z]+$')), 
        fields.Nested(MetricSchema)
    )

metrics = {
    "metrics": {
        "firstMetricName": {
            "value": 100,
            "date": "2015-01-09T04:33:17+00:00"
        },
        "secondMetricName": {
            "value": 110,
            "date": "2015-01-09T04:33:17+00:00"
        }
    }
}

s = UserSchema(strict=True)
s.load(metrics).data
# {'metrics': {'firstMetricName': {'value': 100.0,
#    'date': datetime.datetime(2015, 1, 9, 4, 33, 17, tzinfo=tzutc())},
#   'secondMetricName': {'value': 110.0,
#    'date': datetime.datetime(2015, 1, 9, 4, 33, 17, tzinfo=tzutc())}}}
5reactions
lauwerscommented, Jan 4, 2016

I noticed you added a regular Dict(), but are there any plans to support the DictField() as presented above? I have lots of maps of named objects for which this functionality would be useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to create a schema of a table by fetching column names ...
The idea is to split the string by "=" and use the first part as a dict key (for table name) and create...
Read more >
Creating a REDCap Data Dictionary - UT Southwestern
In either case, what you're creating is referred to as the data dictionary, a description of all the field (variable) names, field types,...
Read more >
Generating a Dictionary Mapping from Field Names to Column ...
This recipe shows a function that takes a DB API 2.0 cursor object and returns a dictionary with column numbers keyed to field...
Read more >
How to use the marshmallow.fields.Dict function in ... - Snyk
To help you get started, we've selected a few marshmallow.fields.Dict examples ... description="Schema name", example="prefs",) schema_version = fields.
Read more >
Specifying a schema | BigQuery - Google Cloud
To create a JSON schema file, enter a TableFieldSchema for each column. The name and type fields are required. All other fields 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