python - JSON schema validation with arbitrary keys -


i using validictory validating attached json data , schema. working far.

however data dictionary can have arbitrary string keys (others 'bp' but). key 'bp' in schema here hard-coded...it can string given list (enum of string). how add enum definition here "first level" of 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) 

it depends on you're trying do.

if want same specification, range of properties, can abstract out definition:

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

if want any properties follow schema, can use additionalproperties:

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

or range of properties (matched pattern) - instance, lower-case:

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

if want limit number of properties can defined, can use "maxproperties" (v4 of standard only):

{     "type": "object",     "additionalproperties": {...},     "maxproperties": 1 } 

p.s. - in v4 of standard, "required" array. in fact, in v3, "required" defaults false, example doesn't need @ all


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -