19

I am using validictory for validating the attached JSON data and schema. Working so far.

However the data dictionary can have arbitrary string keys (others than 'bp' but). The key 'bp' in the schema here is hard-coded...it can be a string from a given list (enum of string). How do I add the enum definition here for the "first level" of the dict.

import json
import validictory

data = {'bp': [{'category': 'bp',
         'created': '2013-03-08T09:14:48.148000',
         'day': '2013-03-11T00:00:00',
         'id': 'dc049c0e-d19a-4e3e-93ea-66438a239712',
         'unit': 'mmHg',
         'value': 147.0,
         'value2': 43.0}]}


schema = {
    "type":"object",
    "properties":{
        "bp": {
            "type":"array",
            "required":False,
            "items":
                {
                    "type":"object",
                    "required":False,
                    "properties":{
                        "category": {
                            "type":"string",
                            "default": "bp",
                            "required":False
                        },
                        "created": {
                            "type":"string",
                            "default": "2013-03-08T09:14:48.148000",
                            "required":False
                        },
                        "day": {
                            "type":"string",
                            "default": "2013-03-11T00:00:00",
                            "required":False
                        },
                        "id": {
                            "type":"string",
                            "default": "dc049c0e-d19a-4e3e-93ea-66438a239712",
                            "required":False
                        },
                        "unit": {
                            "type":"string",
                            "default": "mmHg",
                            "required":False
                        },
                        "value2": {
                            "type":"number",
                            "default":43,
                            "required":False
                        },
                        "value": {
                            "type":"number",
                            "default":147,
                            "required":False
                        }
                    }
                }


        }
    }
}

validictory.validate(data,schema)
  • Can you show us how to reproduce the problem? I don't see any right now. – jsalonen Apr 18 '13 at 11:09
  • 1
    @jsalonen: the problem is that the OP wants to validate the input if the top-level key is somthing different from `bp`. I think the problem description is clear enough, and someone with JSON-schema experience should be able to help. – Martijn Pieters Apr 18 '13 at 11:11
  • This seems to be a follow-on from your previous question. If you're having more general issues learning JSON Schema, you can drop by the JSON Schema [Google Group](https://groups.google.com/forum/#!forum/json-schema). It's also worth checking #json-schema on [freenode](http://webchat.freenode.net/) in case anyone's there. – cloudfeet Apr 18 '13 at 11:35

1 Answers1

32

It depends on exactly what you're trying to do.

If you want the same specification, but for a range of properties, you can abstract out the definition:

{
    "type": "object",
    "properties": {
        "bp": {"$ref": "#/definitions/categoryList"},
        "foo": {"$ref": "#/definitions/categoryList"},
        "bar": {"$ref": "#/definitions/categoryList"}
    },
    "definitions": {
        "categoryList": {...}
    }
}

If you want any properties to follow that schema, you can use additionalProperties:

{
    "type": "object",
    "additionalProperties": {...}
}

Or a range of properties (matched by a pattern) - for instance, anything lower-case:

{
    "type": "object",
    "patternProperties": {
        "^[a-z]+$": {...}
    }
}

If you want to limit the number of properties that can be defined, then you can use "maxProperties" (v4 of the standard only):

{
    "type": "object",
    "additionalProperties": {...},
    "maxProperties": 1
}

P.S. - in v4 of the standard, "required" is an array. In fact, even in v3, "required" defaults to false, so your example doesn't need it at all

cloudfeet
  • 12,156
  • 1
  • 56
  • 57
  • 4
    patternProperties did the job –  Apr 18 '13 at 12:08
  • I should say that if it's a fixed set of possible keys (e.g. 10 or so), then the first option is probably the most descriptive. However, if there's any infinite (or extremely large) range of them, then `patternProperties` is definitely the way to go. – cloudfeet Apr 18 '13 at 12:18