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)
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
JSON Schema is a powerful tool. It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements. You can add it to your code as an additional test or in run-time.
JSON Schema validation asserts constraints on the structure of instance data. An instance location that satisfies all asserted constraints is then annotated with any keywords that contain non-assertion information, such as descriptive metadata and usage hints.
The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With