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

Commit 39ff354

Browse files
committed
fix(editor): add list to validator workflow as general
1 parent 50d6108 commit 39ff354

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule Helper.Converter.EditorToHTML.Validator do
77

88
# blocks with no children items
99
@simple_blocks ["header", "paragraph"]
10+
# blocks with "mode" and "items" fields
11+
@complex_blocks ["list"]
1012

1113
@valid_list_mode ["checklist", "order_list", "unorder_list"]
1214
@valid_list_label_type ["success", "done", "todo"]
@@ -53,32 +55,11 @@ defmodule Helper.Converter.EditorToHTML.Validator do
5355
validate_with(type, Schema.get(type), data)
5456
end
5557

56-
defp validate_block(%{"type" => "list", "data" => %{"mode" => mode, "items" => items} = data})
57-
when mode in @valid_list_mode and is_list(items) do
58-
# mode_schema = %{mode: [enum: @valid_list_mode]}
59-
# {:ok, _} = ValidateBySchema.cast(mode_schema, data)
60-
61-
item_schema = %{
62-
"checked" => [:boolean],
63-
"hideLabel" => [:boolean],
64-
"label" => [:string],
65-
"labelType" => [enum: @valid_list_label_type],
66-
"indent" => [enum: @valid_list_indent],
67-
"text" => [:string]
68-
}
69-
70-
Enum.each(items, fn item ->
71-
case ValidateBySchema.cast(item_schema, item) do
72-
{:error, errors} ->
73-
{:error, message} = format_parse_error("list(#{mode})", errors)
74-
raise %MatchError{term: {:error, message}}
75-
76-
_ ->
77-
{:ok, :pass}
78-
end
79-
end)
80-
81-
{:ok, :pass}
58+
# validate block which has mode and items
59+
defp validate_block(%{"type" => type, "data" => %{"mode" => mode, "items" => items} = data})
60+
when type in @complex_blocks do
61+
[parent: parent_schema, item: item_schema] = Schema.get(type)
62+
validate_with(type, parent_schema, item_schema, data)
8263
end
8364

8465
defp validate_block(%{"type" => "code"}) do
@@ -107,6 +88,31 @@ defmodule Helper.Converter.EditorToHTML.Validator do
10788
end
10889
end
10990

91+
defp validate_with(block, parent_schema, item_schema, data) do
92+
case ValidateBySchema.cast(parent_schema, data) do
93+
{:error, errors} ->
94+
format_parse_error(block, errors)
95+
96+
_ ->
97+
{:ok, :pass}
98+
end
99+
100+
%{"mode" => mode, "items" => items} = data
101+
102+
Enum.each(items, fn item ->
103+
case ValidateBySchema.cast(item_schema, item) do
104+
{:error, errors} ->
105+
{:error, message} = format_parse_error("#{block}(#{mode})", errors)
106+
raise %MatchError{term: {:error, message}}
107+
108+
_ ->
109+
{:ok, :pass}
110+
end
111+
end)
112+
113+
{:ok, :pass}
114+
end
115+
110116
# check if the given map has the right key-value fmt of the editorjs structure
111117
defp is_valid_editorjs_fmt(map) when is_map(map) do
112118
Map.has_key?(map, "time") and

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
defmodule Helper.Converter.EditorToHTML.Validator.Schema do
22
@moduledoc false
33

4+
# header
45
@valid_header_level [1, 2, 3]
56

7+
# list
8+
@valid_list_mode ["checklist", "order_list", "unorder_list"]
9+
@valid_list_label_type ["success", "done", "todo"]
10+
@valid_list_indent [0, 1, 2, 3, 4]
11+
612
def get("header") do
713
%{
814
"text" => [:string],
@@ -12,8 +18,20 @@ defmodule Helper.Converter.EditorToHTML.Validator.Schema do
1218
}
1319
end
1420

15-
def get("paragraph") do
16-
%{"text" => [:string]}
21+
def get("paragraph"), do: %{"text" => [:string]}
22+
23+
def get("list") do
24+
[
25+
parent: %{"mode" => [enum: @valid_list_mode]},
26+
item: %{
27+
"checked" => [:boolean],
28+
"hideLabel" => [:boolean],
29+
"label" => [:string],
30+
"labelType" => [enum: @valid_list_label_type],
31+
"indent" => [enum: @valid_list_indent],
32+
"text" => [:string]
33+
}
34+
]
1735
end
1836

1937
def get(_) do

0 commit comments

Comments
 (0)