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.

Commite8d940e

Browse files
committed
refactor(editor): validate_with logic re-org
1 parentbad9428 commite8d940e

File tree

2 files changed

+20
-25
lines changed
  • lib/helper/converter/editor_to_html/validator
  • test/helper/converter/editor_to_html_test

2 files changed

+20
-25
lines changed

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ defmodule Helper.Converter.EditorToHTML.Validator do
3030
end
3131

3232
defpvalidate_editor_fmt(data)do
33-
validate_with("editor",EditorSchema.get("editor"),data)
33+
trydo
34+
validate_with("editor",EditorSchema.get("editor"),data)
35+
rescue
36+
einMatchError->
37+
format_parse_error(e)
38+
39+
_->
40+
format_parse_error()
41+
end
3442
end
3543

3644
defpvalidate_blocks([]),do:{:ok,:pass}
@@ -69,43 +77,30 @@ defmodule Helper.Converter.EditorToHTML.Validator do
6977
end
7078

7179
defpvalidate_block(%{"type"=>type}),do:raise("undown#{type} block")
80+
7281
defpvalidate_block(e),do:raise("undown block:#{e}")
7382

7483
# validate with given schema
7584
defpvalidate_with(block,schema,data)do
7685
caseSchema.cast(schema,data)do
7786
{:error,errors}->
78-
format_parse_error(block,errors)
87+
{:error,message}=format_parse_error(block,errors)
88+
raise%MatchError{term:{:error,message}}
7989

8090
_->
8191
{:ok,:pass}
8292
end
8393
end
8494

8595
defpvalidate_with(block,parent_schema,item_schema,data)do
86-
caseSchema.cast(parent_schema,data)do
87-
{:error,errors}->
88-
{:error,message}=format_parse_error(block,errors)
89-
raise%MatchError{term:{:error,message}}
96+
with{:ok,_}<-validate_with(block,parent_schema,data),
97+
%{"mode"=>mode,"items"=>items}<-datado
98+
Enum.each(items,fnitem->
99+
validate_with("#{block}(#{mode})",item_schema,item)
100+
end)
90101

91-
_->
92-
{:ok,:pass}
102+
{:ok,:pass}
93103
end
94-
95-
%{"mode"=>mode,"items"=>items}=data
96-
97-
Enum.each(items,fnitem->
98-
caseSchema.cast(item_schema,item)do
99-
{:error,errors}->
100-
{:error,message}=format_parse_error("#{block}(#{mode})",errors)
101-
raise%MatchError{term:{:error,message}}
102-
103-
_->
104-
{:ok,:pass}
105-
end
106-
end)
107-
108-
{:ok,:pass}
109104
end
110105

111106
defpformat_parse_error(type,error_list)whenis_list(error_list)do

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
3131
],
3232
"version"=>"2.15.0"
3333
}
34-
@tag:wip
34+
@tag:wip2
3535
test"valid list parse should work"do
3636
{:ok,editor_string}=Jason.encode(@editor_json)
3737
# assert {:ok, converted} = Parser.to_html(editor_string)
@@ -52,7 +52,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
5252
],
5353
"version"=>"2.15.0"
5454
}
55-
@tag:wip2
55+
@tag:wip
5656
test"invalid list mode parse should raise error message"do
5757
{:ok,editor_string}=Jason.encode(@editor_json)
5858
{:error,err_msg}=Parser.to_html(editor_string)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp