@@ -15,6 +15,12 @@ defmodule OpenApiSpex.CastStringTest do
15
15
assert error . value == % { }
16
16
end
17
17
18
+ test "preserves atom values" do
19
+ schema = % Schema { type: :string }
20
+
21
+ assert cast ( value: :hello , schema: schema ) == { :ok , :hello }
22
+ end
23
+
18
24
test "string with pattern" do
19
25
schema = % Schema { type: :string , pattern: ~r/ \d -\d / }
20
26
assert cast ( value: "1-2" , schema: schema ) == { :ok , "1-2" }
@@ -80,6 +86,7 @@ defmodule OpenApiSpex.CastStringTest do
80
86
# Note: we measure length of string after trimming leading and trailing whitespace
81
87
test "minLength" do
82
88
schema = % Schema { type: :string , minLength: 1 }
89
+
83
90
assert { :ok , "a" } = cast ( value: "a" , schema: schema )
84
91
assert { :error , [ error ] } = cast ( value: "" , schema: schema )
85
92
assert % Error { } = error
@@ -89,27 +96,32 @@ defmodule OpenApiSpex.CastStringTest do
89
96
# Note: we measure length of string after trimming leading and trailing whitespace
90
97
test "maxLength" do
91
98
schema = % Schema { type: :string , maxLength: 1 }
99
+
92
100
assert { :ok , "a" } = cast ( value: "a" , schema: schema )
93
- assert { :error , [ error ] } = cast ( value: "aa" , schema: schema )
94
- assert % Error { } = error
95
- assert error . reason == :max_length
101
+ assert { :error , [ % Error { reason: :max_length } ] } = cast ( value: "aa" , schema: schema )
102
+
103
+ assert { :ok , :a } = cast ( value: :a , schema: schema )
104
+ assert { :error , [ % Error { reason: :max_length } ] } = cast ( value: :aa , schema: schema )
96
105
end
97
106
98
107
test "maxLength and minLength" do
99
108
schema = % Schema { type: :string , minLength: 1 , maxLength: 2 }
100
- assert { :error , [ error ] } = cast ( value: "" , schema: schema )
101
- assert % Error { } = error
102
- assert error . reason == :min_length
103
- assert { :error , [ error ] } = cast ( value: "aaa" , schema: schema )
104
- assert % Error { } = error
105
- assert error . reason == :max_length
109
+
110
+ assert { :error , [ % Error { reason: :min_length } ] } = cast ( value: "" , schema: schema )
111
+ assert { : error, [ % Error { reason: :max_length } ] } = cast ( value: "aaa" , schema: schema )
112
+
113
+ assert { :error , [ % Error { reason: :min_length } ] } = cast ( value: :"" , schema: schema )
114
+ assert { : error, [ % Error { reason: :max_length } ] } = cast ( value: :aaa , schema: schema )
106
115
end
107
116
108
117
test "minLength and pattern" do
109
118
schema = % Schema { type: :string , minLength: 1 , pattern: ~r/ \d -\d / }
110
- assert { :error , errors } = cast ( value: "" , schema: schema )
111
- assert length ( errors ) == 2
112
- assert Enum . map ( errors , & & 1 . reason ) == [ :invalid_format , :min_length ]
119
+
120
+ assert { :error , [ % Error { reason: :invalid_format } , % Error { reason: :min_length } ] } =
121
+ cast ( value: "" , schema: schema )
122
+
123
+ assert { :error , [ % Error { reason: :invalid_format } , % Error { reason: :min_length } ] } =
124
+ cast ( value: :"" , schema: schema )
113
125
end
114
126
end
115
127
end
0 commit comments