Cannot find classification schema reference in JSON Schema - jsonschema

I have a product schema which tries to reference an id in my document. It is a common reference to multiple objects. Unfortunately, my ide claims classification reference cannot be found. I am very new to json schemas and find only snippets which don't quite show how the references are supposed to work. Here is my schema.
{
"$schema": "https://json-schema.org/draft-07/schema#",
"$id": "https://digital.com/schemas/products",
"description": "Schema for Product Data",
"title": "Products",
"type": "object",
"required": ["products"],
"properties": {
"products": {
"type": "array"
},
"options": {
"type": "array",
"items": {
"type": "object",
"properties": {
"productId": {
"type": "string"
},
"productName": {
"type": "string"
},
"categories": {
"type": "object",
"additionalProperties": false,
"allOf": [
{ "$ref":"/products/classification" }
]
},
"productType": {
"type": "string",
"enum": ["electronic", "digital", "internet", "video"]
}
},
"required": ["productId"]
}
},
"classification": {
"$id": "/products/classification",
"type":"object",
"properties": {
"relevance-score": {
"type":"integer",
"minimum": 1,
"maximum": 5
},
"group":{
"enum":["adult","teen","seniors"]
}
}
},
"definitions": {
"mp4": {
"type": "object",
"properties": {
"mediaType": {
"type": "string",
"enum": ["video"]
},
"playlength": {
"type": "integer"
}
}
}
},
"mp3": {
"type": "object",
"properties": {
"mediaType": {
"enum": ["audio"]
}
}
}
}
}
I have defined a classification object to use in the class property of categories object like this
"$id":"/products/classification".
I tried setting it relative to the $id at the top of the document which is "https://digital.com/schemas/products" but I'm sure I haven't set it correctly. I want to use the classification object in the allOf property of the categories object.

Your reference is "$ref": "/products/classification". Where is this supposed to point?
Odds are the validator you're using is looking for a schema with the $id of "https://digital.com/products/classification". If the validator doesn't know about this schema (or perhaps is searching for that file), it can't get to it.
The other possibility is that you intend this to be a JSON Pointer. If that's the case, it needs to be URI-formatted: "#/products/classification"
However, this location doesn't existin within your schema, so it'll need to be updated.

Related

Add additional properties to json array member schema

I'm trying to create 2 separate schemas for some json files which serve as scripts.
One schema should contain all commands possible, while another one should only contain a subset of commands.
My thinking was I create minimal schema first:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"$schema": {},
"commands": {
"type": "array",
"items":
{
"title": "One step of the migration",
"type": "object",
"minProperties": 1,
"maxProperties": 1,
"properties": {
"simple_command_1": {"type": "object"},
"simple_command_2": {"type": "object"},
}
}
}
}
}
which would validate json files like this:
{
"$schema": "../../migration_schema_v1.json",
"commands": [
{"simple_command_1": {}},
{"simple_command_1": {}},
{"simple_command_2": {}},
{"simple_command_2": {}},
]
I'm trying to avoid using the word extend but... I want to be able to create a second schema that has every command the first one has and an additional advanced_command_3. So I created this:
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": "/migration_schema_v1.json#/",
"type": "object",
"properties": {
"foo": {
"type": "object",
"title": "this does work"
},
"commands": {
"items":
{
"properties": {
"advanced_command_3": {"type": "object"}
}
}
}
}
}
I'm able to refer this second schema, and it does inherit everything from the first one, but I'm not really able to add any properties (commands)
I was however able to add additional properties in the root, next to "commands". See "foo" property.
Using allOf[] or $ref in the sub-property doesn't seem to make a difference.
Am I getting this all wrong?
Thanks!
I would actually break this into three schemas. The first contains the primary definition for your data, including all of the valid commands.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/definitions",
"type": "object",
"properties": {
"$schema": {},
"commands": {
"type": "array",
"items":
{
"title": "One step of the migration",
"type": "object",
"minProperties": 1,
"maxProperties": 1,
"properties": {
"simple_command_1": {"type": "object"},
"simple_command_2": {"type": "object"},
"advanced_command_3": {"type": "object"}
}
}
}
}
}
The other two are your simple and advanced schemas. They work by defining that only the possibilities of the commands you want are required then referencing back to the definition schema to get the rest of the requirements.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/simple",
"$ref": "definitions",
"properties": {
"commands": {
"items": {
"anyOf": [
{ "required": ["simple_command_1"] },
{ "required": ["simple_command_2"] }
]
}
}
}
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/advanced",
"$ref": "definitions",
"properties": {
"commands": {
"items": {
"anyOf": [
{ "required": ["simple_command_1"] },
{ "required": ["simple_command_2"] },
{ "required": ["advanced_command_3"] }
]
}
}
}
}
I think this setup contains minimal repetition.
At this point you might be able to get away with what you're wanting to do with the extends thing. You'd have to extract the commands requirements in the simple schema into a $defs keyword and reference them from the advanced schema.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/simple",
"$defs": {
"simpleCommands": {
"anyOf": [
{ "required": ["simple_command_1"] },
{ "required": ["simple_command_2"] }
]
}
},
"$ref": "definitions",
"type": "object",
"properties": {
"commands": {
"items": { "$ref": "#/$defs/simpleCommands" }
}
}
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/advanced",
"$ref": "definitions",
"type": "object",
"properties": {
"commands": {
"items": {
"anyOf": [
{ "$ref" : "simple#/$defs/simpleCommands" },
{ "required": ["advanced_command_3"] }
]
}
}
}
}
I haven't tested this though.

How to properly define an object within definition and reference in multiple places for Json Schema

I am trying to create a json schema wherein I have an object within a definition & this definition is called within multiple places.
I see an error saying UnhandledPromiseRejectionWarning: Error: duplicate type name: Location
I have the below code.
{
"$schema": "http://json-schema.org/draft-07/schema",
"definitions": {
"holiday": {
"description": "A collection of time off associated with the employee.",
"required": [],
"properties": {
"location": {
"type": "object",
"nullable": true,
"title": "Location",
"properties": {
"city": {
"type": "string",
"nullable": true,
"tsType": "string | null",
"description": ""
}
}
}
},
"type": "object"
}
},
"description": "The model for the employee object received on the Ingress API.",
"properties": {
"eventType": {
"avroType": "string",
"enum": ["EMPLOYEE_TIMEOFF_CREATED", "EMPLOYEE_CREATED_OR_UPDATED"],
"tsEnumNames": ["EmployeeTimeOffCreated", "EmployeeCreatedOrUpdated"],
"type": "string"
},
"employeeCreatedOrUpdated": {
"description": "Event data for a employee create request.",
"required": ["code", "firstName", "lastName"],
"properties": {
"code": {
"description": "A unique code for an employee.",
"minLength": 1,
"type": "string"
},
"firstName": {
"description": "This field describes the first name of the employee.",
"minLength": 1,
"type": "string"
},
"middleName": {
"description": "This field describes the middle name of the employee.",
"nullable": true,
"tsType": "string | null",
"type": "string"
},
"lastName": {
"description": "This field describes the last name of the employee.",
"minLength": 1,
"type": "string"
},
"timeOff": {
"description": "A collection of employee time off associated with the employee.",
"items": {
"$ref": "#/definitions/holiday"
},
"nullable": true,
"type": "array"
}
},
"title": "EmployeeCreatedOrUpdated",
"type": "object"
},
"employeeTimeOffCreated": {
"description": "Event data for an employee time off created request.",
"required": ["timeOffCreated", "employeeCode"],
"$id": "https://io.something/v2/employee/employeeTimeOffCreated",
"properties": {
"timeOffCreated": {
"$ref": "#/definitions/holiday"
},
"employeeCode": {
"description": "A unique code for an employee.",
"minLength": 1,
"type": "string"
}
},
"title": "EmployeeTimeOffCreated",
"type": "object"
}
},
"required": ["eventType"],
"title": "EmployeeEvent",
"type": "object"
}
So, I use the holiday definition at two places. I tried to have an id within $ref but that doesn't work. Any help is highly appreciated. Thanks.
Here's your schema reduced to only the parts needed to understand the problem.
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"employeeCreatedOrUpdated": {
"type": "object",
"properties": {
"timeOff": {
"type": "array",
"items": { "$ref": "#/definitions/holiday" }
}
}
},
"employeeTimeOffCreated": {
"$id": "https://io.something.ingress/v2/employee/employeeTimeOffCreated",
"type": "object",
"properties": {
"timeOffCreated": { "$ref": "#/definitions/holiday" }
}
}
}
"definitions": {
"holiday": true
}
}
When you use $id in a sub-schema like this, it indicates that the sub-schema is a completely separate schema embedded in the parent schema. Any references inside of the embedded schema are relative the the $id, not the parent schema. So, the reference at "timeOffCreated" expects #/definitions/holiday relative to the embedded schema. There's nothing there, so you get an error.
If you don't need "employeeTimeOffCreated" to be an embedded schema, the easiest thing is to remove $id and your references will work. Otherwise, you can give /definitions/holiday an $id as well and reference with that URI instead.
Embedded schemas are really only good for bundling schemas for distribution. Otherwise, you probably want to maintain separate schemas for each of your entities and just reference other schemas when you need them.
Here's what it would look like neatly bundled. If you want to work on these as separate schemas as suggested, you just need to extract each of the definitions into their own schema.
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "https://io.something.ingress/v2/employee",
"type": "object",
"properties": {
"employeeCreatedOrUpdated": {
"type": "object",
"properties": {
"timeOff": {
"type": "array",
"items": { "$ref": "./holiday" }
}
}
},
"employeeTimeOffCreated": { "$ref": "./employeeTimeOffCreated" }
}
"definitions": {
"employeeTimeOffCreated": {
"$id": "./employeeTimeOffCreated",
"type": "object",
"properties": {
"timeOffCreated": { "$ref": "./holiday" }
}
},
"holiday": {
"$id": "./holiday",
...
}
}
}
Additional reference: https://json-schema.org/understanding-json-schema/structuring.html#bundling

How can I validate a json schema array that contains a MIXED type of objects?

I have searched and haven't quite found a solution.
I would like to do a schema as so:
...
"bag": {
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/definitions/obj1"},
{"$ref": "#/definitions/obj2"},
{"$ref": "#/definitions/obj3"}
]
},
"required": ["items"],
"minItems": 1
}
...
With objects defined:
...
"definitions": {
"obj1": {
"type": "object",
"properties": {
"obj1": {
"type": "object",
"properties": {
"a": {
"type": "string"
}
},
"required": ["a"]
}
}
},
"obj2": {
"type": "object",
"properties": {
"obj1": {
"type": "object",
"properties": {
"b": {
"type": "string"
}
},
"required": ["b"]
}
}
},
"obj3": {
"type": "object",
"properties": {
"obj1": {
"type": "object",
"properties": {
"c": {
"type": "string"
}
},
"required": ["c"]
}
}
}
}
...
Ideally, I would like to validate against a schema that looks like this:
...
"bag": [
{
"obj1": {"a": "test1"}
},
{
"obj3": {"c": "test1"}
}
]
...
In this context, if someone passes obj1 and obj3 into bag. By the schema, obj1 requires property a and obj3 requires property c.
I'm having trouble actually executing this as the validation doesn't seem to enforce correctly.
Any tips? Thanks in advance.
From your current schema and example data, I can't tell exactly what you want, but making an educated guess...
I suspect you want to use oneOf as opposed to anyOf.
anyOf allows you to match multiple subschemas, and it looks like you only want to allow matching one of the subschemas, obj1, 2, or 3.
This would help you debug the issue, but it's not the cause of your always passing validation.
For each definition subschema, you need to add "additionalProperties": false.
Here's the key: JSON Schema is constraints based, meaning anything not constrained is allowed.
additionalProperties restricts the allowed properties of an object to those defined in properties (and patternProperties).
Here's the example schema. You can see it working with your instance here: https://jsonschema.dev/s/MjBUp
{
"$schema": "http://json-schema.org/draft-07/schema",
"definitions": {
"obj1": {
"type": "object",
"properties": {
"obj1": {
"type": "object",
"properties": {
"a": {
"type": "string"
}
},
"required": ["a"]
}
},
"additionalProperties": false
},
"obj2": {
"type": "object",
"properties": {
"obj1": {
"type": "object",
"properties": {
"b": {
"type": "string"
}
},
"required": ["b"]
}
},
"additionalProperties": false
}
},
"type": "array",
"items": {
"anyOf": [
{"$ref": "#/definitions/obj1"},
{"$ref": "#/definitions/obj2"}
]
},
"required": ["items"],
"minItems": 1
}

jsonschema with custom property

I want to define a JsonSchema with a customProperty in it, this property follows some rules, so in order to validate those, I need to define a JsonSchema that will validate it.
So far I've managed to describe it properly, but it only works for the first level of attributes, and I want it to be recursive...
From my comprehension it should work, I probably made a mistake I'm unable to see and at this point I don't know if it's a bug, impossible, or stupid...
I believe redefining every type should be a possibility , but obviously I'd rather not.
Here is an example of the Json I want to validate
{
"title": "TheObject",
"type": "object",
"properties": {
"aString": {
"type": "string",
"myCustomProperty": {}
},
"anObjet": {
"type": "object",
"myCustomProperty": {},
"properties": {
"anotherObject": {
"type": "object",
"myCustomProperty": {}, //if this line is removed it still validates which I don't want
"properties": {}
}
}
}
}
}
and here is the JsonSchema I've done so far:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"title": {"type": "string"},
"type": {"type": "string","enum": ["object"]},
"properties": {
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Field"
}
}
}
},
"definitions": {
"Field": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"myCustomProperty": {
"$ref": "#/definitions/myCustomProperty"
},
"patternProperties": {
"^(?!myCustomProperty).*": {
"$ref": "#/definitions/Field"
}
}
},
"required": [
"type",
"myCustomProperty"
]
},
"myCustomProperty": {
//Some rules
}
}
}
I found a solution, I wasn't far from what I wanted after all.
In my definition of "Field", I am describing an object that defines an object, and I was missing the "properties" field. In which I had to put my recursive reference.
The right jsonSchema is the following :
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"object"
]
},
"properties": {
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Field"
}
}
}
},
"definitions": {
"Field": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"myCustomProperty": {
"$ref": "#/definitions/myCustomProperty"
},
"properties": { <==================== here
"type": "object",
"patternProperties": {
".*": {
"$ref": "#/definitions/Field"
}
}
}
},
"required": [
"type",
"myCustomProperty"
]
},
"myCustomProperty": {/*rules*/}
}
}
So far it works as expected, but if someone has a more elegant suggestion please share!

json schema object property constraints

In my schema I have an array of phone objects. Each object has a "status" property, which can be one of three values: "Primary", "Active" and "Not-in-use".
I want to set the following constraint:
If the number of phone objects > 0 then exactly one must have status="Primary"
Is this possible with json schema? If so, how?
This schema is pretty close to what you want. The only restriction is that the "Primary" phone number needs to be the first item in the array.
You might be able to get "Primary" to be anywhere in the array with some creative use of not. I'll update the answer if I figure it out.
{
"type": "object",
"properties": {
"phoneNumbers": {
"type": "array",
"items": [{ "$ref": "#/definitions/primaryPhone" }],
"additionalItems": { "$ref": "#/definitions/additionalPhone" }
}
},
"definitions": {
"phone": {
"type": "object",
"properties": {
"label": { "type": "string" },
"number": { "type": "string" }
},
"required": ["label", "number", "status"]
},
"primaryPhone": {
"allOf": [{ "$ref": "#/definitions/phone" }],
"properties": {
"status": { "enum": ["Primary"] }
}
},
"additionalPhone": {
"allOf": [{ "$ref": "#/definitions/phone" }],
"properties": {
"status": { "enum": ["Active", "Not-in-use"] }
}
}
}
}