diff --git a/test/cast_test.exs b/test/cast_test.exs index f5d4ef17..ed178f01 100644 --- a/test/cast_test.exs +++ b/test/cast_test.exs @@ -222,6 +222,34 @@ defmodule OpenApiSpec.CastTest do assert Error.message_with_path(error) == "#/age: Invalid strict_integer. Got: string" end + test "cast custom error with custom validator" do + defmodule EvenInt do + require OpenApiSpex + + alias OpenApiSpex.Cast + + OpenApiSpex.schema(%{ + description: "An even integer", + type: :integer, + "x-validate": __MODULE__ + }) + + def cast(context = %Cast{value: value}) when is_integer(value) and rem(value, 2) == 0, + do: Cast.ok(context) + + def cast(context), do: Cast.error(context, {:custom, "Must be an even integer"}) + end + + schema = %Schema{type: :object, properties: %{even_number: EvenInt.schema()}} + + assert {:error, errors} = cast(value: %{"even_number" => 1}, schema: schema) + assert [error] = errors + assert %Error{} = error + assert error.reason == :custom + assert error.path == [:even_number] + assert Error.message_with_path(error) == "#/even_number: Must be an even integer" + end + test "nil value with xxxOf" do schema = %Schema{anyOf: [%Schema{nullable: true, type: :string}]} assert {:ok, nil} = cast(value: nil, schema: schema)