-
Notifications
You must be signed in to change notification settings - Fork 43
Data against Schema validator - $ref, allOf/anyOf/oneOf, nullable $ref objects and nullable enums. #95
Comments
Hi and welcome, I'll try to answer all of this as best as I can: I hope those answers are helpful to you. If so, please close the issue by yourself. |
Thank you @llfbandit for your swift response! Unfortunately I don't get it quite how to use the library correctly.
properties:
maybeSafari:
allOf:
- $ref: "#/components/schemas/SafariPark"
nullable: true
To 1.:I started to use the other constructor as I had issues with the val objectValidator: SchemaValidator = new SchemaValidator("Animal", schema.get("components").get("schemas")) Which leads to an org.openapi4j.core.exception.ResolutionException: Reference '#/components/schemas/SafariPark' is unreachable in '/. But if just put my The following three examples accept this object with 1.1 Without "Some object with foo props" should "not be valid against the schema" in {
val schema: JsonNode = TreeUtil.json.readTree(new File("zoo.json"))
val data: JsonNode = TreeUtil.json.readTree(
"""
|{
| "foo": "bar"
|}
|""".stripMargin)
val objectValidator: SchemaValidator = new SchemaValidator("Animal", schema)
val validation = new ValidationResults();
objectValidator.validate(data, validation)
assert(validation.isValid === false)
} 1.2 Do I necessarily have to create a "Some object with foo props" should "not be valid against the schema with a ValidationContext from root" in {
val schema: JsonNode = TreeUtil.json.readTree(new File("zoo.json"))
val data: JsonNode = TreeUtil.json.readTree(
"""
|{
| "foo": "bar"
|}
|""".stripMargin)
val apiContext: OAI3Context = new OAI3Context(new URI("/"), schema)
val validationContext: ValidationContext[OAI3] = new ValidationContext(apiContext)
validationContext.setFastFail(false)
val objectValidator: SchemaValidator = new SchemaValidator(validationContext, "Animal", schema)
val validation = new ValidationResults();
objectValidator.validate(data, validation)
assert(validation.isValid === false)
} 1.3 And if I do so from which path do I have to put the schema into the "Some object with foo props" should
"not be valid against the schema with a ValidationContext from root and a SchemaValidator from path #/components/schemas" in {
val schema: JsonNode = TreeUtil.json.readTree(new File("zoo.json"))
val data: JsonNode = TreeUtil.json.readTree(
"""
|{
| "foo": "bar"
|}
|""".stripMargin)
val apiContext: OAI3Context = new OAI3Context(new URI("/"), schema)
val validationContext: ValidationContext[OAI3] = new ValidationContext(apiContext)
validationContext.setFastFail(false)
val objectValidator: SchemaValidator = new SchemaValidator(validationContext, "Animal", schema.get("components").get("schemas"))
val validation = new ValidationResults();
objectValidator.validate(data, validation)
assert(validation.isValid === false)
} |
Assuming you're now using 0.9-SNAPSHOT. The following pseudo-code should be enough to answer 1 & 2 (you need to now what schema you will validate data against): val apiContext: OAI3Context = new OAI3Context(your_doc_location_as_URL))
val validationContext: ValidationContext[OAI3] = new ValidationContext(apiContext)
val objectValidator: SchemaValidator = new SchemaValidator(validationContext, null, animalSchema) 3- Ok, did not see it. Need to check. 4- Sorry my answer was unclear. As far as I can tell, you are validating your data directly from |
3 is fixed now in latest 0.9-SNAPSHOT. |
Are you still experiencing difficulties to validate your data? For more clarification, when |
Thank you very much @llfbandit! Yes, this helped. I am sorry I was unavailable for some time. But I ran into an uninformative error message, for which I opened a new PR #105. |
Hey @llfbandit,
your library is great and helps a lot to validate data against a rather complex schema, but I encountered some problems, which I like to share with an example.
My example OpenAPI spec defines a
Zoo
which contains:$ref: "#/components/schemas/???"
. Which leads to aschemaParentNode
/parentSchema
problem.allOf
/anyOf
quantor for grouped objects, see:BigFive
. Which one is to pick, if I want to allow anyAnimal
within that group?anyOf
/oneOf
quantor for arrayitems
, see:SafariPark.animals
. Which one is to pick, if I want to allow any of the givenAnimals
by its group?nullable
referenced objects, as follows (see also Reference objects don't combine well with “nullable” #1368 , Clarify Semantics of nullable in OpenAPI 3.0):-->
nullable
enum
s do not work as expected:I have four problems:
Problem 1:
I guess with the upcoming changes in
0.9
I won't be able to instantiate the SchemaValidator with theschemaParentNode
andparentSchema
, as this constructor is not public anymore.I wrote a validation method in Scala like so:
This validates:
Problem 2:
If I try at purpose to break the schema, by changing
species
to a forbidden value, then I get a rather uninformative validation report like this:Problem 3:
nullable
referenced objects are not validated as nullables:Which give me the following validation error:
Problem 4:
This validates fine:
But this does not, as the validator seems to accept any value, but I expect the
enum
to forbid such values:This it the full
zoo.yaml
:The zoo.yaml as json (converted with the swagger-editor):
The text was updated successfully, but these errors were encountered: