jsonschema validation not returning error as expected?
up vote
2
down vote
favorite
I am using this schema, I was hoping for value-based conditional schema.
If app_name is "test" then property name should be required.
if app_name if "rest" then property ips should be required.
{
"type": "object",
"oneOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": True,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
]
}
I am using following code
formatted_data = {"app_name": "rest", "name": "test data"}
print jsonschema.exceptions.best_match(jsonschema.Draft4Validator(schema).iter_errors(formatted_data))
I got following validation error
'rest' is not one of ['test']
Failed validating 'enum' in schema[0]['properties']['app_name']:
{'enum': ['test']}
On instance['app_name']:
'rest'
I am not sure if schema itself is invalid or there is a problem with library if self.
I am using
python 2.7
jsonschema 2.6.0
python python-2.7 jsonschema
|
show 1 more comment
up vote
2
down vote
favorite
I am using this schema, I was hoping for value-based conditional schema.
If app_name is "test" then property name should be required.
if app_name if "rest" then property ips should be required.
{
"type": "object",
"oneOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": True,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
]
}
I am using following code
formatted_data = {"app_name": "rest", "name": "test data"}
print jsonschema.exceptions.best_match(jsonschema.Draft4Validator(schema).iter_errors(formatted_data))
I got following validation error
'rest' is not one of ['test']
Failed validating 'enum' in schema[0]['properties']['app_name']:
{'enum': ['test']}
On instance['app_name']:
'rest'
I am not sure if schema itself is invalid or there is a problem with library if self.
I am using
python 2.7
jsonschema 2.6.0
python python-2.7 jsonschema
The JSON Schema is valid. That error is correct. What error message are you expecting to see?
– Relequestual
Oct 31 at 9:37
As I was passing a value "rest" in app_name. I was expecting an error message "ips" field is required.
– Sachin Aryal
Nov 14 at 4:44
It looks like they are only reporting one error, as it failes both of theoneOf
conditions. As one of the questions points out, if you try jsonschemavalidator.net, it reports both errors. There is currently no standard for error / validation error reporting defined by JSON Schema, hence my question "what do you expect to see?". I know this probably isn't the answer you want to hear!
– Relequestual
Nov 19 at 9:30
1
I should not though, that we (JSON Schema team) recognise this as an issue, are are looking to define standard error output!
– Relequestual
Nov 19 at 9:31
jsonschemavalidator.net it reports both errors, I expected to seeRequired properties are missing from object: ips.
– Sachin Aryal
Nov 19 at 13:51
|
show 1 more comment
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am using this schema, I was hoping for value-based conditional schema.
If app_name is "test" then property name should be required.
if app_name if "rest" then property ips should be required.
{
"type": "object",
"oneOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": True,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
]
}
I am using following code
formatted_data = {"app_name": "rest", "name": "test data"}
print jsonschema.exceptions.best_match(jsonschema.Draft4Validator(schema).iter_errors(formatted_data))
I got following validation error
'rest' is not one of ['test']
Failed validating 'enum' in schema[0]['properties']['app_name']:
{'enum': ['test']}
On instance['app_name']:
'rest'
I am not sure if schema itself is invalid or there is a problem with library if self.
I am using
python 2.7
jsonschema 2.6.0
python python-2.7 jsonschema
I am using this schema, I was hoping for value-based conditional schema.
If app_name is "test" then property name should be required.
if app_name if "rest" then property ips should be required.
{
"type": "object",
"oneOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": True,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
]
}
I am using following code
formatted_data = {"app_name": "rest", "name": "test data"}
print jsonschema.exceptions.best_match(jsonschema.Draft4Validator(schema).iter_errors(formatted_data))
I got following validation error
'rest' is not one of ['test']
Failed validating 'enum' in schema[0]['properties']['app_name']:
{'enum': ['test']}
On instance['app_name']:
'rest'
I am not sure if schema itself is invalid or there is a problem with library if self.
I am using
python 2.7
jsonschema 2.6.0
python python-2.7 jsonschema
python python-2.7 jsonschema
edited Oct 31 at 7:59
Patrick Artner
19.3k51941
19.3k51941
asked Oct 31 at 3:30
Sachin Aryal
433620
433620
The JSON Schema is valid. That error is correct. What error message are you expecting to see?
– Relequestual
Oct 31 at 9:37
As I was passing a value "rest" in app_name. I was expecting an error message "ips" field is required.
– Sachin Aryal
Nov 14 at 4:44
It looks like they are only reporting one error, as it failes both of theoneOf
conditions. As one of the questions points out, if you try jsonschemavalidator.net, it reports both errors. There is currently no standard for error / validation error reporting defined by JSON Schema, hence my question "what do you expect to see?". I know this probably isn't the answer you want to hear!
– Relequestual
Nov 19 at 9:30
1
I should not though, that we (JSON Schema team) recognise this as an issue, are are looking to define standard error output!
– Relequestual
Nov 19 at 9:31
jsonschemavalidator.net it reports both errors, I expected to seeRequired properties are missing from object: ips.
– Sachin Aryal
Nov 19 at 13:51
|
show 1 more comment
The JSON Schema is valid. That error is correct. What error message are you expecting to see?
– Relequestual
Oct 31 at 9:37
As I was passing a value "rest" in app_name. I was expecting an error message "ips" field is required.
– Sachin Aryal
Nov 14 at 4:44
It looks like they are only reporting one error, as it failes both of theoneOf
conditions. As one of the questions points out, if you try jsonschemavalidator.net, it reports both errors. There is currently no standard for error / validation error reporting defined by JSON Schema, hence my question "what do you expect to see?". I know this probably isn't the answer you want to hear!
– Relequestual
Nov 19 at 9:30
1
I should not though, that we (JSON Schema team) recognise this as an issue, are are looking to define standard error output!
– Relequestual
Nov 19 at 9:31
jsonschemavalidator.net it reports both errors, I expected to seeRequired properties are missing from object: ips.
– Sachin Aryal
Nov 19 at 13:51
The JSON Schema is valid. That error is correct. What error message are you expecting to see?
– Relequestual
Oct 31 at 9:37
The JSON Schema is valid. That error is correct. What error message are you expecting to see?
– Relequestual
Oct 31 at 9:37
As I was passing a value "rest" in app_name. I was expecting an error message "ips" field is required.
– Sachin Aryal
Nov 14 at 4:44
As I was passing a value "rest" in app_name. I was expecting an error message "ips" field is required.
– Sachin Aryal
Nov 14 at 4:44
It looks like they are only reporting one error, as it failes both of the
oneOf
conditions. As one of the questions points out, if you try jsonschemavalidator.net, it reports both errors. There is currently no standard for error / validation error reporting defined by JSON Schema, hence my question "what do you expect to see?". I know this probably isn't the answer you want to hear!– Relequestual
Nov 19 at 9:30
It looks like they are only reporting one error, as it failes both of the
oneOf
conditions. As one of the questions points out, if you try jsonschemavalidator.net, it reports both errors. There is currently no standard for error / validation error reporting defined by JSON Schema, hence my question "what do you expect to see?". I know this probably isn't the answer you want to hear!– Relequestual
Nov 19 at 9:30
1
1
I should not though, that we (JSON Schema team) recognise this as an issue, are are looking to define standard error output!
– Relequestual
Nov 19 at 9:31
I should not though, that we (JSON Schema team) recognise this as an issue, are are looking to define standard error output!
– Relequestual
Nov 19 at 9:31
jsonschemavalidator.net it reports both errors, I expected to see
Required properties are missing from object: ips.
– Sachin Aryal
Nov 19 at 13:51
jsonschemavalidator.net it reports both errors, I expected to see
Required properties are missing from object: ips.
– Sachin Aryal
Nov 19 at 13:51
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
Ok, it seems there's a typo in schema. "True" instead of "true".
You've got:
"uniqueItems": True,
while, best to my knowledge, it should be (though it still may depend on schema validator implementation)
"uniqueItems": true,
(see: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf section 5 JSON Values and in general https://www.json.org/ - JSON Schema is a JSON document and it conforms to JSON standard)
I've run it through .net online JSON schema validator at https://www.jsonschemavalidator.net/ and it immediately pointed out possible error with schema as above.
After typo correction it seems to work perfectly as per your comment from 2018/Nov/16:
As I was passing a value "rest" in app_name. I was expecting an error
message "ips" field is required. – Sachin Aryal
Complete schema (please note the "examples" section - only last 2 examples will validate successfully against schema):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"anyOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
Could you correct item in schema and give it a try with your tooling and let us know the result?
On top of that:
If you pass for validation JSON object like:
{
"app_name" : "rest",
"name" : "qwerty"
},
against your schema, where "oneOf" is used - validator will/should run object against all schemas in "oneOf" array to make sure it matches exactly one of provided schemas. Thus errors for "oneOf/0/" schema are valid - "app_name" : "rest" doesn't validate against defined enum. The validator has no clue what you meant by providing specific JSON for validation against schema, so if "allOf" (logical XOR) condition is not satisfied, I'd expect errors from all schemas the JSON was run against within "oneOf" array (even if these seem to be false-positives for you).
If some error message is missing, you might want to consider checking with/reporting to lib authors your exact case.
Hope it helped.
UPDATE
So it seems you are another one chasing meaningful errors ;-) Yes, that can be a pain when using logical operators.
For simple case like above, you can use for draft-07 if-then-else approach, however there's a caveat to it - see REMARK.
Schema first (note how I replaced "anyOf" with two "if-then"):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"if": {
"properties": {
"app_name": {"enum": ["test"]}
},
},
"then" : { "required": ["name"] },
"if" : {
"properties": {
"app_name": {"enum": ["rest"]}
},
},
"then" : { "required": ["ips"]},
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
REMARK
Schema validator jsonschema.net tends to provide exact if-then-else errors in simple cases, when schema in "then" and "else" contains no nested "if-then" and consists of single statement/schema expression. However, whenever more complex case is structured, you may run into a general error message like: JSON does not match schema from 'then'. or JSON does not match schema from 'else'. without additional details (you'd need to check python output on your own). You can work around it to some point by properly shaping dependencies or sub-schemas, but with really complex schema, if you are after detailed error message, you may be facing the limit of validator implementation error messaging anyway. Please see also alternative schema 2 as an example in here: https://stackoverflow.com/a/53320222/2811843 (the whole point being to structure schema in a way, that if-then-else fails on a single keyword schema and rest of schema logic sits under other keywords)
Having said that, you may always check that approach with your tooling and if necessary file a report to your favourite lib authors about error messaging details for failed if-then-else schemas.
alternative with "dependencies"
Another alternative for your case is using "dependencies" keyword from draft-06, invert the initial logic and shape "definitions" nodes so it leads directly to unique error:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"dependencies" : {
"ips" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/rest"}
},
},
"name" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/test"}
},
}
},
"definitions" : {
"allowed-app_name-value" : {
"test" : {
"enum": ["test"]
},
"rest" : {
"enum": ["rest"]
}
}
},
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
yet it's still a naming workaround aimed to identify exact error in a human readable way. For instance, the jsonschema .net will feed you with JSON line numbers and messages like documented in here: https://www.newtonsoft.com/jsonschema/help/html/JTokenIsValidWithValidationErrors.htm
Each tool has it's own error messaging approach. Please check JSON Schema team on github, as there's some work on unifying output for JSON Schema validation in progress for next draft.
Whenever analysing errors programatically, you may want to pay attention to error indexes (schemas are usually nested in schemas) if they appear, on which line the error occured etc..
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting
– Sachin Aryal
Nov 20 at 7:52
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53075861%2fjsonschema-validation-not-returning-error-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Ok, it seems there's a typo in schema. "True" instead of "true".
You've got:
"uniqueItems": True,
while, best to my knowledge, it should be (though it still may depend on schema validator implementation)
"uniqueItems": true,
(see: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf section 5 JSON Values and in general https://www.json.org/ - JSON Schema is a JSON document and it conforms to JSON standard)
I've run it through .net online JSON schema validator at https://www.jsonschemavalidator.net/ and it immediately pointed out possible error with schema as above.
After typo correction it seems to work perfectly as per your comment from 2018/Nov/16:
As I was passing a value "rest" in app_name. I was expecting an error
message "ips" field is required. – Sachin Aryal
Complete schema (please note the "examples" section - only last 2 examples will validate successfully against schema):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"anyOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
Could you correct item in schema and give it a try with your tooling and let us know the result?
On top of that:
If you pass for validation JSON object like:
{
"app_name" : "rest",
"name" : "qwerty"
},
against your schema, where "oneOf" is used - validator will/should run object against all schemas in "oneOf" array to make sure it matches exactly one of provided schemas. Thus errors for "oneOf/0/" schema are valid - "app_name" : "rest" doesn't validate against defined enum. The validator has no clue what you meant by providing specific JSON for validation against schema, so if "allOf" (logical XOR) condition is not satisfied, I'd expect errors from all schemas the JSON was run against within "oneOf" array (even if these seem to be false-positives for you).
If some error message is missing, you might want to consider checking with/reporting to lib authors your exact case.
Hope it helped.
UPDATE
So it seems you are another one chasing meaningful errors ;-) Yes, that can be a pain when using logical operators.
For simple case like above, you can use for draft-07 if-then-else approach, however there's a caveat to it - see REMARK.
Schema first (note how I replaced "anyOf" with two "if-then"):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"if": {
"properties": {
"app_name": {"enum": ["test"]}
},
},
"then" : { "required": ["name"] },
"if" : {
"properties": {
"app_name": {"enum": ["rest"]}
},
},
"then" : { "required": ["ips"]},
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
REMARK
Schema validator jsonschema.net tends to provide exact if-then-else errors in simple cases, when schema in "then" and "else" contains no nested "if-then" and consists of single statement/schema expression. However, whenever more complex case is structured, you may run into a general error message like: JSON does not match schema from 'then'. or JSON does not match schema from 'else'. without additional details (you'd need to check python output on your own). You can work around it to some point by properly shaping dependencies or sub-schemas, but with really complex schema, if you are after detailed error message, you may be facing the limit of validator implementation error messaging anyway. Please see also alternative schema 2 as an example in here: https://stackoverflow.com/a/53320222/2811843 (the whole point being to structure schema in a way, that if-then-else fails on a single keyword schema and rest of schema logic sits under other keywords)
Having said that, you may always check that approach with your tooling and if necessary file a report to your favourite lib authors about error messaging details for failed if-then-else schemas.
alternative with "dependencies"
Another alternative for your case is using "dependencies" keyword from draft-06, invert the initial logic and shape "definitions" nodes so it leads directly to unique error:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"dependencies" : {
"ips" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/rest"}
},
},
"name" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/test"}
},
}
},
"definitions" : {
"allowed-app_name-value" : {
"test" : {
"enum": ["test"]
},
"rest" : {
"enum": ["rest"]
}
}
},
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
yet it's still a naming workaround aimed to identify exact error in a human readable way. For instance, the jsonschema .net will feed you with JSON line numbers and messages like documented in here: https://www.newtonsoft.com/jsonschema/help/html/JTokenIsValidWithValidationErrors.htm
Each tool has it's own error messaging approach. Please check JSON Schema team on github, as there's some work on unifying output for JSON Schema validation in progress for next draft.
Whenever analysing errors programatically, you may want to pay attention to error indexes (schemas are usually nested in schemas) if they appear, on which line the error occured etc..
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting
– Sachin Aryal
Nov 20 at 7:52
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
add a comment |
up vote
1
down vote
Ok, it seems there's a typo in schema. "True" instead of "true".
You've got:
"uniqueItems": True,
while, best to my knowledge, it should be (though it still may depend on schema validator implementation)
"uniqueItems": true,
(see: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf section 5 JSON Values and in general https://www.json.org/ - JSON Schema is a JSON document and it conforms to JSON standard)
I've run it through .net online JSON schema validator at https://www.jsonschemavalidator.net/ and it immediately pointed out possible error with schema as above.
After typo correction it seems to work perfectly as per your comment from 2018/Nov/16:
As I was passing a value "rest" in app_name. I was expecting an error
message "ips" field is required. – Sachin Aryal
Complete schema (please note the "examples" section - only last 2 examples will validate successfully against schema):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"anyOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
Could you correct item in schema and give it a try with your tooling and let us know the result?
On top of that:
If you pass for validation JSON object like:
{
"app_name" : "rest",
"name" : "qwerty"
},
against your schema, where "oneOf" is used - validator will/should run object against all schemas in "oneOf" array to make sure it matches exactly one of provided schemas. Thus errors for "oneOf/0/" schema are valid - "app_name" : "rest" doesn't validate against defined enum. The validator has no clue what you meant by providing specific JSON for validation against schema, so if "allOf" (logical XOR) condition is not satisfied, I'd expect errors from all schemas the JSON was run against within "oneOf" array (even if these seem to be false-positives for you).
If some error message is missing, you might want to consider checking with/reporting to lib authors your exact case.
Hope it helped.
UPDATE
So it seems you are another one chasing meaningful errors ;-) Yes, that can be a pain when using logical operators.
For simple case like above, you can use for draft-07 if-then-else approach, however there's a caveat to it - see REMARK.
Schema first (note how I replaced "anyOf" with two "if-then"):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"if": {
"properties": {
"app_name": {"enum": ["test"]}
},
},
"then" : { "required": ["name"] },
"if" : {
"properties": {
"app_name": {"enum": ["rest"]}
},
},
"then" : { "required": ["ips"]},
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
REMARK
Schema validator jsonschema.net tends to provide exact if-then-else errors in simple cases, when schema in "then" and "else" contains no nested "if-then" and consists of single statement/schema expression. However, whenever more complex case is structured, you may run into a general error message like: JSON does not match schema from 'then'. or JSON does not match schema from 'else'. without additional details (you'd need to check python output on your own). You can work around it to some point by properly shaping dependencies or sub-schemas, but with really complex schema, if you are after detailed error message, you may be facing the limit of validator implementation error messaging anyway. Please see also alternative schema 2 as an example in here: https://stackoverflow.com/a/53320222/2811843 (the whole point being to structure schema in a way, that if-then-else fails on a single keyword schema and rest of schema logic sits under other keywords)
Having said that, you may always check that approach with your tooling and if necessary file a report to your favourite lib authors about error messaging details for failed if-then-else schemas.
alternative with "dependencies"
Another alternative for your case is using "dependencies" keyword from draft-06, invert the initial logic and shape "definitions" nodes so it leads directly to unique error:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"dependencies" : {
"ips" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/rest"}
},
},
"name" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/test"}
},
}
},
"definitions" : {
"allowed-app_name-value" : {
"test" : {
"enum": ["test"]
},
"rest" : {
"enum": ["rest"]
}
}
},
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
yet it's still a naming workaround aimed to identify exact error in a human readable way. For instance, the jsonschema .net will feed you with JSON line numbers and messages like documented in here: https://www.newtonsoft.com/jsonschema/help/html/JTokenIsValidWithValidationErrors.htm
Each tool has it's own error messaging approach. Please check JSON Schema team on github, as there's some work on unifying output for JSON Schema validation in progress for next draft.
Whenever analysing errors programatically, you may want to pay attention to error indexes (schemas are usually nested in schemas) if they appear, on which line the error occured etc..
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting
– Sachin Aryal
Nov 20 at 7:52
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
add a comment |
up vote
1
down vote
up vote
1
down vote
Ok, it seems there's a typo in schema. "True" instead of "true".
You've got:
"uniqueItems": True,
while, best to my knowledge, it should be (though it still may depend on schema validator implementation)
"uniqueItems": true,
(see: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf section 5 JSON Values and in general https://www.json.org/ - JSON Schema is a JSON document and it conforms to JSON standard)
I've run it through .net online JSON schema validator at https://www.jsonschemavalidator.net/ and it immediately pointed out possible error with schema as above.
After typo correction it seems to work perfectly as per your comment from 2018/Nov/16:
As I was passing a value "rest" in app_name. I was expecting an error
message "ips" field is required. – Sachin Aryal
Complete schema (please note the "examples" section - only last 2 examples will validate successfully against schema):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"anyOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
Could you correct item in schema and give it a try with your tooling and let us know the result?
On top of that:
If you pass for validation JSON object like:
{
"app_name" : "rest",
"name" : "qwerty"
},
against your schema, where "oneOf" is used - validator will/should run object against all schemas in "oneOf" array to make sure it matches exactly one of provided schemas. Thus errors for "oneOf/0/" schema are valid - "app_name" : "rest" doesn't validate against defined enum. The validator has no clue what you meant by providing specific JSON for validation against schema, so if "allOf" (logical XOR) condition is not satisfied, I'd expect errors from all schemas the JSON was run against within "oneOf" array (even if these seem to be false-positives for you).
If some error message is missing, you might want to consider checking with/reporting to lib authors your exact case.
Hope it helped.
UPDATE
So it seems you are another one chasing meaningful errors ;-) Yes, that can be a pain when using logical operators.
For simple case like above, you can use for draft-07 if-then-else approach, however there's a caveat to it - see REMARK.
Schema first (note how I replaced "anyOf" with two "if-then"):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"if": {
"properties": {
"app_name": {"enum": ["test"]}
},
},
"then" : { "required": ["name"] },
"if" : {
"properties": {
"app_name": {"enum": ["rest"]}
},
},
"then" : { "required": ["ips"]},
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
REMARK
Schema validator jsonschema.net tends to provide exact if-then-else errors in simple cases, when schema in "then" and "else" contains no nested "if-then" and consists of single statement/schema expression. However, whenever more complex case is structured, you may run into a general error message like: JSON does not match schema from 'then'. or JSON does not match schema from 'else'. without additional details (you'd need to check python output on your own). You can work around it to some point by properly shaping dependencies or sub-schemas, but with really complex schema, if you are after detailed error message, you may be facing the limit of validator implementation error messaging anyway. Please see also alternative schema 2 as an example in here: https://stackoverflow.com/a/53320222/2811843 (the whole point being to structure schema in a way, that if-then-else fails on a single keyword schema and rest of schema logic sits under other keywords)
Having said that, you may always check that approach with your tooling and if necessary file a report to your favourite lib authors about error messaging details for failed if-then-else schemas.
alternative with "dependencies"
Another alternative for your case is using "dependencies" keyword from draft-06, invert the initial logic and shape "definitions" nodes so it leads directly to unique error:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"dependencies" : {
"ips" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/rest"}
},
},
"name" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/test"}
},
}
},
"definitions" : {
"allowed-app_name-value" : {
"test" : {
"enum": ["test"]
},
"rest" : {
"enum": ["rest"]
}
}
},
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
yet it's still a naming workaround aimed to identify exact error in a human readable way. For instance, the jsonschema .net will feed you with JSON line numbers and messages like documented in here: https://www.newtonsoft.com/jsonschema/help/html/JTokenIsValidWithValidationErrors.htm
Each tool has it's own error messaging approach. Please check JSON Schema team on github, as there's some work on unifying output for JSON Schema validation in progress for next draft.
Whenever analysing errors programatically, you may want to pay attention to error indexes (schemas are usually nested in schemas) if they appear, on which line the error occured etc..
Ok, it seems there's a typo in schema. "True" instead of "true".
You've got:
"uniqueItems": True,
while, best to my knowledge, it should be (though it still may depend on schema validator implementation)
"uniqueItems": true,
(see: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf section 5 JSON Values and in general https://www.json.org/ - JSON Schema is a JSON document and it conforms to JSON standard)
I've run it through .net online JSON schema validator at https://www.jsonschemavalidator.net/ and it immediately pointed out possible error with schema as above.
After typo correction it seems to work perfectly as per your comment from 2018/Nov/16:
As I was passing a value "rest" in app_name. I was expecting an error
message "ips" field is required. – Sachin Aryal
Complete schema (please note the "examples" section - only last 2 examples will validate successfully against schema):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"anyOf": [
{
"properties": {
"app_name": {"enum": ["test"]}
},
"required": ["name"]
},
{
"properties": {
"app_name": {"enum": ["rest"]}
},
"required": ["ips"]
},
],
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
Could you correct item in schema and give it a try with your tooling and let us know the result?
On top of that:
If you pass for validation JSON object like:
{
"app_name" : "rest",
"name" : "qwerty"
},
against your schema, where "oneOf" is used - validator will/should run object against all schemas in "oneOf" array to make sure it matches exactly one of provided schemas. Thus errors for "oneOf/0/" schema are valid - "app_name" : "rest" doesn't validate against defined enum. The validator has no clue what you meant by providing specific JSON for validation against schema, so if "allOf" (logical XOR) condition is not satisfied, I'd expect errors from all schemas the JSON was run against within "oneOf" array (even if these seem to be false-positives for you).
If some error message is missing, you might want to consider checking with/reporting to lib authors your exact case.
Hope it helped.
UPDATE
So it seems you are another one chasing meaningful errors ;-) Yes, that can be a pain when using logical operators.
For simple case like above, you can use for draft-07 if-then-else approach, however there's a caveat to it - see REMARK.
Schema first (note how I replaced "anyOf" with two "if-then"):
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"if": {
"properties": {
"app_name": {"enum": ["test"]}
},
},
"then" : { "required": ["name"] },
"if" : {
"properties": {
"app_name": {"enum": ["rest"]}
},
},
"then" : { "required": ["ips"]},
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
REMARK
Schema validator jsonschema.net tends to provide exact if-then-else errors in simple cases, when schema in "then" and "else" contains no nested "if-then" and consists of single statement/schema expression. However, whenever more complex case is structured, you may run into a general error message like: JSON does not match schema from 'then'. or JSON does not match schema from 'else'. without additional details (you'd need to check python output on your own). You can work around it to some point by properly shaping dependencies or sub-schemas, but with really complex schema, if you are after detailed error message, you may be facing the limit of validator implementation error messaging anyway. Please see also alternative schema 2 as an example in here: https://stackoverflow.com/a/53320222/2811843 (the whole point being to structure schema in a way, that if-then-else fails on a single keyword schema and rest of schema logic sits under other keywords)
Having said that, you may always check that approach with your tooling and if necessary file a report to your favourite lib authors about error messaging details for failed if-then-else schemas.
alternative with "dependencies"
Another alternative for your case is using "dependencies" keyword from draft-06, invert the initial logic and shape "definitions" nodes so it leads directly to unique error:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"ips": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "string",
"pattern": "[^ ]",
"minLength": 1,
"maxLength": 50
}
},
"app_name": {
"type": "string",
"minLength": 1,
"maxLength": 10,
"enum": [
"test",
"rest"
]
}
},
"required": [
"app_name"
],
"dependencies" : {
"ips" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/rest"}
},
},
"name" : {
"properties": {
"app_name": {"$ref":"#/definitions/allowed-app_name-value/test"}
},
}
},
"definitions" : {
"allowed-app_name-value" : {
"test" : {
"enum": ["test"]
},
"rest" : {
"enum": ["rest"]
}
}
},
"examples" : [
{
"app_name" : "rest",
},
{
"app_name" : "test",
},
{
"app_name" : "test",
"ips" : [
"something1",
"something2"
]
},
{
"app_name" : "rest",
"name" : "qwerty"
},
{
"app_name" : "test",
"name" : "qwerty"
},
{
"app_name" : "rest",
"ips" : [
"something1",
"something2"
]
}
]
}
yet it's still a naming workaround aimed to identify exact error in a human readable way. For instance, the jsonschema .net will feed you with JSON line numbers and messages like documented in here: https://www.newtonsoft.com/jsonschema/help/html/JTokenIsValidWithValidationErrors.htm
Each tool has it's own error messaging approach. Please check JSON Schema team on github, as there's some work on unifying output for JSON Schema validation in progress for next draft.
Whenever analysing errors programatically, you may want to pay attention to error indexes (schemas are usually nested in schemas) if they appear, on which line the error occured etc..
edited Nov 20 at 11:06
answered Nov 15 at 23:49
PsychoFish
81199
81199
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting
– Sachin Aryal
Nov 20 at 7:52
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
add a comment |
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting
– Sachin Aryal
Nov 20 at 7:52
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data
{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting– Sachin Aryal
Nov 20 at 7:52
It is not a typo mistake (I am using python Dictionary), If I try with jsonschemavalidator.net , It gives me the expected error message. For example, if I pass the following data
{ "app_name" : "rest", "name" : "qwerty" } I got this error
{'app_name': 'rest', 'name': 'qwerty'} is not valid under any of the given schemas` in python. When i try this with jsonschemavalidator.net , i got 3 error message. May be there is problem with error reporting– Sachin Aryal
Nov 20 at 7:52
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
@SachinAryal multiple error messages are expected. As I mentioned, logical operators force validator to run (in case JSON doc doesn't match schema) through all schemas in *Of statement. Error messaging depends on implementation, but there's usually a point, where lib/tool logic simply doesn't know what did you mean and delivers all errors found in all schemas, as pointed out above. I am updating the answer with usage of if-then-else (not a fan of it, but for simple cases it may do the trick - please note REMARK section)
– PsychoFish
Nov 20 at 8:14
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53075861%2fjsonschema-validation-not-returning-error-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The JSON Schema is valid. That error is correct. What error message are you expecting to see?
– Relequestual
Oct 31 at 9:37
As I was passing a value "rest" in app_name. I was expecting an error message "ips" field is required.
– Sachin Aryal
Nov 14 at 4:44
It looks like they are only reporting one error, as it failes both of the
oneOf
conditions. As one of the questions points out, if you try jsonschemavalidator.net, it reports both errors. There is currently no standard for error / validation error reporting defined by JSON Schema, hence my question "what do you expect to see?". I know this probably isn't the answer you want to hear!– Relequestual
Nov 19 at 9:30
1
I should not though, that we (JSON Schema team) recognise this as an issue, are are looking to define standard error output!
– Relequestual
Nov 19 at 9:31
jsonschemavalidator.net it reports both errors, I expected to see
Required properties are missing from object: ips.
– Sachin Aryal
Nov 19 at 13:51