open-api-spex / open_api_spex

Open API Specifications for Elixir Plug applications
Mozilla Public License 2.0
722 stars 186 forks source link

Validate schemas using ex_json_schema #47

Open fenollp opened 6 years ago

fenollp commented 6 years ago

Following on #23 (and my attempt at solving it: #42), I have been instead using the really good ex_json_schema.

Here is the code I am using:

    case %{
           "components" => components,
           "additionalProperties" => false,
           "type" => "object",
           "required" => [schema_name],
           "properties" => %{
             schema_name => %{"$ref" => "#/components/schemas/#{schema_name}"}
           }
         }
         |> ExJsonSchema.Schema.resolve()
         |> ExJsonSchema.Validator.validate(resp)

There needs to be a translation step first though, to rewrite nullable: true and such incompatibilities of OpenAPIV3 schemas with JSON Schema Draft-04.

Would you be open to use ex_json_schema validators instead of the current ones in this lib? Where do you think this translation step should be: within this lib or within ex_json_schema?

Thanks

mbuhot commented 6 years ago

I'd be OK with using ex_json_schema for validations. As you say, it just needs to be possible to map swagger schemas to json schema.

It would be great if there was a mode: swagger option in ex_json_schema, but if you want to get something working immediately we'd need to map it in open_api_spex.

fenollp commented 6 years ago

The people over at OpenAPI and at JSON Schema want to converge their schema specs eventually. To me it makes more sense to do a conversion to (and from?) JSON Schema before validation.

In which module do you think this conversion step should go?

fenollp commented 5 years ago

@moxley why is this labeled as wontfix? Thanks.

moxley commented 5 years ago

@fenollp: The Won't Fix label is only tentative, but requires further discussion to be sure. From what I understand, JSON Schema isn't compatible with Open API. Open API borrows much from JSON Schema, but it extends and modifies it. Here are some of the details of the differences: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schema-object. I don't think it's feasible to swap in a JSON Schema library.

By the way, we set up a private channel in the Elixir Slack for discussions. Would you be willing to join us there? You've already been added.

mbuhot commented 4 years ago

Looks like this can be achieved when OpenApi 3.1 is released. OpenAPI schemas will be a proper superset of the latest json schema draft 🎉

https://github.com/OAI/OpenAPI-Specification/pull/1977

Stratus3D commented 1 month ago

Any chance this library will be updated to OpenAPI 3.1?