Skip to content

Is it possible to add ability to convert raw dict to reduced dict using schema? #455

@asherascout

Description

@asherascout

jsonschema already have a function validate to validate a raw dict with a schema.

To explain what I mean, for example, there is a dict:

raw_dict = {
  "latitude": 48.858093,
  "longitude": 2.294694,
  "extra_key": "value1"
}

and a schema:

schema = {
  "id": "https://example.com/geographical-location.schema.json",
  "$schema": "http://json-schema.org/draft-06/schema#",
  "title": "Longitude and Latitude Values",
  "description": "A geographical coordinate.",
  "required": [ "latitude", "longitude" ],
  "type": "object",
  "properties": {
    "latitude": {
      "type": "number",
      "minimum": -90,
      "maximum": 90
    },
    "longitude": {
      "type": "number",
      "minimum": -180,
      "maximum": 180
    }
  }
}

Now I want a function which like:

def reduce(instance, schema):
    ...
    return new_instance

>>> reduce(raw_dict, schema)
>>> raw_dict = {
      "latitude": 48.858093,
      "longitude": 2.294694
    }

the reduce function validate the raw_dict and pass it ,then convert it to the result which fit the schema, if the dict is not passed, it behaves like validate, raise an error.

If this function is allowed to add into jsonschema, I can take some time to write it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions