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.

Commitb27054b

Browse files
committed
feat(editor-validator): add type and allow_empty to list option
1 parent4d7d2d0 commitb27054b

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

‎lib/helper/converter/editor_to_html/validator/editor_schema.ex‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
5858
parent:%{
5959
"id"=>[:string,required:false],
6060
"mode"=>[enum:@valid_list_mode],
61-
"items"=>[:list]
61+
"items"=>[:list,type::map,allow_empty:false]
6262
},
6363
item:%{
6464
"checked"=>[:boolean],
@@ -77,7 +77,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
7777
parent:%{
7878
"id"=>[:string,required:false],
7979
"columnCount"=>[:number,min:2],
80-
"items"=>[:list]
80+
"items"=>[:list,type::map,allow_empty:false]
8181
},
8282
item:%{
8383
"text"=>[:string],
@@ -94,7 +94,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
9494
parent:%{
9595
"id"=>[:string,required:false],
9696
"mode"=>[enum:@valid_image_mode],
97-
"items"=>[:list]
97+
"items"=>[:list,type::map,allow_empty:false]
9898
},
9999
item:%{
100100
"src"=>[:string,starts_with:"https://"],

‎lib/helper/validator/schema.ex‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ defmodule Helper.Validator.Schema do
5959
defpoption_valid?({:min,v})whenis_integer(v),do:true
6060
defpoption_valid?({:required,v})whenis_boolean(v),do:true
6161
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
64+
6265
defpoption_valid?(_),do:false
6366

6467
defpmatch(field,nil,enum:_,required:false),do:done(field,nil)
@@ -83,6 +86,7 @@ defmodule Helper.Validator.Schema do
8386
end
8487

8588
# custom validate logic
89+
# min option for @support_min types
8690
defpmatch(field,value,type,[{:min,min}|options])
8791
whentypein@support_minandg_not_nil(value)andg_pos_int(min)do
8892
caseUtils.large_than(value,min)do
@@ -94,6 +98,7 @@ defmodule Helper.Validator.Schema do
9498
end
9599
end
96100

101+
# starts_with option for string
97102
defpmatch(field,value,type,[{:starts_with,starts}|options])whenis_binary(value)do
98103
caseString.starts_with?(value,starts)do
99104
true->
@@ -104,6 +109,31 @@ defmodule Helper.Validator.Schema do
104109
end
105110
end
106111

112+
# item type for list
113+
defpmatch(field,value,type,[{:type,:map}|options])whenis_list(value)do
114+
caseEnum.all?(value,&is_map(&1))do
115+
true->
116+
match(field,value,type,options)
117+
118+
false->
119+
error(field,value,:list_type_map)
120+
end
121+
end
122+
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)
127+
128+
_->
129+
match(field,value,type,options)
130+
end
131+
end
132+
133+
defpmatch(field,value,type,[{:allow_empty,true}|options])whenis_list(value)do
134+
match(field,value,type,options)
135+
end
136+
107137
# custom validate logic end
108138

109139
# main type
@@ -140,6 +170,14 @@ defmodule Helper.Validator.Schema do
140170
{:error,%{field:field|>to_string,value:value,message:"should starts with:#{expect}"}}
141171
end
142172

173+
defperror(field,value,:list_type_map)do
174+
{:error,%{field:field|>to_string,value:value,message:"item should be map"}}
175+
end
176+
177+
defperror(field,value,:allow_empty)do
178+
{:error,%{field:field|>to_string,value:value,message:"empty is not allowed"}}
179+
end
180+
143181
# custom error hint end
144182

145183
defperror(field,value,option:option)do

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,14 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
198198
{:ok,editor_string}=Jason.encode(editor_json)
199199
{:error,err_msg}=Parser.to_html(editor_string)
200200

201-
asserterr_msg==[
201+
assert[
202+
%{block:"image",field:"items",message:"empty is not allowed",value:[]},
202203
%{
203204
block:"image",
204205
field:"mode",
205206
message:"should be: single | jiugongge | gallery"
206207
}
207-
]
208+
]==err_msg
208209
end
209210

210211
@tag:wip2

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,14 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
215215
{:ok,editor_string}=Jason.encode(editor_json)
216216
{:error,err_msg}=Parser.to_html(editor_string)
217217

218-
asserterr_msg==[
218+
assert[
219+
%{block:"list",field:"items",message:"empty is not allowed",value:[]},
219220
%{
220221
block:"list",
221222
field:"mode",
222223
message:"should be: checklist | order_list | unorder_list"
223224
}
224-
]
225+
]==err_msg
225226
end
226227

227228
@tag:wip

‎test/helper/validator/schema_test.exs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
130130
schema=%{"text"=>[:list,required:true]}
131131
data=%{"text"=>[]}
132132
assert{:ok,_}=Schema.cast(schema,data)
133+
134+
schema=%{"text"=>[:list]}
135+
data=%{"text"=>[]}
136+
assert{:ok,_}=Schema.cast(schema,data)
137+
138+
schema=%{"text"=>[:list,allow_empty:true]}
139+
data=%{"text"=>[]}
140+
assert{:ok,_}=Schema.cast(schema,data)
141+
142+
schema=%{"text"=>[:list,type::map]}
143+
data=%{"text"=>[1,2,3]}
144+
{:error,error}=Schema.cast(schema,data)
145+
146+
asserterror==
147+
[%{field:"text",message:"item should be map",value:[1,2,3]}]
148+
149+
schema=%{"text"=>[:list,allow_empty:false]}
150+
data=%{"text"=>[]}
151+
{:error,error}=Schema.cast(schema,data)
152+
assert[%{field:"text",message:"empty is not allowed",value:[]}]==error
153+
154+
# IO.inspect(Schema.cast(schema, data), label: "schema result")
133155
end
134156

135157
@tag:wip2

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp