JSON Schema: Additional properties false for embedded objects - json

I have a json schema with hundreds of objects, each containing other objects and so on...
I want all my json schema objects to have the option "additionalProperties" set to false, but it seems that I have to set it for each object in the json schema.
Is there any solution like setting "additionalProperties" to false only once for all objects including multiple level of embedded objects in the whole schema ?
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Modified JSON Schema draft v4 that includes the optional '$ref' and 'format'",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"box": {
"type": "object",
"properties": {
"anotherBox": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
},
"title": {
"type": "string"
}
}
}
Thank you

Related

Can json schema references have a template?

I'm working on reducing json schema, to try and make it easier to follow. Many of my nodes are of the same general form:
{
"[$PassedName]": {
"type": "object",
"properties": {
"current": {
"type": "object",
"[$PassedName]" : {"$ref": "#/definitions/Objects/[$PassedName]"}
},
"history": {
"type": "array",
"[$PassedName]" : {"$ref": "#/definitions/Objects/[$PassedName]"}
}
}
}
}
What I'd like to do is to pass a string to a reference, so the reference could use that string in the given locations. For example, giving the reference the string value "SSN" would have a resultant schema of the following:
{
"SSN": {
"type": "object",
"properties": {
"current": {
"type": "object",
"SSN": "#/definitions/Objects/SSN"
},
"history": {
"type": "array",
"SSN": "#/definitions/Objects/SSN"
}
}
}
}
Is this at all possible with json schemas? I've more than a dozen of basically the same node, so I'd like to simplify, if possible.

Is it possible to reference to JSON data itself within a schema definition?

Is it possible to define a JSON schema to check inner data integrity? So that mistakes like "ba" could be detected without further runtime checks.
Data JSON
{
"childNames": ["foo", "bar", "baz"],
"children": [
{
"name": "foo"
},
{
"name": "ba"
}
]
}
JSON Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://tbd.com/foo/bar",
"type": "object",
"properties": {
"childNames": {
"type": "array",
"items": { "type": "string" }
},
"children": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"enum": { "$ref": "???" }
}
}
}
}
}
}
If you're using .Net, then you can do this with JsonSchema.Net and JsonSchema.Net.Data.
You'll need to use a custom vocabulary which defines the data keyword.
Your schema will need to change to:
{
"$schema": "https://gregsdennis.github.io/json-everything/meta/data",
"$id": "https://tbd.com/foo/bar",
"type": "object",
"properties": {
"childNames": {
"type": "array",
"items": { "type": "string" }
},
"children": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"data": {
"enum": "#/childNames"
}
}
}
}
}
}
}
Note the data keyword where your ??? was and the different meta-schema in the $schema keyword.
You can try it on https://json-everything.net/json-schema.
Information on how this works is here.
If you're not using .Net, you may need to figure out how to support this vocabulary (or work with the maintainer of the library you're using to do so).
Disclaimer: I'm the creator & maintainer of JsonSchema.Net and the other json-everything libraries.

JSON Schema reference resolution

I have a JSON schema that contains "$ref" tags and I am trying to get a version of the JSON schema that have the "$ref" tags resolved. I am only looking to resolve "$ref" from definition (tags) within the JSON Schema string (ie. not external resolution needed).
Is there a library that performs the resolution of the JSON Schema? (I am currently using org.everit.json.schema library, which is great, but I can't find how to do what I need).
For example, my original schema is:
{
"$id": "https://example.com/arrays.schema.json",
"description": "A representation of a person, company, organization, or place",
"title": "complex-schema",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": { "$ref": "#/$defs/veggie" }
}
},
"$defs": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
Which would resolve to something like this (notice that the "#defs/veggie" resolves to its definition inserted inline in the schema):
{
"$id": "https://example.com/arrays.schema.json",
"description": "A representation of a person, company, organization, or place",
"title": "complex-schema",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "string"
}
},
"vegetables": {
"type": "array",
"items": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
}
This isn't possible in the general sense, because:
the $ref might be recursive (i.e. reference itself again)
the keywords in the $ref might duplicate some of the keywords in the containing schema, which would cause some logic to be overwritten.
Why do you need to alter the schema in this way? Generally, a JSON Schema implementation will resolve the $refs automatically while evaluating the schema against provided data.

JSON Schema validator with an array of specific objects (different types)

I have the following JSON data that I would like to validate.
[
{ "fieldType": "oneThing" },
{ "fieldType": "anotherThing" },
{ "fieldType": "oneThing" }
]
And my current (non working) schema is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"oneOf": [
{ "$ref": "#/definitions/oneThing" },
{ "$ref": "#/definitions/anotherThing" }
]
},
"definitions": {
"oneThing": {
"type": "object",
"properties": {
"fieldType": {
"type": "string",
"pattern": "oneThing"
}
},
"required": [
"fieldType"
]
},
"anotherThing": {
"type": "object",
"properties": {
"fieldType": {
"type": "string",
"pattern": "anotherThing"
}
},
"required": [
"fieldType"
]
}
}
}
I'm getting the following error but I fail to see what I'm doing wrong.
[] Object value found, but an array is required
More context: I'm generating a dynamic HTML form based on a JSON configuration. The HTML form will have a specific set of valid field types and the same field type may exist multiple times in the config, thus oneThing appearing more than once in the above sample json.
As it turns out, this had nothing to do with my JSON schema but with how I was calling the library that was parsing the schema.
I'm using https://github.com/justinrainbow/json-schema and was passing the wrong data type to the class. Duh!

Required fields for properties within an array of objects schema definition

I have following schema definition. library is required. there can be many. so in that library object there are some other properties which is required. But though i use following code it is not validating. I mean book title is not required. title of the author is required.How can i fix this?
schema definition
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["library"],
"properties": {
"library": {
"type": "array",
"items": [
{"required":["title"],
"type": "object",
"properties": {
"title": {
"type": "string"
},
"author": {
"type": "string"
}
}
}
]
}}}
json input
{"library":[]
}
expected json input
{"library":[{"title":"Max"}]
}
I assume you want "json input" to fail.
If so, your problem is that you didn't describe a library with a book with missing title, Instead, your document says 'I am a library with no books'.
Failing JSON:
{"library":[{}]}
Alternately, if you want to probibit empty libraries: JSON Schema has a minLength property.
Your question is a little difficult to understand, but I noticed that you are using the tuple form of the items keyword. I'm sure that is not what you want. Try the following schema and I think it will behave the way you expect.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["library"],
"properties": {
"library": {
"type": "array",
"items": {
"required": ["title"],
"type": "object",
"properties": {
"title": {
"type": "string"
},
"author": {
"type": "string"
}
}
}
}
}
}