I have this schema. It checks comments, and works fine at the moment.
var schema = {
id: '',
type: 'object',
additionalProperties: false,
properties: {
text: {
type: 'string',
minLength: 1,
required: true
},
author: {
type: 'number',
required: true
}
}
};
My comment structure is:
{
text: "Hello world!",
author: 1
}
But now, I need to validate an array of objects like this. So I can get something like:
[
{
text: "Hello world! Im comment #1",
author: 1
},
{
text: "Super awesome comment #2!",
author: 0
}
]
Sometimes I get one comment only so I get one object, and need to use first schema, but sometimes I get an array of comments, and my schema does not fit.
I heard about json schema anyOf
, but I dont know how to do it.
Some like:
anyOf
schema-1 (object)
schema-2 (array with objects)
Any help?
Thanks.
The type keyword is fundamental to JSON Schema. It specifies the data type for a schema. At its core, JSON Schema defines the following basic types: string. number.
In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.
A schema can reference another schema using the $ref keyword. The value of $ref is a URI-reference that is resolved against the schema's Base URI. When evaluating a $ref , an implementation uses the resolved identifier to retrieve the referenced schema and applies that schema to the instance.
$id is a reserved keyword. It serves for: Declaring an identifier for the schema or subschema. Declaring a base URL against which $ref URLs are resolved.
The solution is to have a common definition in one place, and then reference that common definition from two different options inside oneOf
:
Here, we put the simple object definition inside definitions
:
{
"definitions": {
"singleObject": {
... same definition as in your question ...
}
}
}
We then reference this schema, inside oneOf
:
{
"oneOf": [
{"$ref": "#/definitions/singleObject"}, // plain object
{
"type": "array", // array of plain objects
"items": {"$ref": "#/definitions/singleObject"}
}
],
"definitions": {
"singleObject": {...}
}
}
You can organise this a few different ways - I personally often end up with the simple-object definition as the root schema, and have the single/array switcher in definitions
, so the schema for my documents is actually http://example.com/schema#/definitions/arrayOrSingle
.
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