Skip to content

Commit

Permalink
Validate schema against meta-schema on all drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschmidt committed Jul 27, 2023
1 parent fb5bd54 commit bbfa1b3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Add the project to your Mix dependencies in `mix.exs`:
```elixir
defp deps do
[
{:ex_json_schema, "~> 0.9.3"}
{:ex_json_schema, "~> 0.10.0"}
]
end
```
Expand Down
19 changes: 6 additions & 13 deletions lib/ex_json_schema/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ defmodule ExJsonSchema.Schema do

@spec assert_valid_schema(map) :: :ok | {:error, Validator.errors()}
defp assert_valid_schema(schema) do
with false <- meta04?(schema),
false <- meta06?(schema),
false <- meta07?(schema) do
if meta_schema?(schema) do
:ok
else
schema_module =
schema
|> Map.get("$schema", @current_draft_schema_url <> "#")
Expand All @@ -160,8 +160,6 @@ defmodule ExJsonSchema.Schema do
schema_module.schema()
|> resolve()
|> ExJsonSchema.Validator.validate(schema, error_formatter: false)
else
_ -> :ok
end
end

Expand Down Expand Up @@ -372,14 +370,9 @@ defmodule ExJsonSchema.Schema do
end)
end

defp meta04?(%{"$schema" => @draft4_schema_url <> _}), do: true
defp meta04?(_), do: false

defp meta06?(%{"$schema" => @draft6_schema_url <> _}), do: true
defp meta06?(_), do: false

defp meta07?(%{"$schema" => @draft7_schema_url <> _}), do: true
defp meta07?(_), do: false
defp meta_schema?(%{"id" => "http://json-schema.org/" <> _}), do: true
defp meta_schema?(%{"$id" => "http://json-schema.org/" <> _}), do: true
defp meta_schema?(_), do: false

defp do_get_fragment(nil, _, _ref), do: {:error, :invalid_reference}
defp do_get_fragment(schema, [], _), do: {:ok, schema}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule ExJsonSchema.Mixfile do
use Mix.Project

@source_url "https://github.com/jonasschmidt/ex_json_schema"
@version "0.9.3"
@version "0.10.0"

def project do
[
Expand Down
9 changes: 9 additions & 0 deletions test/ex_json_schema/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ defmodule ExJsonSchema.SchemaTest do
assert_raise ExJsonSchema.Schema.InvalidSchemaError,
~s(schema did not pass validation against its meta-schema: [%ExJsonSchema.Validator.Error{error: %ExJsonSchema.Validator.Error.Type{expected: ["object"], actual: "string"}, path: "#/properties"}]),
fn -> resolve(schema) end

assert_raise ExJsonSchema.Schema.InvalidSchemaError,
fn -> resolve(schema |> Map.put("$schema", "http://json-schema.org/draft-04/schema")) end

assert_raise ExJsonSchema.Schema.InvalidSchemaError,
fn -> resolve(schema |> Map.put("$schema", "http://json-schema.org/draft-06/schema")) end

assert_raise ExJsonSchema.Schema.InvalidSchemaError,
fn -> resolve(schema |> Map.put("$schema", "http://json-schema.org/draft-07/schema")) end
end

test "resolving an absolute reference in a scoped schema" do
Expand Down

0 comments on commit bbfa1b3

Please sign in to comment.