@@ -15,7 +15,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
1515 schema = % { "text" => [ :string , required: true ] }
1616 data = % { "no_exsit" => "text" }
1717 { :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
1919
2020 schema = % { "text" => [ :string , required: true ] }
2121 data = % { "text" => "text" }
@@ -24,36 +24,42 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
2424 schema = % { "text" => [ :string , min: 5 ] }
2525 data = % { "text" => "text" }
2626 { :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
2828
2929 schema = % { "text" => [ :string , required: false , min: 5 ] }
3030 data = % { "text" => "text" }
3131 { :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
3333
3434 schema = % { "text" => [ :string , min: 5 ] }
3535 data = % { "no_exsit" => "text" }
3636 { :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
3838
3939 schema = % { "text" => [ :string , required: true , min: 5 ] }
4040 data = % { "no_exsit" => "text" }
4141 { :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
4343
4444 schema = % { "text" => [ :string , required: true , min: "5" ] }
4545 data = % { "text" => "text" }
4646 { :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
4848
4949 schema = % { "text" => [ :string , starts_with: "https://" ] }
5050 data = % { "text" => "text" }
5151 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+
5359 # IO.inspect(Schema.cast(schema, data), label: "schema result")
5460 end
5561
56- @ tag :wip2
62+ @ tag :wip
5763 test "number with options" do
5864 schema = % { "text" => [ :number , required: false ] }
5965 data = % { "no_exsit" => 1 }
@@ -92,7 +98,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
9298 # hello world
9399 end
94100
95- @ tag :wip2
101+ @ tag :wip
96102 test "number with wrong option" do
97103 schema = % { "text" => [ :number , required: true , min: "5" ] }
98104 data = % { "text" => 1 }
@@ -107,7 +113,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
107113 assert error == [ % { field: "text" , message: "unknow option: no_exsit_option: xxx" , value: 1 } ]
108114 end
109115
110- @ tag :wip2
116+ @ tag :wip
111117 test "number with options edage case" do
112118 schema = % { "text" => [ :number , min: 2 ] }
113119 data = % { "text" => "aa" }
@@ -143,8 +149,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
143149 data = % { "text" => [ 1 , 2 , 3 ] }
144150 { :error , error } = Schema . cast ( schema , data )
145151
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
148153
149154 schema = % { "text" => [ :list , allow_empty: false ] }
150155 data = % { "text" => [ ] }
@@ -154,7 +159,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
154159 # IO.inspect(Schema.cast(schema, data), label: "schema result")
155160 end
156161
157- @ tag :wip2
162+ @ tag :wip
158163 test "boolean with options" do
159164 schema = % { "text" => [ :boolean , required: false ] }
160165 data = % { "no_exsit" => false }
@@ -163,14 +168,14 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
163168 schema = % { "text" => [ :boolean , required: true ] }
164169 data = % { "no_exsit" => false }
165170 { :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
167172
168173 schema = % { "text" => [ :boolean , required: true ] }
169174 data = % { "text" => false }
170175 assert { :ok , _ } = Schema . cast ( schema , data )
171176 end
172177
173- @ tag :wip2
178+ @ tag :wip
174179 test "enum with options" do
175180 schema = % { "text" => [ enum: [ 1 , 2 , 3 ] , required: false ] }
176181 data = % { "no_exsit" => false }
@@ -179,7 +184,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
179184 schema = % { "text" => [ enum: [ 1 , 2 , 3 ] , required: true ] }
180185 data = % { "no_exsit" => false }
181186 { :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
183188
184189 schema = % { "text" => [ enum: [ 1 , 2 , 3 ] ] }
185190 data = % { "text" => 1 }
@@ -188,5 +193,15 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
188193 # IO.inspect(Schema.cast(schema, data), label: "schema result")
189194 # hello world
190195 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
191206 end
192207end
0 commit comments