I’m trying to do json schema validation in spring boot 3.3.3, in resources folder I have put the schemas main.json, sub1.json and sub2.json. I’m using $ref to refer other schema objects from main schema and I want to set a field condition in sub2 based on a value of property from sub1, but I’m getting required field missing always when validating even when value is different. Added expected and obtained result at the end with same results.
Dependency used for schema validation:
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.4.0</version>
</dependency>
Bean to load schemas:
@Bean
public JsonSchema jsonSchema() {
return JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012)
.getSchema(getClass().getClassLoader().getResourceAsStream(“/main.json”));
}
Controller method:
@Autowired
private JsonSchema jsonSchema;
@Autowired
private ObjectMapper objectMapper;
@PostMapping("/validate")
public void validate(@RequestBody RequestDto request) {
JsonNode jsonNode = objectMapper.valueToTree(request);
Set<ValidationMessage> validationMessages = jsonSchema.validate(jsonNode);
if (!validationMessages.isEmpty()) {
throw new JsonSchemaValidationException(validationMessages);
}
log.info("Validation Passed");
}
main.json:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "ResponsePayload",
"description": "Payload object response",
"type": "object",
"properties": {
"objectA": {
"$ref": "classpath:/sub1.json"
},
"objectB": {
"$ref": "classpath:/sub2.json"
},
"objectC": {
"$ref": "classpath:/sub3.json"
}
},
"required": [
"objectA",
"objectB"
]
}
sub1.json:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Sub1Payload",
"description": "Sub payload 1",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"number": {
"type": "integer"
},
"type-emp-land": {
"type": "object",
"properties": {
"location": {
"type": "string"
},
"area": {
"type": "string"
}
}
}
},
"required": [
"name",
"number",
"type-emp-land"
]
}
sub2.json:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Sub2Payload",
"description": "Sub payload 2",
"type": "object",
"properties": {
"location": {
"type": "string"
},
"area": {
"type": "string"
},
"radius": {
"type": "object",
"properties": {
"cm": {
"type": "integer"
},
"km": {
"type": "integer"
}
}
},
"type-emp-land-location": {
"$ref": "classpath:/sub1.json#/properties/type-emp-land/properties/location"
}
},
"required": [
"location",
"area"
],
"allOf": [
{
"if": {
"properties": {
"type-emp-land-location": {
"const": "InsideNothingHall"
}
}
},
"then": {
"required": [
"radius"
]
}
}
]
}
Input 1:
{
"objectA": {
"name": "Wired",
"number": 1234,
"type-emp-land": {
"location": "InsideNothingHall",
"area": "Inside square"
}
},
"objectB": {
"location": "Mid",
"area": "50 cm"
}
}
Expected: JsonSchemaValidationException with required field ‘radius’ missing message
Output: JsonSchemaValidationException with required field ‘radius’ missing message
Input 2:
{
"objectA": {
"name": "Wired",
"number": 1234,
"type-emp-land": {
"location": "OutTown",
"area": "Inside square"
}
},
"objectB": {
"location": "Mid",
"area": "50 cm"
}
}
Expected: Validation Passed – log
Output: JsonSchemaValidationException with required field ‘radius’ missing message
For both the cases I’m getting the same exception but in case of input2 no exception should occur. I’m new to json schema validation, what am I missing here?
Arun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.