json-editor
json-editor copied to clipboard
Q: How to use anyOf / allOf?
General information
- json-editor version: latest from cdn.
Expected behavior
var schema = {
"properties": {
"chord": {
"description": "Font",
"properties": {
"name": { "type": "string" },
"file": { "type": "string" },
"description": { "type": "string" },
"size": { "type": "number" }
},
"allOf": [
{ "required": [ "description" ] },
{ "required": [ "name" ] },
{ "required": [ "file" ] }
]
}
}
};
var data = {
"chord" : {
"file" : "Helvetica.otf",
"name" : "Helvetica",
"description" : "Sans serif font",
"size" : 10
}
};
var editor = new JSONEditor(document.getElementById('editor_holder'), {
schema: schema,
no_additional_properties: true,
required_by_default: true,
disable_collapse: true,
show_opt_in: true,
});
editor.on('ready', function() {
editor.setValue(data);
});
Actual behavior

Expected behavior
var schema = {
"properties": {
"chord": {
"description": "Font",
"properties": {
"name": { "type": "string" },
"file": { "type": "string" },
"description": { "type": "string" },
"size": { "type": "number" }
},
"anyOf": [
{ "required": [ "description" ] },
{ "required": [ "name" ] },
{ "required": [ "file" ] }
]
}
}
};
var data = {
"chord" : {
"name" : "Helvetica",
"size" : 10
}
};
var editor = new JSONEditor(document.getElementById('editor_holder'), {
schema: schema,
no_additional_properties: true,
required_by_default: true,
disable_collapse: true,
show_opt_in: true,
});
editor.on('ready', function() {
editor.setValue(data);
});
Actual behavior

Note that the file input field is missing.
Could you please add a link to an example?
I've attached a zip with two standalone html files corresponding to the two cases. Does that help?
Sorry, I don't get what you are trying to achieve.
There are a few examples with allOf in the source:
https://github.com/json-editor/json-editor/search?q=allof
Let's concentrate on the second case, basic2.html.
The schema defines an item chord that has 4 properties, name, file, description and size.
The anyOf part of the scheme defines that at least one of name, file and description must be supplied.
The data has a chord item with name specified, and the data passes scheme validation (e.g. https://github.com/java-json-tools/json-schema-validator).
When the schema and data is passed to JSONEditor, it complains that "Value must validate against at least one of the provided schemas".

Possibly related: The name and size properties are not editable, and the editor for file is missing.
The data has a chord item with name specified, and the data passes scheme validation (e.g. https://github.com/java-json-tools/json-schema-validator).
When the schema and data is passed to JSONEditor, it complains that "Value must validate against at least one of the provided schemas".
But chord is missing file and description, that's why the editor complains. anyof is meant to define different types/structures. The part in your example anyOf > required should just be required with an array of propety-keys.
AFAIK it is perfectly valid to use anyOf for requirements.
See e.g. https://stackoverflow.com/questions/41172240/json-schema-how-to-make-anyof-two-or-more-properties-required
Is there an example in the spec? I just found: https://json-schema.org/understanding-json-schema/reference/combining.html#anyof
How about using definitions?
CC: @robocoder @germanbisurgi
I'm the first to admit I'm not a schema expert, but to the best of my knowledge anyOf creates schema alternatives. Data must match at least one of the alternatives. I'm not sure how definitions would help.
I thought anyOf definitions must contain all attributes. But in your example there's just required.
Maybe definitions is not the right thing to use. I thought it could help in specifying complete "anyOf"-schemas.
The allOf example works better if "additionalProperties": false is in the schema instead of using no_additional_properties: true in the editor options.
The anyOf example seems to show the sub-schemas being hoisted into the wrong level (i.e., acting on "chord")
Is there any progress on handling anyOf? The remark from @robocoder about "sub-schemas being hoisted into the wrong level" seems to confirm that the usage is okay, but there's a bug somewhere in the editor code.
@schmunk42 Please remove the needinfo label.