Giter Club home page Giter Club logo

Comments (6)

llfbandit avatar llfbandit commented on June 22, 2024

Hi and welcome,

I'll try to answer all of this as best as I can:
1- You are setting the schema as the schema to validate and as its own parent. This is useless. You don't need the protected method, since data can be validated from a single root schema. Try it, it won't change the results.
2- Version 0.9 will report subvalidation items instead of this for all collection schemas (oneOf/anyOf/allOf). You can try it, this is already available.
3- If you set $ref, all siblings are ignored. You need to refactor your description to include it. By default, nullable is false.
4- I don't get this one. In your Animal schema, you defined subspecies as nullable. Your discriminator does not work. This only allowed when using oneOf, anyOf and allOf.

I hope those answers are helpful to you. If so, please close the issue by yourself.
Thanks.

from openapi4j.

swiesend avatar swiesend commented on June 22, 2024

Thank you @llfbandit for your swift response!

Unfortunately I don't get it quite how to use the library correctly.

  1. How do I have to initialize the SchemaValidator in order to resolve $ref: "#/components/schemas/*" correctly? To clarify my point I added some more examples .

  2. Yes, this is why I wanted to use the 0.9-SNAPSHOT, but failed because I do not initialize the SchemaValidator correctly.

  3. I think my spec description already includes nullable: true in the referenced SafariPark object, which is from type: object and sets nullable: true.
    Or would it be better to go for the allOf quantor and set the nullable: true in the parent of the $ref instead of inside the ref itself, like pointed out here:

           properties:
            maybeSafari:
              allOf:
                - $ref: "#/components/schemas/SafariPark"
              nullable: true
  1. I don't undestand why the discriminator does not work here as it is inherited by all Animal instances, which are just grouped by BigFive. If there are some errors in the logic of my schema I would appreciate if you can point them out a little more in detail, so that I can investigate such inconsistencies.

To 1.:

I started to use the other constructor as I had issues with the $ref: "#/components/schemas/*" references, which were not resolved properly when I just pass the schemas form path components/schemas as root.

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 zoo.json as root schema then the SchemaValidator validates against anything.

The following three examples accept this object with foo properties without complaining.

1.1 Without ValidationContext only SchemaValidator

"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 ValidationContext where the schema is the root?

"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 SchemaValidator?

"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)
  }

from openapi4j.

llfbandit avatar llfbandit commented on June 22, 2024

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 Animal "abstract" schema not from BigFive or a dedicated animal instance. This is why you don't get what you expect.

from openapi4j.

llfbandit avatar llfbandit commented on June 22, 2024

3 is fixed now in latest 0.9-SNAPSHOT.

from openapi4j.

llfbandit avatar llfbandit commented on June 22, 2024

Are you still experiencing difficulties to validate your data?

For more clarification, when OAI3Context is created JSON references are registered and so known when you validate your data. Obviously, you should not re-create it each time you need validation.

from openapi4j.

swiesend avatar swiesend commented on June 22, 2024

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.

from openapi4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.