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

Commit ff76a4f

Browse files
authored
refactor(editor-validator): improve error hint (#294)
1 parent f6159a7 commit ff76a4f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ defmodule Helper.Converter.EditorToHTML.Validator do
66
alias Validator.Schema
77
alias Converter.EditorToHTML.Validator.EditorSchema
88

9-
# blocks with no children items
109
@normal_blocks ["header", "paragraph", "quote"]
11-
# blocks with "items" fields
12-
@complex_blocks ["list", "table"]
10+
11+
# blocks with "items" fields (has many children item)
12+
@children_blocks ["list", "table"]
13+
14+
# all the supported blocks
15+
@supported_blocks @normal_blocks ++ @children_blocks
1316

1417
@spec is_valid(map) :: {:error, map} | {:ok, :pass}
1518
def is_valid(data) when is_map(data) do
@@ -60,14 +63,18 @@ defmodule Helper.Converter.EditorToHTML.Validator do
6063

6164
# validate block which has mode and items
6265
defp validate_block(%{"type" => type, "data" => data})
63-
when type in @complex_blocks do
66+
when type in @children_blocks do
6467
[parent: parent_schema, item: item_schema] = EditorSchema.get(type)
6568
validate_with(type, parent_schema, item_schema, data)
6669
end
6770

68-
defp validate_block(%{"type" => type}), do: raise("undown #{type} block")
71+
defp validate_block(%{"type" => type}) do
72+
raise("undown #{type} block, supported blocks: #{@supported_blocks |> Enum.join(" | ")}")
73+
end
6974

70-
defp validate_block(e), do: raise("undown block: #{e}")
75+
defp validate_block(e) do
76+
raise("undown block: #{e}, supported blocks: #{@supported_blocks |> Enum.join(" | ")}")
77+
end
7178

7279
# validate with given schema
7380
defp validate_with(block, schema, data) do

test/helper/converter/editor_to_html_test/index_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do
4444
assert {:ok, _} = Parser.to_html(editor_string)
4545
end
4646

47-
@tag :wip
47+
@tag :wip2
4848
test "invalid editorjs json fmt should raise error" do
4949
editor_json = %{
5050
"invalid_time" => 1_567_250_876_713,
@@ -100,7 +100,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML do
100100
{:ok, editor_string} = Jason.encode(editor_json)
101101
{:error, error} = Parser.to_html(editor_string)
102102

103-
assert error == "undown block: 1"
103+
assert String.contains?(error, "undown block: 1")
104104
end
105105

106106
test "real-world editor.js data should work" do

0 commit comments

Comments
 (0)