How to reference json schema definition from another schema - json

I have a json schema representing a geometry as a Point or a MultiPoint. Each is defined within the schema in "definitions":
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/geometry.json#",
"type": "object",
"oneOf": [
{
"allOf": [
{
"required": [
"type",
"coordinates"
]
},
{
"oneOf": [
{
"$ref": "#/definitions/Point"
},
{
"$ref": "#/definitions/MultiPoint"
}
]
}
]
}
],
"definitions": {
"Point": {
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"$ref": "#/definitions/position"
}
}
},
"MultiPoint": {
"title": "MultiPoint",
"type": "object",
"properties": {
"type": {
"enum": [
"MultiPoint"
]
},
"coordinates": {
"$ref": "#/definitions/positionArray"
}
}
},
"position": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
},
"positionArray": {
"type": "array",
"items": {
"$ref": "#/definitions/position"
}
}
}
}
Now I want to make another schema, which makes use of the Point definition. Currently, I copypasted the definitions for Point and position in the properties "startPosition" and "endPosition" and it works. But is there a way how to just reference the Point's definition from my geometry.json schema?
note: I only want to allow Point to be used here but not a MultiPoint - A geometry.json ref would allow both instead.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/myitem.json#",
"type": "object",
"additionalProperties": false,
"required": [
"myproperty"
],
"properties": {
"myproperty": {
"type": "array",
"minItems": 0,
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"startPosition": {
"geometry": {
"required": [
"type",
"coordinates"
],
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
}
},
"endPosition": {
"geometry": {
"required": [
"type",
"coordinates"
],
"title": "Point",
"type": "object",
"properties": {
"type": {
"enum": [
"Point"
]
},
"coordinates": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"additionalItems": false,
"items": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
}
}
}
}
}
}
}

Haven't tested it myself, but according to this, you can make use of JSON pointers:
In file geometry.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/geometry.json",
"type": "object",
"definitions": {
"Point": { ...},
"MultiPoint": {...}
}
}
In file myitem.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://schema.my-site.org/myitem.json#",
"type": "object",
"properties": {
"point": {
"$ref": "http://schema.my-site.org/geometry.json#definitions/Point"
}
}
}

Related

JSON Schema Grammar Not Validating Enums

I have defined the following JSON Schema Grammar which has to take "TypeA" type of elements and none other than that.
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "Schema definition",
"type": "object",
"scname": "string",
"properties": {
"itemInfo": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/definitions/TypeADefinition"
}
]
}
}
},
"required": [
"itemInfo"
],
"definitions": {
"TypeADefinition": {
"type": "object",
"properties": {
"elementOf": {
"types": {
"enum": [
"TypeA"
]
}
},
"elements": {
"items": {
"oneOf": [
{
"$ref": "#/definitions/TypeAElementDefinition"
}
]
},
"type": "array"
}
}
},
"TypeAElementDefinition": {
"type": "object",
"properties": {
"name": {
"type": "string",
"maxLength": 128
}
},
"required": [
"name"
],
"additionalProperties": false
}
}
}
JSON Object 1:
{
"itemInfo": [
{
"elementOf": "TypeA",
"elements": [
{
"name": "John Doe"
}
]
}
]
}
JSON Object 2:
{
"itemInfo": [
{
"elementOf": "TypeB",
"elements": [
{
"name": "John Doe"
}
]
}
]
}
Both of these JSON objects are getting validated by the JSON grammar that I have defined but only the first JSON object should be validated successfully by the grammar the second JSON should not validated as it has elementOf "TypeB".
Is there anything that is missing in my Schema Grammar?
UPDATE
After updating the schema grammar to the following, I am able to invalidate 2nd JSON object.
{
"$schema": "http://json-schema.org/draft-04/schema",
"title": "Schema definition",
"type": "object",
"scname": "string",
"properties": {
"itemInfo": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/definitions/TypeADefinition"
},
{
"$ref": "#/definitions/TypeBDefinition"
}
]
}
}
},
"required": [
"itemInfo"
],
"definitions": {
"TypeADefinition": {
"type": "object",
"properties": {
"elementOf": {
"type": "string",
"enum": [
"TypeA"
]
},
"elements": {
"items": {
"oneOf": [
{
"$ref": "#/definitions/TypeAElementDefinition"
}
]
},
"type": "array"
}
}
},
"TypeAElementDefinition": {
"type": "object",
"properties": {
"name": {
"type": "string",
"maxLength": 128
}
},
"required": [
"name"
],
"additionalProperties": false
},
"TypeBDefinition": {
"type": "object",
"properties": {
"elementOf": {
"type": "string",
"enum": [
"TypeB"
]
},
"elements": {
"items": {
"oneOf": [
{
"$ref": "#/definitions/TypeBElementDefinition"
}
]
},
"type": "array"
}
}
},
"TypeBElementDefinition": {
"type": "object",
"properties": {
"name": {
"type": "string",
"maxLength": 128
}
},
"required": [
"name"
],
"additionalProperties": false
}
}
}
I have also added a definition for 'TypeB' in the grammar which should validate TypeB elements. But it is not happening.
TypeB element is referring to 'TypeADefinition', which should not happen. What is the issue with the grammar?
Error:
Message: Value "TypeB" is not defined in enum.
Schema path: #/definitions/TypeADefinition/properties/elementOf/enum

How to prevent additions properties when using oneOf in JSON Schema

I have a JSON configuration with an array of "specs", with many different kinds of "path" properties. The "kind" property is the selector of the variant.
I want to restrict the validation to not allow addition properties. In the JSON example, the properties "noAllowedInt" and "notAllowedObject" must report a validation error.
How can I add this rule to the JSON schema?
{
"specs": [
{
"id": 12,
"label": "Serial",
"path": {
"kind": "serial",
"speed": 9600,
"parity": "even"
}
},
{
"id": 13,
"label": "Memory",
"path": {
"kind": "memory",
"storage": "permanent",
"location": "external"
},
"noAllowedInt": 42,
"notAllowedObject": {
"value": 3.1415
}
}
]
}
There are two Json Schema to validate this, a simple one and one with "definitions"
Simple JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
}
},
"required": [
"id",
"label"
],
"oneOf": [
{
"type": "object",
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"serial"
]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
},
{
"type": "object",
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"memory"
]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
}
]
}
}
},
"required": [
"specs"
]
}
JSON Schema with "definitions"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
}
},
"required": [
"id",
"label"
],
"oneOf": [
{
"$ref": "#/definitions/PathKindSerialType"
},
{
"$ref": "#/definitions/PathKindMemory"
}
]
}
}
},
"required": [
"specs"
],
"definitions": {
"PathKindSerialType": {
"type": "object",
"additionalProperties": true,
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"serial"
]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
},
"PathKindMemory": {
"type": "object",
"additionalProperties": true,
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"memory"
]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
}
}
}
I guess you need to keep the path definition as a property at the specs array level. This would be my proposal:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
},
"path": {
"oneOf": [
{
"$ref": "#/definitions/PathKindSerialType"
},
{
"$ref": "#/definitions/PathKindMemory"
}
]
}
},
"required": [ "id", "label" ]
}
}
},
"required": [ "specs" ],
"definitions": {
"PathKindSerialType": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [ "serial" ]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [ "kind" ]
},
"PathKindMemory": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [ "memory" ]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [ "kind" ]
}
}
}

json schema definition using conditional statements

Iam trying to define an optional condition using json schema conditional statement (Using draft 7)
I have a json response like this.
[{
"views": [{
"name": "RSO Roster",
"displayOrder": 5,
"groups": [{
"type": "scrollable",
"displayOrder": 1,
"auditType": "player-pregame_roster",
"tiles": [{
"context": "event",
"dataStamp": 1535184247,
"tile_type": "person"
}, {
"context": "event",
"errorCode": 2,
"errorText": "seloger",
"tile_type": "person"
}
]
}
]
},
{
"name": "Leaders",
"displayOrder": 1,
"groups": [{
"type": "static",
"displayOrder": 1,
"tiles": [{
"context": "event",
"dataStamp": 1535184247,
"eventId":123
"tile_type": "static"
}
]
}
]
}
]
}]
In this response if the tile object contains the key errorCode the required field must be errorText and errorCode keys.Like wise
if the tile object doessnot contains any "errorCode" or "errorText" key then the tile item contains the required field "dataStamp".
To validate the above condition i have defined a schema like below.But it is not working.Whats wrong with my schema .
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"views": {
"$id": "views",
"type": "array",
"items": {
"$id": "views/items",
"type": "object",
"properties": {
"groups": {
"$id": "views/groups",
"type": "array",
"items": {
"$id": "views/groups/items",
"type": "object",
"properties": {
"tiles": {
"$id": "views/groups/tiles",
"type": "array",
"items": {
"$id": "views/groups/tiles/items",
"type": "object",
"properties": {
"dataStamp": {
"$id": "views/groups/tiles/dataStamp",
"type": "integer"
},
"tile_type": {
"$id": "views/groups/tiles/tile_type",
"type": "string"
},
"errorCode": {
"type": "integer",
"enum": [
2, 10
]
},
"errorText": {
"type": "string",
"enum": [
"seloger", "france24"
]
}
},
"if": {
"properties": {
"tile_type": {
"enum": ["person"]
},
"errorCode": {
"enum": [2, 10]
}
},
"required": ["errorCode", "errorText"]
}
}
}
},
"required": [
"type",
"tiles"
]
}
}
},
"required": [
"groups"
]
}
}
},
"required": [
"views"
]
}
}
The if statement is missing required in properties:
"if": {
"properties": {
"tile_type": {
"enum": ["person"]
},
"errorCode": {
"enum": [2, 10]
},
"required": ["errorCode"]
}
},
If there is no required the value of property is validated only if the property is set. So original if schema would pass any object without tile_type and errorCode.
https://stackoverflow.com/a/51034071/329463 might give you some inspiration on building exclusive properties clusters.
EDIT: modified full schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"views": {
"$id": "views",
"type": "array",
"items": {
"$id": "views/items",
"type": "object",
"properties": {
"groups": {
"$id": "views/groups",
"type": "array",
"items": {
"$id": "views/groups/items",
"type": "object",
"properties": {
"tiles": {
"$id": "views/groups/tiles",
"type": "array",
"items": {
"$id": "views/groups/tiles/items",
"type": "object",
"properties": {
"dataStamp": {
"$id": "views/groups/tiles/dataStamp",
"type": "integer"
},
"tile_type": {
"$id": "views/groups/tiles/tile_type",
"type": "string"
},
"errorCode": {
"type": "integer",
"enum": [
2, 10
]
},
"errorText": {
"type": "string",
"enum": [
"seloger", "france24"
]
}
},
"if": {
"properties": {
"tile_type": {
"enum": ["person"]
},
"errorCode": {
"enum": [2, 10]
}
},
"required":["errorCode"]
},
"then": {
"required": ["errorCode", "errorText"]
},
"else": {
"required": ["dataStamp"]
}
}
}
},
"required": [
"type",
"tiles"
]
}
}
},
"required": [
"groups"
]
}
}
},
"required": [
"views"
]
}
}

Json schema auto completion property

I have already created my own schema on intellij environment, and it's working good, but still have problems in auto completion which provides intellij to the schema,
for example if object "car" defined in json schema then intellij can recognize that there's such object in the schema and intellij will give it as suggestion through out coding json, the problem that I'm facing is that the suggestions are contains all the objects that defined in the schema, but the expectations are to get the objects which defined under the scoop of another object
This is some code of my own schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Pipe File",
"type": "object",
"definitions": {
"Pipe": {
"type": "object",
"properties": {
"components": {
"$ref": "#/definitions/components"
}
},
"required": [
"components"
]
},
"components": {
"description": "section which defines the pipes in the file",
"type": "array",
"minItems": 1,
"items": {
"oneOf": [
{
"$ref": "#/definitions/setValuesComponent"
},
{
"$ref": "#/definitions/invokeWebServicesComp"
}
]
}
},
"setValuesComponent": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"type": {
"enum": [
"setValuesComp"
]
},
"out": {
"type": "object",
"properties": {
"dateFormat": {
"$ref": "#/definitions/setValuesCompOut"
},
"dateTimeFormat": {
"$ref": "#/definitions/setValuesCompOut"
},
"dateFormatBank": {
"$ref": "#/definitions/setValuesCompOut"
}
}
},
"condition": {
}
},
"required": [
"name",
"type",
"out"
]
},
"setValuesCompOut": {
"type": "object",
"properties": {
"exprValue": {
"type": "string"
},
"ctxEntry": {
"type": "string"
},
"value": {
"type": "string"
},
"exprConst": {
"type": "string",
"pattern": "(Class|class)\\.\\w+\\.\\w+"
}
},
"anyOf": [
{
"required": [
"exprValue"
]
},
{
"required": [
"ctxEntry"
]
},
{
"required": [
"value"
]
},
{
"required": [
"exprConst"
]
}
]
},
"invokeWebServicesComp": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"type": {
"enum": [
"invokeWebServices"
]
},
"mode": {
"enum": [
"innerJoin",
"leftJoin",
"union",
"parallelJoin"
]
},
"method": {
"type": "string"
},
"headers": {
"$ref": "#/definitions/invokeWebServicesCompHeaders"
},
"dataFilePath": {
"type": "string"
},
"restRelativeUrl": {
"type": "string"
},
"in": {
"$ref": "#/definitions/invokeWebServicesCompIn"
},
"out": {
"$ref": "#/definitions/invokeWebServicesCompOut"
}
},
"required": [
"type",
"name",
"out",
"in"
]
},
"invokeWebServicesCompOut": {
"type": "object",
"patternProperties": {
"doc": {
"type": "string",
"pattern": ".+"
}
}
},
"invokeWebServicesCompHeaders": {
"type": "object",
"patternProperties": {
".{1,}": {
"type": "string",
"pattern": ".+"
}
}
},
"invokeWebServicesCompIn": {
"type": "object",
"patternProperties": {
".{1,}": {
"type": "string",
"pattern": ".+"
}
}
},
"properties": {
"pipes": {
"description": "section which defines the mandatory pipes object in the file",
"type": "object",
"patternProperties": {
".{1,}": {
"$ref": "#/definitions/Pipe"
}
}
}
},
"required": [
"pipes"
]
}
}
So what I expected is, when the type of object determined to "setValuesComp", the auto completion will suggest the relevant properties, that's mean it will not suggest "in" property which is belong to "invokeWebServicesComp" not "setValuesComponent".this picture show the auto complete problem in my real environment
Your JSON schema seems to be invalid. The below JSON content should be present inside object type.
"properties": {
"pipes": {
"description": "section which defines the mandatory pipes object in the file",
"type": "object",
"patternProperties": {
".{1,}": {
"$ref": "#/definitions/Pipe"
}
}
}
},
"required": [
"pipes"
]
In your schema, it is present as part of the "definitions". Please make this correction and then check if you are able to get the suggestions.

AJV schema validation error

I have the input json like below,
{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}
the condition is,
if the type is 'field', then 'itemId' should be the required field
and if the type is 'fieldGroup' or 'subSection', then 'itemId' is optional
This is the Json Schema I tried and its not working as expected,
"type": "object",
"additionalProperties": false,
"properties" : {
"contents" : {
"type" : "array",
"items": {"$ref": "#displayItem" }
}
},
"definitions": {
"displayItem" : {
"id": "#displayItem",
"type": "object",
"items": {
"anyOf": [
{"$ref": "#fieldType"},
{"$ref": "#fieldGroupSubSectionType"}
]
}
},
"fieldType" : {
"id": "#fieldType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["field"]
}
}
},
"fieldGroupSubSectionType" : {
"id": "#fieldGroupSubSectionType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": [ "string", "null" ]
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
}
Any help / workaround with Sample Json Schema to achieve the above use case is appreciated.
If I understand the description of what you want correctly, then the json example you provide is not valid since it has a type: "field" but does not have an "itemId" property.
Assuming that is true. Instead of using
type: ["string", null]
use the required property.
I changed your schema a bit, instead of having separate definitions I inlined them, but other than that (and the use of required) is the same:
{
"type": "object",
"additionalProperties": false,
"properties": {
"contents": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"field"
]
}
},
"required": [
"itemId"
]
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
]
}
}
}
}
Here is your answer with a little cleanup for best practices and style. The trick is that you need to use implication "a implies b <=> (not a) or b". In this case you have "type = field implies itemId is required <=> type is not field or itemId is required".
{
"type": "object",
"properties": {
"contents": {
"type": "array",
"items": { "$ref": "#/definitions/displayItem" }
}
},
"definitions": {
"displayItem": {
"type": "object",
"properties": {
"itemId": { "type": "string" },
"type": { "enum": ["field", "fieldGroup", "subSection"] }
},
"anyOf": [
{ "not": { "$ref": "#/definitions/fieldType" } },
{ "required": ["itemId"] }
]
},
"fieldType": {
"properties": {
"type": { "enum": ["field"] }
}
}
}
}