json-editor: errors not work in anyOf
General information
- json-editor version: 1.4 & 2.0 /* all */
Expected behavior
when error occur the editor can show error message.
Actual behavior
It doesn’t show error message in anyOf array.
Steps to reproduce the behavior
Direct link to example: https://is.gd/FM9u4L
labelComponent
doesn’t work.
{
"properties": {
"id": {
"type": "string",
"pattern": "^[a-zA-Z0-9_]+$",
"minLength": 1,
"maxLength": 50
},
"children": {
"items": {
"type": "object",
"anyOf": [
{
"$ref": "#/definitions/labelComponent"
}
]
},
"type": "array"
}
},
"type": "object",
"defaultProperties": [
"id",
"children"
],
"definitions": {
"labelComponent": {
"type": "object",
"properties": {
"id": {
"type": "string",
"pattern": "^[a-zA-Z0-9_]+$",
"minLength": 1,
"maxLength": 50
}
},
"defaultProperties": [
"id"
]
}
}
}
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 27 (10 by maintainers)
Commits related to this issue
- test case for page `array-anyof.html` (it does not work correctly: it should failed, because there is still a bug #459) — committed to wclssdn/json-editor by wclssdn 5 years ago
- test case for page `array-anyof.html` (it does not work correctly: it should failed, because there is still a bug #459) — committed to wclssdn/json-editor by wclssdn 5 years ago
I am probably missing something. If I open the original link https://is.gd/FM9u4L , it still does not work as expected: there is only “high-level” error “Value must validate against at least one of the provided schemas”, but no errors inside anyOf are displayed. The same behavior is in my local installation of this editor (by npm).
How can errors inside anyOf options be displayed?
@pmk65 Yes i think so.
It would be great if someone could create a page in
tests/pages
with the schema.@wclssdn Sorry, It looks like your #445 PR have only been merged into the master branch. I have asked @schmunk42 to add it to the develop/2.x branch too.
@wclssdn I have extended the validation function, so now the error object has a count (If larger than 1, then duplicate errors was found/removed.)
Example error object with errorcount
https://github.com/json-editor/json-editor/blob/009f8fe5aef2e90403b44730ba544107b1aa4b8d/src/validator.js#L670-L691
Then In the editor showValidationErrors function, we can add something like:
if (error.errorcount && error.errorcount > 1) error.message += ' (' + error.errorcount + ' errors)';
(I have added it tosrc/editors/object.js
in the develop/2.x branch) This would produce an error message like this: Value must not validate against the provided schema (2 errors)I have added this to the
_validateSchema
insrc/validators.js
to remove any duplicate error messages. (Any error object with samepath
,message
andproperty
will be filtered out.)The errors of
anyOf
is already checked insrc/validator.js#L90
. So the second loop is not necessary.I have added the switched if statement to the develop/2.x branch.
@wclssdn The
check
variable insrc/editors/multiple.js
, line 297 expands to root.children.0.anyOf[0], buterror.path
is root.children.0, so they will never match.Changing the if statement from;
if(error.path.substr(0,check.length)===check) {
To:if(error.path === check.substr(0,error.path.length)) {
Will match root.children.0 against root.children.0, so the match is valid.
BUT the fields data-schemapath is root.children.0.id, so the error is not placed under the field, but at the top. (So we need to figure out how to append the property name (in this case id, to the error path)
I think it needs to loop through the child editors in the compare function. That way you can get the property name. Here’s my test so far. Now the error is placed under the field. but there’s a problem if there more than 1 property. So it’s not entirely correct.
@wclssdn I noticed a similar problem in the
src/editors/multiselect.js
editor, where the errors wasn’t displayed on form. This is because the error for child elements have a number added to the end of the path. I.e.root.children.1
In the
src/editors/multiselect.js
editor, I solved that by creating a regex to match against the path (which allows the additional child number on the path) instead of a direct comparison. Like this: