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

Commit 50d6108

Browse files
committed
fix(editor): re-org validate workflow by using config
1 parent 0ea6c34 commit 50d6108

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ defmodule Helper.Converter.EditorToHTML.Validator do
33

44
alias Helper.{Utils, ValidateBySchema}
55

6-
@valid_header_level [1, 2, 3]
6+
alias Helper.Converter.EditorToHTML.Validator.Schema
7+
8+
# blocks with no children items
9+
@simple_blocks ["header", "paragraph"]
710

811
@valid_list_mode ["checklist", "order_list", "unorder_list"]
912
@valid_list_label_type ["success", "done", "todo"]
@@ -45,33 +48,9 @@ defmodule Helper.Converter.EditorToHTML.Validator do
4548
{:ok, :pass}
4649
end
4750

48-
defp validate_block(%{"type" => "paragraph", "data" => %{"text" => text} = data}) do
49-
schema = %{"text" => [:string]}
50-
51-
case ValidateBySchema.cast(schema, data) do
52-
{:error, errors} ->
53-
format_parse_error("paragraph", errors)
54-
55-
_ ->
56-
{:ok, :pass}
57-
end
58-
end
59-
60-
defp validate_block(%{"type" => "header", "data" => %{"text" => text, "level" => level} = data}) do
61-
schema = %{
62-
"text" => [:string],
63-
"level" => [enum: @valid_header_level],
64-
"eyebrowTitle" => [:string, required: false],
65-
"footerTitle" => [:string, required: false]
66-
}
67-
68-
case ValidateBySchema.cast(schema, data) do
69-
{:error, errors} ->
70-
format_parse_error("header", errors)
71-
72-
_ ->
73-
{:ok, :pass}
74-
end
51+
# validate block which have no nested items
52+
defp validate_block(%{"type" => type, "data" => data}) when type in @simple_blocks do
53+
validate_with(type, Schema.get(type), data)
7554
end
7655

7756
defp validate_block(%{"type" => "list", "data" => %{"mode" => mode, "items" => items} = data})
@@ -117,6 +96,17 @@ defmodule Helper.Converter.EditorToHTML.Validator do
11796
defp validate_block(%{"type" => type}), do: raise("undown #{type} block")
11897
defp validate_block(_), do: raise("undown block")
11998

99+
# validate with given schema
100+
defp validate_with(block, schema, data) do
101+
case ValidateBySchema.cast(schema, data) do
102+
{:error, errors} ->
103+
format_parse_error(block, errors)
104+
105+
_ ->
106+
{:ok, :pass}
107+
end
108+
end
109+
120110
# check if the given map has the right key-value fmt of the editorjs structure
121111
defp is_valid_editorjs_fmt(map) when is_map(map) do
122112
Map.has_key?(map, "time") and
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule Helper.Converter.EditorToHTML.Validator.Schema do
2+
@moduledoc false
3+
4+
@valid_header_level [1, 2, 3]
5+
6+
def get("header") do
7+
%{
8+
"text" => [:string],
9+
"level" => [enum: @valid_header_level],
10+
"eyebrowTitle" => [:string, required: false],
11+
"footerTitle" => [:string, required: false]
12+
}
13+
end
14+
15+
def get("paragraph") do
16+
%{"text" => [:string]}
17+
end
18+
19+
def get(_) do
20+
%{}
21+
end
22+
end

test/helper/converter/editor_to_html_test/list_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
7373
],
7474
"version" => "2.15.0"
7575
}
76-
@tag :wip2
76+
@tag :wip
7777
test "valid list parse should work" do
7878
{:ok, editor_string} = Jason.encode(@editor_json)
7979
assert {:ok, _} = Parser.to_html(editor_string)
@@ -101,7 +101,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
101101
],
102102
"version" => "2.15.0"
103103
}
104-
@tag :wip2
104+
@tag :wip
105105
test "invalid indent field should get error" do
106106
{:ok, editor_string} = Jason.encode(@editor_json)
107107
{:error, error} = Parser.to_html(editor_string)

0 commit comments

Comments
 (0)