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.

Commit2a7293e

Browse files
committed
feat(editor-validator): add allow_empty to string
1 parentf60bf10 commit2a7293e

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

‎lib/helper/validator/schema.ex‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ defmodule Helper.Validator.Schema do
8686
end
8787

8888
# custom validate logic
89-
# min option for @support_min types
89+
## min option for @support_min types
9090
defpmatch(field,value,type,[{:min,min}|options])
9191
whentypein@support_minandg_not_nil(value)andg_pos_int(min)do
9292
caseUtils.large_than(value,min)do
@@ -98,7 +98,7 @@ defmodule Helper.Validator.Schema do
9898
end
9999
end
100100

101-
# starts_with option for string
101+
## starts_with option for string
102102
defpmatch(field,value,type,[{:starts_with,starts}|options])whenis_binary(value)do
103103
caseString.starts_with?(value,starts)do
104104
true->
@@ -109,7 +109,7 @@ defmodule Helper.Validator.Schema do
109109
end
110110
end
111111

112-
# item type for list
112+
## item type for list
113113
defpmatch(field,value,type,[{:type,:map}|options])whenis_list(value)do
114114
caseEnum.all?(value,&is_map(&1))do
115115
true->
@@ -120,17 +120,20 @@ defmodule Helper.Validator.Schema do
120120
end
121121
end
122122

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)
123+
# allow empty for list
124+
defpmatch(field,value,_type,[{:allow_empty,false}|_options])
125+
whenis_list(value)andvalue==[]do
126+
error(field,value,:allow_empty)
127+
end
127128

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

133-
defpmatch(field,value,type,[{:allow_empty,true}|options])whenis_list(value)do
135+
defpmatch(field,value,type,[{:allow_empty,_}|options])
136+
whenis_binary(value)oris_list(value)do
134137
match(field,value,type,options)
135138
end
136139

‎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: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
5050
data=%{"text"=>"text"}
5151
assert{:error,error}=Schema.cast(schema,data)
5252
asserterror==[%{field:"text",message:"should starts with: https://",value:"text"}]
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"}
@@ -154,7 +160,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
154160
# IO.inspect(Schema.cast(schema, data), label: "schema result")
155161
end
156162

157-
@tag:wip2
163+
@tag:wip
158164
test"boolean with options"do
159165
schema=%{"text"=>[:boolean,required:false]}
160166
data=%{"no_exsit"=>false}
@@ -170,7 +176,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
170176
assert{:ok,_}=Schema.cast(schema,data)
171177
end
172178

173-
@tag:wip2
179+
@tag:wip
174180
test"enum with options"do
175181
schema=%{"text"=>[enum:[1,2,3],required:false]}
176182
data=%{"no_exsit"=>false}
@@ -188,5 +194,15 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
188194
# IO.inspect(Schema.cast(schema, data), label: "schema result")
189195
# hello world
190196
end
197+
198+
@tag:wip2
199+
test"schema invalid option test"do
200+
schema=%{"text"=>[:string,allow_empty:false]}
201+
data=%{"text"=>""}
202+
# assert {:ok, _} = Schema.cast(schema, data)
203+
204+
IO.inspect(Schema.cast(schema,data),label:"schema result")
205+
#
206+
end
191207
end
192208
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp