My problem is similar to this question: How to validate against an OpenAPI schema with $refs using json-schema-validator
Sample Yaml Schema in json:
{
"openapi": "3.0.0",
"info": {
"title": "API",
"description": "This API",
"version": "3.x"
},
"servers": [{
"url": "https://api.yourcompanyexample.com/oi",
"description": "Example server for your company"
}],
"security": [{
"bearerAuth": []
}],
"tags": [{
"name": "Orders"
}],
"components": {
"schemas": {
"Order": {
"required": [
"recipient",
],
"type": "object",
"properties": {
"recipient": {
"maxLength": 20,
"type": "string",
"description": "Recipient",
"example": "Joe"
}
"customerInformation": {
"$ref": "#/components/schemas/CustomerInformation"
}
}
},
"CustomerInformation": {
"type": "object",
"properties": {
"firstName": {
"maxLength": 25,
"type": "string",
"description": "Customer’s first name"
},
"emailAddress": {
"maxLength": 254,
"type": "string",
"description": "Email address of the customer"
}
}
}
}
}
}
Sample JSON String
{
"id": "DemoId",
"orderId": "DemoOrderId",
"customerInformation": {
"firstName": "John",
"emailAddress": [{
"emailName": "personal",
"emailAddress": "evensteven@personal.com"
}, {
"emailName": "work",
"emailAddress": "evensteven@work.com"
}]
}
}
ObjectMapper objectMapper = new ObjectMapper();
this.yamlReader = new ObjectMapper(new YAMLFactory());
JsonSchemaFactory this.factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4)).objectMapper(yamlReader).addMetaSchema(myJsonMetaSchema).build();
JsonSchema = this.jsonSchema = factory.getSchema(getClass().getClassLoader().getResourceAsStream("schema.yaml"));
JsonNode jsonNode = objectMapper.readTree(getClass().getClassLoader().getResourceAsStream("data.json"));
**Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
// I get no errors even though recipient is missing**
JsonSchema portionSchema = factory.getSchema(jsonSchema.getSchemaNode().get("components").get("schemas").get("Order"));
// I remove customer: $ref: '#/components/schemas/Customer' from the schema otherwise it complains about not finding it as it is not loaded in the portionSchema anymore
****Set<ValidationMessage> errors = portionSchema.validate(jsonNode);
//I get error message about the required field**
I have also tried loading the schema from "schemas" & "components" node as well. It does not give any error.
My expectation is:
JsonSchema portionSchema = factory.getSchema(jsonSchema.getSchemaNode().get("components"); ****Set errors = portionSchema.validate(jsonNode); it should give validation errors.