Skip to content

Commit

Permalink
Raise an exception when trying to resolve a remote schema without a r…
Browse files Browse the repository at this point in the history
…emote schema resolver
  • Loading branch information
jonasschmidt committed Dec 16, 2016
1 parent 0afaa77 commit f803562
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -10,7 +10,7 @@ Add the project to your Mix dependencies in `mix.exs`:

```elixir
defp deps do
[{:ex_json_schema, "~> 0.5.2"}]
[{:ex_json_schema, "~> 0.5.3"}]
end
```

Expand Down
6 changes: 5 additions & 1 deletion lib/ex_json_schema/schema.ex
Expand Up @@ -7,6 +7,10 @@ defmodule ExJsonSchema.Schema do
defexception message: "invalid schema"
end

defmodule UndefinedRemoteSchemaResolverError do
defexception message: "trying to resolve a remote schema but no remote schema resolver function is defined"
end

alias ExJsonSchema.Schema.Draft4
alias ExJsonSchema.Schema.Root

Expand Down Expand Up @@ -162,7 +166,7 @@ defmodule ExJsonSchema.Schema do
end

defp remote_schema_resolver do
Application.get_env(:ex_json_schema, :remote_schema_resolver)
Application.get_env(:ex_json_schema, :remote_schema_resolver) || fn _url -> raise UndefinedRemoteSchemaResolverError end
end

defp assert_reference_valid(path, root, _ref) do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Expand Up @@ -4,7 +4,7 @@ defmodule ExJsonSchema.Mixfile do
def project do
[
app: :ex_json_schema,
version: "0.5.2",
version: "0.5.3",
elixir: "~> 1.0",
description: "A JSON Schema validator with full support for the draft 4 specification and zero dependencies.",
deps: deps,
Expand Down
13 changes: 13 additions & 0 deletions test/ex_json_schema/config_test.exs
@@ -0,0 +1,13 @@
defmodule ExJsonSchema.ConfigTest do
use ExUnit.Case

import ExJsonSchema.Schema, only: [resolve: 1]

test "raising an exception when trying to resolve a remote schema and no remote schema resolver is defined" do
resolver = Application.get_env(:ex_json_schema, :remote_schema_resolver)
Application.put_env(:ex_json_schema, :remote_schema_resolver, nil)
schema = %{"$ref" => "http://somewhere/schema.json"}
assert_raise ExJsonSchema.Schema.UndefinedRemoteSchemaResolverError, fn -> resolve(schema) end
Application.put_env(:ex_json_schema, :remote_schema_resolver, resolver)
end
end

0 comments on commit f803562

Please sign in to comment.