Skip to content

Commit

Permalink
Merge pull request #174 from hrzndhrn/issue/173
Browse files Browse the repository at this point in the history
Fix issue #173
  • Loading branch information
NickNeck committed Jul 27, 2023
2 parents 958fb6c + e127ca8 commit 5a09671
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 295 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
@@ -1,6 +1,10 @@
# Changelog

## 0.17.0
## 0.17.1 - 2023/07/28

+ Fix the ref inliner.

## 0.17.0 - 2022/09/19

+ Require Elixir 1.8.0
+ Update inspect implementations for `Xema.Ref` and `Xema.Schema`.
Expand Down
7 changes: 4 additions & 3 deletions lib/xema/ref.ex
Expand Up @@ -111,12 +111,13 @@ defmodule Xema.Ref do

defp fetch_by_key!("#", _master, root), do: {root, root}

defp fetch_by_key!(key, master, nil),
do: {Map.fetch!(master.refs, key), master}
defp fetch_by_key!(key, master, nil) do
{Map.fetch!(master.refs, key), master}
end

defp fetch_by_key!(key, master, root) do
case Map.get(root.refs, key) do
nil -> {Map.fetch!(master.refs, key), master}
nil -> {Map.fetch!(master.refs, key), root}
schema -> {schema, root}
end
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Expand Up @@ -4,7 +4,7 @@ defmodule Xema.Mixfile do
def project do
[
app: :xema,
version: "0.17.0",
version: "0.17.1",
elixir: "~> 1.8",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down Expand Up @@ -35,7 +35,7 @@ defmodule Xema.Mixfile do
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.travis": :test,
"coveralls.github": :test,
"coveralls.html": :test,
"gen.test_suite": :test
]
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Expand Up @@ -15,9 +15,9 @@
"ex_doc": {:hex, :ex_doc, "0.30.3", "bfca4d340e3b95f2eb26e72e4890da83e2b3a5c5b0e52607333bf5017284b063", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "fbc8702046c1d25edf79de376297e608ac78cdc3a29f075484773ad1718918b6"},
"excoveralls": {:hex, :excoveralls, "0.16.1", "0bd42ed05c7d2f4d180331a20113ec537be509da31fed5c8f7047ce59ee5a7c5", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "dae763468e2008cf7075a64cb1249c97cb4bc71e236c5c2b5e5cdf1cfa2bf138"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~> 2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
Expand Down
3 changes: 0 additions & 3 deletions test/support/examples.ex

This file was deleted.

3 changes: 0 additions & 3 deletions test/support/usage.ex

This file was deleted.

22 changes: 9 additions & 13 deletions test/xema/cast/any_of_test.exs
Expand Up @@ -278,19 +278,15 @@ defmodule Xema.Cast.AnyOfTest do
end

test "from data causing a deep nested cast error", %{schema: schema} do
assert {:error, error} =
cast(schema, %{foo: %{faa: "one"}, bar: %{bas: %{baz: "two", bax: "three"}}})

assert Exception.message(error) ==
"""
cannot cast %{bax: "three", baz: "two"} at [:bar, :bas] to any of:
cannot cast "two" at [:baz] to any of:
cannot cast "two" to :integer
cannot cast "two" to nil
cannot cast "three" at [:bax] to any of:
cannot cast "three" to :integer
cannot cast "three" to nil\
"""
assert {:error, error} = cast(schema, %{bar: %{bas: %{baz: "two", bax: "three"}}})

assert message = Exception.message(error)
assert message =~ ~s|cannot cast "two" at [:baz] to any of:|
assert message =~ ~s|cannot cast "two" to :integer|
assert message =~ ~s|cannot cast "two" to nil|
assert message =~ ~s|cannot cast "three" at [:bax] to any of:|
assert message =~ ~s|cannot cast "three" to :integer|
assert message =~ ~s|cannot cast "three" to nil|
end
end

Expand Down
9 changes: 4 additions & 5 deletions test/xema/cast/example_test.exs
Expand Up @@ -146,11 +146,10 @@ defmodule Xema.Cast.ExampleTest do
test "validate/1" do
assert {:error, error} = Person.validate(%{name: 42, age: -1, fav: :php})

assert Exception.message(error) == """
Value -1 is less than minimum value of 0, at [:age].
Value :php is not defined in enum, at [:fav].
Expected :string, got 42, at [:name].\
"""
assert message = Exception.message(error)
assert message =~ ~s|Value -1 is less than minimum value of 0, at [:age].|
assert message =~ ~s|Value :php is not defined in enum, at [:fav].|
assert message =~ ~s|Expected :string, got 42, at [:name].|
end
end
end
30 changes: 10 additions & 20 deletions test/xema/custom_validator_test.exs
Expand Up @@ -88,14 +88,15 @@ defmodule Xema.CustomValidatorTest do
three: "one"
})

message = """
Expected minimum length of 5, got "foo", at [:long].
Validator fails with :no_palindrome for value "cats live on no evil star", at [:palindrome].
Expected maximum length of 3, got "foobar", at [:short].
Validator fails with :not_three_words for value "one", at [:three].\
"""

assert Exception.message(error) == message
assert message = Exception.message(error)

assert message =~ ~s|Expected minimum length of 5, got "foo", at [:long].|

assert message =~
~s|Validator fails with :no_palindrome for value "cats live on no evil star", at [:palindrome].|

assert message =~ ~s|Expected maximum length of 3, got "foobar", at [:short].|
assert message =~ ~s|Validator fails with :not_three_words for value "one", at [:three].|
end
end

Expand All @@ -111,17 +112,6 @@ defmodule Xema.CustomValidatorTest do
from = ~N[2019-01-03 12:05:42]
to = ~N[2019-01-01 12:05:42]

message =
String.replace(
"""
Validator fails with :to_before_from
for value %{from: ~N[2019-01-03 12:05:42],
to: ~N[2019-01-01 12:05:42]}.\
""",
"\n",
" "
)

assert {:error,
%ValidationError{
reason: %{
Expand All @@ -133,7 +123,7 @@ defmodule Xema.CustomValidatorTest do
}
} = error} = Schemas.validate(:timespan, %{from: from, to: to})

assert Exception.message(error) == message
assert Exception.message(error) =~ "Validator fails with :to_before_from"
end

test "with invalid data" do
Expand Down
19 changes: 10 additions & 9 deletions test/xema/keyword_test.exs
Expand Up @@ -78,12 +78,9 @@ defmodule Xema.KeywordTest do
} = error
} = validate(schema, foo: "foo", bar: 2)

message = """
Expected :string, got 2, at [:bar].
Expected :number, got "foo", at [:foo].\
"""

assert Exception.message(error) == message
assert message = Exception.message(error)
assert message =~ ~s|Expected :string, got 2, at [:bar].|
assert message =~ ~s|Expected :number, got "foo", at [:foo].|
end
end

Expand Down Expand Up @@ -263,12 +260,14 @@ defmodule Xema.KeywordTest do
:error,
%ValidationError{
reason: %{
required: [:a, :c]
required: required
}
} = error
} = validate(schema, b: 3, d: 8)

assert Exception.message(error) == "Required properties are missing: [:a, :c]."
assert Enum.sort(required) == [:a, :c]

assert Exception.message(error) =~ "Required properties are missing:"
end
end

Expand Down Expand Up @@ -627,7 +626,7 @@ defmodule Xema.KeywordTest do
:error,
%ValidationError{
reason: %{
value: [:a, :b, :foo],
value: values,
property_names: [
a: %{min_length: 3, value: "a"},
b: %{min_length: 3, value: "b"}
Expand All @@ -636,6 +635,8 @@ defmodule Xema.KeywordTest do
} = error
} = validate(schema, data)

assert Enum.sort(values) == [:a, :b, :foo]

message = """
Invalid property names.
:a : Expected minimum length of 3, got "a".
Expand Down
58 changes: 28 additions & 30 deletions test/xema/map_test.exs
Expand Up @@ -80,12 +80,9 @@ defmodule Xema.MapTest do
}
} = error} = validate(schema, %{foo: "foo", bar: 2})

message = """
Expected :string, got 2, at [:bar].
Expected :number, got "foo", at [:foo].\
"""

assert Exception.message(error) == message
assert message = Exception.message(error)
assert message =~ ~s|Expected :string, got 2, at [:bar].|
assert message =~ ~s|Expected :number, got "foo", at [:foo].|
end
end

Expand Down Expand Up @@ -225,8 +222,7 @@ defmodule Xema.MapTest do
} = error
} = validate(schema, %{a: 1, b: 2, c: 3, d: 4})

assert Exception.message(error) ==
"Expected at most 3 properties, got %{a: 1, b: 2, c: 3, d: 4}."
assert Exception.message(error) =~ "Expected at most 3 properties, got"
end
end

Expand Down Expand Up @@ -269,12 +265,9 @@ defmodule Xema.MapTest do
}
} = error} = validate(schema, %{foo: 44, add: 1, extra: 2})

message = """
Expected only defined properties, got key [:add].
Expected only defined properties, got key [:extra].\
"""

assert Exception.message(error) == message
assert message = Exception.message(error)
assert message =~ ~s|Expected only defined properties, got key [:add].|
assert message =~ ~s|Expected only defined properties, got key [:extra].|
end
end

Expand Down Expand Up @@ -404,22 +397,26 @@ defmodule Xema.MapTest do
assert {:error,
%ValidationError{
reason: %{
required: [:a, :b, :c]
required: required
}
} = error} = validate(schema, %{"a" => 1, "b" => 2, "c" => 3})

assert Exception.message(error) == "Required properties are missing: [:a, :b, :c]."
assert Enum.sort(required) == [:a, :b, :c]

assert Exception.message(error) =~ "Required properties are missing:"
end

test "validate/2 without required properties", %{schema: schema} do
assert {:error,
%ValidationError{
reason: %{
required: [:a, :c]
required: required
}
} = error} = validate(schema, %{b: 3, d: 8})

assert Exception.message(error) == "Required properties are missing: [:a, :c]."
assert Enum.sort(required) == [:a, :c]

assert Exception.message(error) =~ "Required properties are missing:"
end
end

Expand Down Expand Up @@ -806,14 +803,18 @@ defmodule Xema.MapTest do
assert {:error,
%ValidationError{
reason: %{
property_names: [
a: %{min_length: 3, value: "a"},
b: %{min_length: 3, value: "b"}
],
value: [:a, :b, :foo]
property_names: property_names,
value: value
}
} = error} = validate(schema, %{foo: 1, a: 2, b: 3})

assert Enum.sort(value) == [:a, :b, :foo]

assert Enum.sort(property_names) == [
a: %{min_length: 3, value: "a"},
b: %{min_length: 3, value: "b"}
]

message = """
Invalid property names.
:a : Expected minimum length of 3, got "a".
Expand Down Expand Up @@ -917,13 +918,10 @@ defmodule Xema.MapTest do
}
} = error} = validate(schema, %{properties: "maybe", items: 5, minimum: "5"})

message = """
Expected :string, got 5, at [:items].
Expected :integer, got "5", at [:minimum].
Value "maybe" is not defined in enum, at [:properties].\
"""

assert Exception.message(error) == message
assert message = Exception.message(error)
assert message =~ ~s|Expected :string, got 5, at [:items].|
assert message =~ ~s|Expected :integer, got "5", at [:minimum].|
assert message =~ ~s|Value "maybe" is not defined in enum, at [:properties].|
end
end

Expand Down

0 comments on commit 5a09671

Please sign in to comment.