How to set JSON validation schema for general properties values - json

I want to validate JSON using schema (currently draft 6, but we could upgrade if needed). My case is an object with properties whos values all have the same structure, e.g.:
{
"blueFoo": {
"bar1": "someValue",
"bar2": "differentValue"
},
"redFoo": {
"bar1": "someOtherValue",
"bar2": "LoremYpsum"
},
"purpleFoo": {
"bar1": "anotherString",
"bar2": "nextValue"
},
...
}
is there a way to set validation schema for general property values? Something like:
{
"type": "object",
"propertyValue": {
"type": "object",
"required": ["bar1", "bar2"],
"additionalProperties": false,
"properties": {
"bar1": {"type": "string"},
"bar2": {"type": "string"}
}
}
}
Thank you.

additionalProperties is exactly for that purpose:
{
"type": "object",
"additionalProperties": {
"properties": {
"bar1": {"type": "string"},
"bar2": {"type": "string"}
},
"required": ["bar1", "bar2"],
"additionalProperties": false
}
}

Related

JSON Schema Conditional Statements

I am trying to validate what I thought was a simple JSON schema as a configuration file for my Python application, it's a list of header key/value pairs, the only complication is that if the 'Type' field is set to 'AnyValue' then the value key is not required.
Here is the schema as it is:
{
"definitions":
{
'KeyEntry':
{
"properties":
{
'Type': {"type" : "string"},
'Key': {"type": "string"}
},
"required": ['Type', 'Key'],
"anyOf":
[
{
"if":
{
"properties": {'Type': {"const": 'ExactValue'}}
},
"then":
{
"properties":
{
'Value': {"type": "string"}
},
"required": ['Type', 'Key', 'Value'],
"additionalProperties": false
}
},
{
"if":
{
"properties": {'Type': {"const": 'AnyValue'}}
},
"then":
{
"required": ['Type', 'Key'],
"additionalProperties": false
}
}
]
}
},
"type": "object",
"properties":
{
'Keys':
{
"type": "array",
"items": {"$ref": "#/definitions/KeyEntry"}
}
},
"required": ['Keys']
}
Most of the validation works, except if I add extra values, even though I have set "additionalProperties": false throughout the schema.
Here is an example where extra values are accepted:
{
"Keys": [
{
"Type": "AnyValue",
"Key": "Version",
"Y": "Yes",
"N": "No",
}
]
}
Please can someone help explain where I have gone wrong and how I should correct it, please?
additionalProperties draft-07...
Validation with "additionalProperties" applies only to the child values of instance names that do not match any names in "properties", and do not match any regular expression in "patternProperties".
This means that additionalProperties is only aware of keywords which appear in properties or that match the regex from patternProperties. Using additionalProperties with required without properties is going to create a dud schema (nothing will pass validation).
In stead, you can separate the concerns into what you actually want...
You want type, key, and value, to all be strings.
One of...
If type is AnyValue, then require type and key only.
If type is ExactValue, then require type, key, and value.
This is also easier to understand. Here's a live demo with your data.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"KeyEntry": {
"properties": {
"Type": {
"type": "string"
},
"Key": {
"type": "string"
},
"Value": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"Type",
"Key"
],
"anyOf": [
{
"if": {
"properties": {
"Type": {
"const": "ExactValue"
}
}
},
"then": {
"required": [
"Type",
"Key",
"Value"
]
},
"else": false
},
{
"if": {
"properties": {
"Type": {
"const": "AnyValue"
}
}
},
"then": {
"required": [
"Type",
"Key"
]
},
"else": false
}
]
}
},
"type": "object",
"properties": {
"Keys": {
"type": "array",
"items": {
"$ref": "#/definitions/KeyEntry"
}
}
},
"required": [
"Keys"
]
}

enum list as object property in json schema

I'm looking to find a way to declare objects defined in an enum list.
Here is what I want to check :
{
"object1": {
"subobject1": {
"value": 123
},
"subobject2": {
"value": 456
}
},
"object2": {
"subobject3": {
"value": 789
}
},
"object3": {
"subobject4": {
"value": 123
}
},
"object4": {
"subobject5": {
"value": 234
}
}
}
Here is my schema that I want to use for validation :
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"definitions": {
"list1": {
"enum": [
"object1",
"object2",
"object3",
"object4"
],
"list2": {
"enum": [
"subobject1",
"subobject2",
"subobject3",
"subobject4",
"subobject5"
]
}
}
},
"properties": {
"type": {
"anyOf": [
{
"$ref": "#/definitions/list1"
}
]
}
},
"additionalProperties": false
}
But I get the following error :
Property 'object1' has not been defined and the schema does not allow additional properties.
I really want to be very restrictive and be able to declare only the one that as listed in the enum because I know that if I remove "additionalProperties": false I can add any propertry I want and it works.
The instance you gave as example can be validated with the following schema
{
"type": "object",
"definitions": {
"list1": {
"properties": {
"subobject1": {"type": "object"},
"subobject2": {"type": "object"},
"subobject3": {"type": "object"},
"subobject4": {"type": "object"},
"subobject5": {"type": "object"}
}
}
},
"properties": {
"object1": {"type": "object", "$ref": "#/definitions/list1"},
"object2": {"type": "object", "$ref": "#/definitions/list1"},
"object3": {"type": "object", "$ref": "#/definitions/list1"},
"object4": {"type": "object", "$ref": "#/definitions/list1"}
},
"additionalProperties": false
}
Maybe it's not what you wanted? Am I close?

`additionalProperties` rule in JSON schema is not applied to nested level properties

So I have a JSON schema with additionalProperties rule set to false like.
{
"type": "object",
"properties": {
"metadata": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
}
}
},
"street_type": {
"type": "string",
"enum": [
"Street",
"Avenue",
"Boulevard"
]
}
},
"additionalProperties": false
}
and a payload like
{
"metadata": {
"a": "aa",
"b": "bb",
"c": "cc",
"d": "dd"
}
}
Should I expect my JSON schema parser/validator to pass the validation, the JSON schema parser I am using com.github.fge.jsonschema.main.JsonSchema passes validation though metadata/d is not present in the schema with additionalProperties set to false,
This is very misleading, can someone direct me in the correct direction.
Is the additionalProperties JSON schema definition only applies to top-level fields and not to any nested level fields?
Is the additionalProperties JSON schema definition only applies to top-level fields and not to any nested level fields?
No you should be able to put it at whichever level you need as long as it is in a schema describing an object. In your case you simply put it at the wrong place. This should work:
{
"type": "object",
"properties": {
"metadata": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
}
},
"additionalProperties": false
},
"street_type": {
"type": "string",
"enum": [
"Street",
"Avenue",
"Boulevard"
]
}
}
}
Let say that you wanted to validate the following object as is:
{
a: {
b: {
c: {
d: 42
}
}
}
}
One valid schema for it would be:
{
"type": "object",
"additionalProperties": false,
"properties": {
"a": {
"type": "object",
"additionalProperties": false,
"properties": {
"b": {
"type": "object",
"additionalProperties": false,
"properties": {
"c": {
"type": "object",
"additionalProperties": false,
"properties": {
"d": {
"const": 42
}
}
}
}
}
}
}
}
}
The schema above is extremely verbose but is here for illustration purpose. You should be able to make it a bit more succinct by using $ref and combining schemas together.

json schema for dynamic array

I have the following json
{
"Dettype": "QTY",
"Details": [
{
"12568": {
"Id": 12568,
"qty":1,
"Freq":"2",
"Option": 0,
"promote":"yes"
},
"22456": {
"Id": 22456,
"qty":2,
"Freq":"3",
"Option": 1,
"promote":"no"
}
}
]
}
For the above json i need to write a json schema file which will valdiates the request.
but the problem is in array the key value for each item changes dynamically. If it is some constant value i can write but don't how to do the dynamic pattern
JSON schema i got
{
"type": "object",
"additionalProperties": true,
"properties": {
"Dettype": {
"type": "string"
},
"Details": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"**DYNAMIC VALUE**": {
"type": "object",
"additionalProperties": true,
"properties": {
"Id": {
"type": "integer"
},
"qty": {
"type": "integer"
},
"Freq": {
"type": "string"
},
"Option": {
"type": "integer"
},
"promote": {
"type": "string"
}
}
}
}
}
}
}
}
Can some one tell what changes need to be done for schema
This is what patternProperties is for.
Here it seems your object member keys are always digits; therefore you can write things like this:
"type": "object",
"patternProperties": {
"^\\d+$": {
"type": "object",
"etc": "etc"
}
}
You can also use additionalProperties if you want all properties to match some schema:
{
"type": "object",
"additionalProperties": {
"type": "object",
"etc": "etc"
}
}

JSON Schema - How to specify an object that contains anything?

I've got a JSON schema:
{
"required": ["name", "description"],
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"meta": { "type": "object" }
}
}
How do I specify that the meta nested object can contain absolutely anything?
Just leave the schema empty:
{
"required": ["name", "description"],
"properties": {
"name": {"type": "string"},
"description": {"type": "string"},
"meta": {}
}
}
If you don't specify any constraints, then anything goes!