Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ash/type/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ defmodule Ash.Type.Range do
end
end

def apply_constraints(_value, _constraints), do: {:error, message: "is not a valid range"}

# An empty range is constructed, not mistyped, so it is refused rather than nulled.
defp do_apply_constraints(%Range{empty?: true} = range, constraints) do
if Keyword.get(constraints, :allow_empty?, false) do
Expand Down Expand Up @@ -344,7 +346,7 @@ defmodule Ash.Type.Range do

defp extract({lower, upper}), do: {:ok, lower, upper, :"[)", false}

defp extract(%{} = map) do
defp extract(%{} = map) when not is_struct(map) do
lower = map[:lower] || map["lower"]
upper = map[:upper] || map["upper"]
bounds = map[:bounds] || map["bounds"] || :"[)"
Expand Down
20 changes: 20 additions & 0 deletions test/type/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ defmodule Ash.Type.RangeTest do
)
end

test "cast_input refuses a value that is not a range" do
for value <- [~U[2026-01-01 00:00:00Z], ~D[2026-01-01], Decimal.new(1), "nope", 5] do
assert {:error, _} = Ash.Type.cast_input(Ash.Type.Range, value, @constraints),
"expected #{inspect(value)} to be refused"
end
end

test "cast_stored refuses a value that is not a range" do
assert {:error, _} =
Ash.Type.cast_stored(Ash.Type.Range, ~U[2026-01-01 00:00:00Z], @constraints)
end

test "apply_constraints refuses a value that is not a range" do
assert {:error, _} =
Ash.Type.apply_constraints(Ash.Type.Range, ~U[2026-01-01 00:00:00Z], @constraints)

assert {:error, _} =
Ash.Type.apply_constraints(Ash.Type.Range, %{lower: 1, upper: 5}, @int_constraints)
end

test "cast_stored refuses a bounds that is not a bounds specifier" do
assert {:error, _} =
Ash.Type.cast_stored(
Expand Down
Loading