ash/test/type/new_type_test.exs
Riccardo Binetti df7adce976
fix: apply constraints to NewType even when casting an array (#1341)
The previous implementation was behaving differently when dealing with single
values and arrays.
Close #1342
2024-07-25 10:17:18 -04:00

50 lines
1.4 KiB
Elixir

defmodule Ash.Test.Type.NewTypeTest do
@moduledoc false
use ExUnit.Case, async: true
defmodule SSN do
use Ash.Type.NewType,
subtype_of: :string,
constraints: [match: ~r/^(?!0{3})(?!6{3})[0-8]\d{2}-(?!0{2})\d{2}-(?!0{4})\d{4}$/]
end
defmodule CustomMap do
use Ash.Type.NewType,
subtype_of: :map,
constraints: [
fields: [
foo: [
type: :string
],
bar: [
type: :integer
]
]
]
end
test "it handles simple types" do
assert {:ok, "123-45-6789"} = Ash.Type.cast_input(SSN, "123-45-6789")
assert {:ok, nil} = Ash.Type.cast_input(SSN, nil)
end
test "it applies the provided constraints" do
assert {:error,
message: "must match the pattern %{regex}",
regex: "~r/^(?!0{3})(?!6{3})[0-8]\\d{2}-(?!0{2})\\d{2}-(?!0{4})\\d{4}$/"} =
Ash.Type.cast_input(SSN, "hello world")
end
test "it casts maps correctly" do
assert {:ok, %{foo: "baz", bar: 42}} ==
Ash.Type.cast_input(CustomMap, %{"foo" => "baz", "bar" => "42", "other" => "ignored"})
end
test "it casts array of maps correctly" do
assert {:ok, [%{foo: "baz", bar: 42}]} ==
Ash.Type.cast_input({:array, CustomMap}, [
%{"foo" => "baz", "bar" => "42", "other" => "ignored"}
])
end
end