I'm struggling with the json validation against json schema and Rest Assured. The thing is that I need to use if else condition in it and for some reason my test are always green even I'm defining wrong condition. I used json-schema 7 which should support if else but when I'm defining false condition and my test should be failed with some error but its actually passed according to that I can say that its not checking that condition at all.
Here is my json:
{
"courses": [{
"position": "1",
"previous_course_rank": "",
"location": "Weston, FL",
"established": "",
"course_name": "Weston Hills - Tour Course",
"course_id": "056",
"holes": [{
"hole": "1"
}]
}]
}
Here is my json schema validation with the if else condition in it. As you can see from if else condition I'm expecting to have established empty string only with course_id: 056 but in other cases I'm expecting established to be year like 2022. The thing is when I'm trying to test false condition and putting in if year pattern [0-9{4} its green too and I can't understand why.
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"courses"
],
"properties": {
"courses": {
"type": "array",
"items": {
"type": "object",
"required": [
"position",
"previous_course_rank",
"location",
"established",
"course_name",
"course_id",
"holes"
],
"properties": {
"position": {
"type": "string"
},
"previous_course_rank": {
"type": "string"
},
"location": {
"type": "string"
},
"course_name": {
"type": "string"
},
"course_id": {
"type": "string"
},
"holes": {
"type": "array",
"items": {
"type": "object",
"required": [
"hole"
],
"properties": {
"hole": {
"type": "string"
}
}
}
}
},
"if": {
"properties": {
"course_id": {
"const": "056"
}
}
},
"then": {
"properties": {
"established": {
"pattern": "^$"
}
}
},
"else": {
"properties": {
"established": {
"pattern": "^[0-9]{4}$"
}
}
}
}
}
}
}
and here is my method of checking json against json schema
ValidatableResponse response = getResponse(feedUrl);
response
.body(matchesJsonSchemaInClasspath(pathToSchema))
.assertThat();
if: false, then: falseas your schema. That should fail validation. Otherwise, what you're using doesn't supportif/then/elsekeywords (found in draft-07 and up).