@@ -15,7 +15,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
15
15
schema = % { "text" => [ :string , required: true ] }
16
16
data = % { "no_exsit" => "text" }
17
17
{ :error , error } = Schema . cast ( schema , data )
18
- assert error == [ % { field: "text" , message: "should be: string" , value: nil } ]
18
+ assert [ % { field: "text" , message: "should be: string" , value: nil } ] == error
19
19
20
20
schema = % { "text" => [ :string , required: true ] }
21
21
data = % { "text" => "text" }
@@ -24,36 +24,42 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
24
24
schema = % { "text" => [ :string , min: 5 ] }
25
25
data = % { "text" => "text" }
26
26
{ :error , error } = Schema . cast ( schema , data )
27
- assert error == [ % { field: "text" , message: "min size: 5" , value: "text" } ]
27
+ assert [ % { field: "text" , message: "min size: 5" , value: "text" } ] == error
28
28
29
29
schema = % { "text" => [ :string , required: false , min: 5 ] }
30
30
data = % { "text" => "text" }
31
31
{ :error , error } = Schema . cast ( schema , data )
32
- assert error == [ % { field: "text" , message: "min size: 5" , value: "text" } ]
32
+ assert [ % { field: "text" , message: "min size: 5" , value: "text" } ] === error
33
33
34
34
schema = % { "text" => [ :string , min: 5 ] }
35
35
data = % { "no_exsit" => "text" }
36
36
{ :error , error } = Schema . cast ( schema , data )
37
- assert error == [ % { field: "text" , message: "should be: string" , value: nil } ]
37
+ assert [ % { field: "text" , message: "should be: string" , value: nil } ] == error
38
38
39
39
schema = % { "text" => [ :string , required: true , min: 5 ] }
40
40
data = % { "no_exsit" => "text" }
41
41
{ :error , error } = Schema . cast ( schema , data )
42
- assert error == [ % { field: "text" , message: "should be: string" , value: nil } ]
42
+ assert [ % { field: "text" , message: "should be: string" , value: nil } ] == error
43
43
44
44
schema = % { "text" => [ :string , required: true , min: "5" ] }
45
45
data = % { "text" => "text" }
46
46
{ :error , error } = Schema . cast ( schema , data )
47
- assert error == [ % { field: "text" , message: "unknow option: min: 5" , value: "text" } ]
47
+ assert [ % { field: "text" , message: "unknow option: min: 5" , value: "text" } ] == error
48
48
49
49
schema = % { "text" => [ :string , starts_with: "https://" ] }
50
50
data = % { "text" => "text" }
51
51
assert { :error , error } = Schema . cast ( schema , data )
52
- assert error == [ % { field: "text" , message: "should starts with: https://" , value: "text" } ]
52
+ assert [ % { field: "text" , message: "should starts with: https://" , value: "text" } ] == error
53
+
54
+ schema = % { "text" => [ :string , allow_empty: false ] }
55
+ data = % { "text" => "" }
56
+ assert { :error , error } = Schema . cast ( schema , data )
57
+ assert [ % { field: "text" , message: "empty is not allowed" , value: "" } ] == error
58
+
53
59
# IO.inspect(Schema.cast(schema, data), label: "schema result")
54
60
end
55
61
56
- @ tag :wip2
62
+ @ tag :wip
57
63
test "number with options" do
58
64
schema = % { "text" => [ :number , required: false ] }
59
65
data = % { "no_exsit" => 1 }
@@ -92,7 +98,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
92
98
# hello world
93
99
end
94
100
95
- @ tag :wip2
101
+ @ tag :wip
96
102
test "number with wrong option" do
97
103
schema = % { "text" => [ :number , required: true , min: "5" ] }
98
104
data = % { "text" => 1 }
@@ -107,7 +113,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
107
113
assert error == [ % { field: "text" , message: "unknow option: no_exsit_option: xxx" , value: 1 } ]
108
114
end
109
115
110
- @ tag :wip2
116
+ @ tag :wip
111
117
test "number with options edage case" do
112
118
schema = % { "text" => [ :number , min: 2 ] }
113
119
data = % { "text" => "aa" }
@@ -143,8 +149,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
143
149
data = % { "text" => [ 1 , 2 , 3 ] }
144
150
{ :error , error } = Schema . cast ( schema , data )
145
151
146
- assert error ==
147
- [ % { field: "text" , message: "item should be map" , value: [ 1 , 2 , 3 ] } ]
152
+ assert [ % { field: "text" , message: "item should be map" , value: [ 1 , 2 , 3 ] } ] == error
148
153
149
154
schema = % { "text" => [ :list , allow_empty: false ] }
150
155
data = % { "text" => [ ] }
@@ -154,7 +159,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
154
159
# IO.inspect(Schema.cast(schema, data), label: "schema result")
155
160
end
156
161
157
- @ tag :wip2
162
+ @ tag :wip
158
163
test "boolean with options" do
159
164
schema = % { "text" => [ :boolean , required: false ] }
160
165
data = % { "no_exsit" => false }
@@ -163,14 +168,14 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
163
168
schema = % { "text" => [ :boolean , required: true ] }
164
169
data = % { "no_exsit" => false }
165
170
{ :error , error } = Schema . cast ( schema , data )
166
- assert error == [ % { field: "text" , message: "should be: boolean" , value: nil } ]
171
+ assert [ % { field: "text" , message: "should be: boolean" , value: nil } ] == error
167
172
168
173
schema = % { "text" => [ :boolean , required: true ] }
169
174
data = % { "text" => false }
170
175
assert { :ok , _ } = Schema . cast ( schema , data )
171
176
end
172
177
173
- @ tag :wip2
178
+ @ tag :wip
174
179
test "enum with options" do
175
180
schema = % { "text" => [ enum: [ 1 , 2 , 3 ] , required: false ] }
176
181
data = % { "no_exsit" => false }
@@ -179,7 +184,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
179
184
schema = % { "text" => [ enum: [ 1 , 2 , 3 ] , required: true ] }
180
185
data = % { "no_exsit" => false }
181
186
{ :error , error } = Schema . cast ( schema , data )
182
- assert error == [ % { field: "text" , message: "should be: 1 | 2 | 3" } ]
187
+ assert [ % { field: "text" , message: "should be: 1 | 2 | 3" } ] == error
183
188
184
189
schema = % { "text" => [ enum: [ 1 , 2 , 3 ] ] }
185
190
data = % { "text" => 1 }
@@ -188,5 +193,15 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
188
193
# IO.inspect(Schema.cast(schema, data), label: "schema result")
189
194
# hello world
190
195
end
196
+
197
+ @ tag :wip
198
+ test "schema invalid option should got error" do
199
+ schema = % { "text" => [ :number , allow_empty: false ] }
200
+ data = % { "text" => 1 }
201
+
202
+ { :error , error } = Schema . cast ( schema , data )
203
+
204
+ assert [ % { field: "text" , message: "unknow option: allow_empty: false" , value: 1 } ] == error
205
+ end
191
206
end
192
207
end
0 commit comments