Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle oneOf cast when there are multiple success along with failure #362

Merged
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
2 changes: 1 addition & 1 deletion lib/open_api_spex/cast/one_of.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule OpenApiSpex.Cast.OneOf do
case {valid_schemas, failed_schemas} do
{[], []} -> "no schemas given"
{[], _} -> "no schemas validate"
{_, []} -> "more than one schemas validate"
{_, _} -> "more than one schemas validate"
end

Cast.error(
Expand Down
12 changes: 12 additions & 0 deletions test/cast/one_of_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ defmodule OpenApiSpex.CastOneOfTest do
"Failed to cast value to one of: more than one schemas validate"
end

test "oneOf, more than one matching schema with failing schema" do
schema = %Schema{
oneOf: [%Schema{type: :integer}, %Schema{type: :string}, %Schema{type: :bool}]
}

assert {:error, [error]} = cast(value: "1", schema: schema)
assert error.reason == :one_of

assert Error.message(error) ==
"Failed to cast value to one of: more than one schemas validate"
end

test "oneOf, no castable schema" do
schema = %Schema{oneOf: [%Schema{type: :string}]}
assert {:error, [error]} = cast(value: 1, schema: schema)
Expand Down