json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

Q: How to use anyOf / allOf?

Open sciurius opened this issue 4 years ago • 11 comments

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

basic1

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

basic2

Note that the file input field is missing.

sciurius avatar Apr 21 '21 10:04 sciurius

Could you please add a link to an example?

schmunk42 avatar Apr 21 '21 20:04 schmunk42

I've attached a zip with two standalone html files corresponding to the two cases. Does that help?

cases.zip

sciurius avatar Apr 21 '21 21:04 sciurius

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

schmunk42 avatar Apr 22 '21 10:04 schmunk42

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".

scrot20210422134218

Possibly related: The name and size properties are not editable, and the editor for file is missing.

sciurius avatar Apr 22 '21 11:04 sciurius

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.

schmunk42 avatar Apr 27 '21 06:04 schmunk42

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

sciurius avatar Apr 27 '21 07:04 sciurius

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

schmunk42 avatar Apr 27 '21 07:04 schmunk42

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.

sciurius avatar Apr 27 '21 09:04 sciurius

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.

schmunk42 avatar Apr 27 '21 09:04 schmunk42

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")

robocoder avatar Apr 27 '21 20:04 robocoder

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.

sciurius avatar Oct 14 '21 20:10 sciurius