Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit e8d940e

Browse files
committed
refactor(editor): validate_with logic re-org
1 parent bad9428 commit e8d940e

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
defp validate_editor_fmt(data) do
33-
validate_with("editor", EditorSchema.get("editor"), data)
33+
try do
34+
validate_with("editor", EditorSchema.get("editor"), data)
35+
rescue
36+
e in MatchError ->
37+
format_parse_error(e)
38+
39+
_ ->
40+
format_parse_error()
41+
end
3442
end
3543

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

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

7483
# validate with given schema
7584
defp validate_with(block, schema, data) do
7685
case Schema.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
defp validate_with(block, parent_schema, item_schema, data) do
86-
case Schema.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} <- data do
98+
Enum.each(items, fn item ->
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, fn item ->
98-
case Schema.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
defp format_parse_error(type, error_list) when is_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)