Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitd035a4b

Browse files
authored
feat(editor-validator): more spec error hint on invalid schema options (#300)
* feat(editor-validator): add allow_empty to string* refactor(editor-validator): more spec on invalid schema opt error hint
1 parentf60bf10 commitd035a4b

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

‎lib/helper/validator/schema.ex‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ defmodule Helper.Validator.Schema do
5656
end)
5757
end
5858

59-
defpoption_valid?({:min,v})whenis_integer(v),do:true
60-
defpoption_valid?({:required,v})whenis_boolean(v),do:true
61-
defpoption_valid?({:starts_with,v})whenis_binary(v),do:true
62-
defpoption_valid?({:type,:map}),do:true
63-
defpoption_valid?({:allow_empty,v})whenis_boolean(v),do:true
59+
defpoption_valid?(:string,{:min,v})whenis_integer(v),do:true
60+
defpoption_valid?(:number,{:min,v})whenis_integer(v),do:true
6461

65-
defpoption_valid?(_),do:false
62+
defpoption_valid?(_,{:required,v})whenis_boolean(v),do:true
63+
defpoption_valid?(:string,{:starts_with,v})whenis_binary(v),do:true
64+
defpoption_valid?(:list,{:type,:map}),do:true
65+
defpoption_valid?(:string,{:allow_empty,v})whenis_boolean(v),do:true
66+
defpoption_valid?(:list,{:allow_empty,v})whenis_boolean(v),do:true
67+
68+
defpoption_valid?(_,_),do:false
6669

6770
defpmatch(field,nil,enum:_,required:false),do:done(field,nil)
6871
defpmatch(field,value,enum:enum,required:_),do:match(field,value,enum:enum)
@@ -86,7 +89,7 @@ defmodule Helper.Validator.Schema do
8689
end
8790

8891
# custom validate logic
89-
# min option for @support_min types
92+
## min option for @support_min types
9093
defpmatch(field,value,type,[{:min,min}|options])
9194
whentypein@support_minandg_not_nil(value)andg_pos_int(min)do
9295
caseUtils.large_than(value,min)do
@@ -98,7 +101,7 @@ defmodule Helper.Validator.Schema do
98101
end
99102
end
100103

101-
# starts_with option for string
104+
## starts_with option for string
102105
defpmatch(field,value,type,[{:starts_with,starts}|options])whenis_binary(value)do
103106
caseString.starts_with?(value,starts)do
104107
true->
@@ -109,7 +112,7 @@ defmodule Helper.Validator.Schema do
109112
end
110113
end
111114

112-
# item type for list
115+
## item type for list
113116
defpmatch(field,value,type,[{:type,:map}|options])whenis_list(value)do
114117
caseEnum.all?(value,&is_map(&1))do
115118
true->
@@ -120,17 +123,20 @@ defmodule Helper.Validator.Schema do
120123
end
121124
end
122125

123-
defpmatch(field,value,type,[{:allow_empty,false}|options])whenis_list(value)do
124-
caselength(value)do
125-
0->
126-
error(field,value,:allow_empty)
126+
# allow empty for list
127+
defpmatch(field,value,_type,[{:allow_empty,false}|_options])
128+
whenis_list(value)andvalue==[]do
129+
error(field,value,:allow_empty)
130+
end
127131

128-
_->
129-
match(field,value,type,options)
130-
end
132+
# allow empty for string
133+
defpmatch(field,value,_type,[{:allow_empty,false}|_options])
134+
whenis_binary(value)andbyte_size(value)==0do
135+
error(field,value,:allow_empty)
131136
end
132137

133-
defpmatch(field,value,type,[{:allow_empty,true}|options])whenis_list(value)do
138+
defpmatch(field,value,type,[{:allow_empty,_}|options])
139+
whenis_binary(value)oris_list(value)do
134140
match(field,value,type,options)
135141
end
136142

@@ -146,7 +152,7 @@ defmodule Helper.Validator.Schema do
146152
# error for option
147153
defpmatch(field,value,type,[option])whenis_tuple(option)do
148154
# 如果这里不判断的话会和下面的 match 冲突,是否有更好的写法?
149-
caseoption_valid?(option)do
155+
caseoption_valid?(type,option)do
150156
true->
151157
error(field,value,type)
152158

‎test/helper/converter/editor_to_html_test/image_test.exs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
208208
]==err_msg
209209
end
210210

211-
@tag:wip2
211+
@tag:wip
212212
test"invalid data parse should raise error message"do
213213
editor_json=
214214
set_items("single",[
@@ -231,7 +231,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
231231
]
232232
end
233233

234-
@tag:wip2
234+
@tag:wip
235235
test"src should starts with https://"do
236236
editor_json=
237237
set_items("single",[

‎test/helper/validator/schema_test.exs‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
asserterror==[%{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-
asserterror==[%{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-
asserterror==[%{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-
asserterror==[%{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-
asserterror==[%{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-
asserterror==[%{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-
asserterror==[%{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
asserterror==[%{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-
asserterror==
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-
asserterror==[%{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-
asserterror==[%{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
192207
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp