Skip to content

Commit c136120

Browse files
committed
1 parent 0d0373c commit c136120

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

lib/open_api_spex/cast/error.ex

+8-3
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,14 @@ defmodule OpenApiSpex.Cast.Error do
265265
"#{count} is not a multiple of #{multiple}"
266266
end
267267

268-
def message(%{reason: max, length: max, value: size})
269-
when max in [:exclusive_max, :maximum] do
270-
"#{size} is larger than maximum #{max}"
268+
def message(%{reason: :exclusive_max, length: max, value: value})
269+
when value >= max do
270+
"#{value} is larger than exclusive maximum #{max}"
271+
end
272+
273+
def message(%{reason: :maximum, length: max, value: value})
274+
when value > max do
275+
"#{value} is larger than inclusive maximum #{max}"
271276
end
272277

273278
def message(%{reason: :exclusive_min, length: min, value: value})

lib/open_api_spex/cast/integer.ex

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ defmodule OpenApiSpex.Cast.Integer do
4646

4747
defp cast_integer(%{value: value, schema: %{maximum: maximum, exclusiveMaximum: true}} = ctx)
4848
when is_integer(value) and is_integer(maximum) do
49-
if value > maximum do
50-
Cast.error(ctx, {:exclusive_max, maximum, value})
51-
else
49+
if value < maximum do
5250
Cast.success(ctx, [:maximum, :exclusiveMaximum])
51+
else
52+
Cast.error(ctx, {:exclusive_max, maximum, value})
5353
end
5454
end
5555

5656
defp cast_integer(%{value: value, schema: %{maximum: maximum}} = ctx)
5757
when is_integer(value) and is_integer(maximum) do
58-
if value >= maximum do
59-
Cast.error(ctx, {:maximum, maximum, value})
60-
else
58+
if value <= maximum do
6159
Cast.success(ctx, :maximum)
60+
else
61+
Cast.error(ctx, {:maximum, maximum, value})
6262
end
6363
end
6464

test/cast/integer_test.exs

+8-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ defmodule OpenApiSpex.CastIntegerTest do
4242
test "with maximum" do
4343
schema = %Schema{type: :integer, maximum: 2}
4444
assert cast(value: 1, schema: schema) == {:ok, 1}
45-
assert {:error, [error]} = cast(value: 2, schema: schema)
45+
assert cast(value: 2, schema: schema) == {:ok, 2}
46+
assert {:error, [error]} = cast(value: 3, schema: schema)
4647
assert error.reason == :maximum
47-
assert error.value == 2
48+
assert error.value == 3
4849
# error.length is the maximum
4950
assert error.length == 2
51+
assert Error.message(error) =~ "larger than inclusive maximum"
5052
end
5153

5254
test "with minimum w/ exclusiveMinimum" do
@@ -62,12 +64,13 @@ defmodule OpenApiSpex.CastIntegerTest do
6264

6365
test "with maximum w/ exclusiveMaximum" do
6466
schema = %Schema{type: :integer, maximum: 2, exclusiveMaximum: true}
65-
assert cast(value: 2, schema: schema) == {:ok, 2}
66-
assert {:error, [error]} = cast(value: 3, schema: schema)
67+
assert cast(value: 1, schema: schema) == {:ok, 1}
68+
assert {:error, [error]} = cast(value: 2, schema: schema)
6769
assert error.reason == :exclusive_max
68-
assert error.value == 3
70+
assert error.value == 2
6971
# error.length is the maximum
7072
assert error.length == 2
73+
assert Error.message(error) =~ "larger than exclusive maximum"
7174
end
7275
end
7376
end

0 commit comments

Comments
 (0)