jonasschmidt / ex_json_schema

An Elixir JSON Schema validator
MIT License
366 stars 98 forks source link

How to validate a schema itself? #86

Closed lud-wj closed 1 year ago

lud-wj commented 1 year ago

The following code passes without problem:

    ExJsonSchema.Schema.resolve(
      Jason.decode!("""
      {
         "$schema": "http://json-schema.org/draft-07/schema#",
         "properties": {
           "a": {
             "type": "string"
           }
         },
         "required": "not an array"
      }
      """)
    )

But I guess it should not.

I was trying to debug this, but saw that you have this code :

    with false <- meta04?(schema),
         false <- meta06?(schema),
         false <- meta07?(schema) do
      #...
      |> ExJsonSchema.Validator.validate(schema, error_formatter: false)
    else
      _ -> :ok
    end

It specifically prevent validation of schemas with a known meta-schema.

But as far as I understand you cannot have a false in the with because the following code will throw:

    schema_version =
      root_schema
      |> Map.get("$schema", @current_draft_schema_url <> "#")
      |> schema_version!()

(I know I could use the "latest" URL but that does not work for me. I want a defined spec).

What do you think?

jonasschmidt commented 1 year ago

Oh, yeah, that doesn't look right at all. And that logic has been there a while, which means schema validation is probably not tested for all drafts but the current one. Let me have a look, it doesn't look like it would be a difficult fix.

lud-wj commented 1 year ago

Thanks!

jonasschmidt commented 1 year ago

Ok, fixed (and tested now). Thank you for reporting the bug and pinpointing the faulty implementation, that saved me some time 🙇 It's on hex.pm as 0.10.1 now: https://hex.pm/packages/ex_json_schema/0.10.1

lud-wj commented 1 year ago

Great, thank you! :racing_car: