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

Commit bad9428

Browse files
committed
refactor(editor): adjust parent schema cast logic
1 parent 23085c8 commit bad9428

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
3030

3131
def get("list") do
3232
[
33-
parent: %{"mode" => [enum: @valid_list_mode]},
33+
parent: %{"mode" => [enum: @valid_list_mode], "items" => [:list]},
3434
item: %{
3535
"checked" => [:boolean],
3636
"hideLabel" => [:boolean],

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
5050
end
5151

5252
# validate block which has mode and items
53-
defp validate_block(%{"type" => type, "data" => %{"mode" => _, "items" => _} = data})
53+
defp validate_block(%{"type" => type, "data" => data})
5454
when type in @complex_blocks do
5555
[parent: parent_schema, item: item_schema] = EditorSchema.get(type)
5656
validate_with(type, parent_schema, item_schema, data)
@@ -85,7 +85,8 @@ defmodule Helper.Converter.EditorToHTML.Validator do
8585
defp validate_with(block, parent_schema, item_schema, data) do
8686
case Schema.cast(parent_schema, data) do
8787
{:error, errors} ->
88-
format_parse_error(block, errors)
88+
{:error, message} = format_parse_error(block, errors)
89+
raise %MatchError{term: {:error, message}}
8990

9091
_ ->
9192
{:ok, :pass}

test/helper/converter/editor_to_html_test/header_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Header do
124124
}\">footer title content</div>\n</div>\n<div>"
125125
end
126126

127-
@tag :wip2
127+
@tag :wip
128128
test "wrong header format data should have invalid hint" do
129129
json =
130130
Map.merge(@editor_json, %{

test/helper/converter/editor_to_html_test/index_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do
4141
assert {:ok, _} = Parser.to_html(editor_string)
4242
end
4343

44-
@tag :wip2
44+
@tag :wip
4545
test "invalid editorjs json fmt should raise error" do
4646
editor_json = %{
4747
"invalid_time" => 1_567_250_876_713,

test/helper/converter/editor_to_html_test/list_test.exs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
3939
IO.inspect(converted, label: "list converted")
4040
end
4141

42+
@editor_json %{
43+
"time" => 1_567_250_876_713,
44+
"blocks" => [
45+
%{
46+
"type" => "list",
47+
"data" => %{
48+
"invalid-mode" => "",
49+
"items" => []
50+
}
51+
}
52+
],
53+
"version" => "2.15.0"
54+
}
55+
@tag :wip2
56+
test "invalid list mode parse should raise error message" do
57+
{:ok, editor_string} = Jason.encode(@editor_json)
58+
{:error, err_msg} = Parser.to_html(editor_string)
59+
60+
assert err_msg == [
61+
%{
62+
block: "list",
63+
field: "mode",
64+
message: "should be: checklist | order_list | unorder_list"
65+
}
66+
]
67+
end
68+
4269
@editor_json %{
4370
"time" => 1_567_250_876_713,
4471
"blocks" => [
@@ -61,7 +88,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
6188
],
6289
"version" => "2.15.0"
6390
}
64-
@tag :wip2
91+
@tag :wip
6592
test "invalid list data parse should raise error message" do
6693
{:ok, editor_string} = Jason.encode(@editor_json)
6794
{:error, err_msg} = Parser.to_html(editor_string)

0 commit comments

Comments
 (0)