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

Commit 23085c8

Browse files
committed
refactor(editor): re-org / rename the schema validator
1 parent 89c2e2a commit 23085c8

File tree

5 files changed

+47
-57
lines changed

5 files changed

+47
-57
lines changed

lib/helper/converter/editor_to_html/validator/schema.ex renamed to lib/helper/converter/editor_to_html/validator/editor_schema.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Helper.Converter.EditorToHTML.Validator.Schema do
1+
defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
22
@moduledoc false
33

44
# header

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
defmodule Helper.Converter.EditorToHTML.Validator do
22
@moduledoc false
33

4-
alias Helper.{Utils, ValidateBySchema}
4+
alias Helper.{Converter, Validator}
55

6-
alias Helper.Converter.EditorToHTML.Validator.Schema
6+
alias Validator.Schema
7+
alias Converter.EditorToHTML.Validator.EditorSchema
78

89
# blocks with no children items
910
@simple_blocks ["header", "paragraph"]
@@ -22,14 +23,14 @@ defmodule Helper.Converter.EditorToHTML.Validator do
2223
e in RuntimeError ->
2324
format_parse_error(e)
2425

25-
e ->
26+
_ ->
2627
format_parse_error()
2728
end
2829
end
2930
end
3031

3132
defp validate_editor_fmt(data) do
32-
validate_with("editor", Schema.get("editor"), data)
33+
validate_with("editor", EditorSchema.get("editor"), data)
3334
end
3435

3536
defp validate_blocks([]), do: {:ok, :pass}
@@ -45,19 +46,19 @@ defmodule Helper.Converter.EditorToHTML.Validator do
4546

4647
# validate block which have no nested items
4748
defp validate_block(%{"type" => type, "data" => data}) when type in @simple_blocks do
48-
validate_with(type, Schema.get(type), data)
49+
validate_with(type, EditorSchema.get(type), data)
4950
end
5051

5152
# validate block which has mode and items
52-
defp validate_block(%{"type" => type, "data" => %{"mode" => mode, "items" => items} = data})
53+
defp validate_block(%{"type" => type, "data" => %{"mode" => _, "items" => _} = data})
5354
when type in @complex_blocks do
54-
[parent: parent_schema, item: item_schema] = Schema.get(type)
55+
[parent: parent_schema, item: item_schema] = EditorSchema.get(type)
5556
validate_with(type, parent_schema, item_schema, data)
5657
end
5758

5859
defp validate_block(%{"type" => "code"}) do
5960
# schema = %{text: [:string]}
60-
# case ValidateBySchema.cast(schema, data) do
61+
# case Schema.cast(schema, data) do
6162
# {:error, errors} ->
6263
# format_parse_error("paragraph", errors)
6364

@@ -72,7 +73,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
7273

7374
# validate with given schema
7475
defp validate_with(block, schema, data) do
75-
case ValidateBySchema.cast(schema, data) do
76+
case Schema.cast(schema, data) do
7677
{:error, errors} ->
7778
format_parse_error(block, errors)
7879

@@ -82,7 +83,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
8283
end
8384

8485
defp validate_with(block, parent_schema, item_schema, data) do
85-
case ValidateBySchema.cast(parent_schema, data) do
86+
case Schema.cast(parent_schema, data) do
8687
{:error, errors} ->
8788
format_parse_error(block, errors)
8889

@@ -93,7 +94,7 @@ defmodule Helper.Converter.EditorToHTML.Validator do
9394
%{"mode" => mode, "items" => items} = data
9495

9596
Enum.each(items, fn item ->
96-
case ValidateBySchema.cast(item_schema, item) do
97+
case Schema.cast(item_schema, item) do
9798
{:error, errors} ->
9899
{:error, message} = format_parse_error("#{block}(#{mode})", errors)
99100
raise %MatchError{term: {:error, message}}
@@ -106,16 +107,6 @@ defmodule Helper.Converter.EditorToHTML.Validator do
106107
{:ok, :pass}
107108
end
108109

109-
# check if the given map has the right key-value fmt of the editorjs structure
110-
defp is_valid_editorjs_fmt(map) when is_map(map) do
111-
Map.has_key?(map, "time") and
112-
Map.has_key?(map, "version") and
113-
Map.has_key?(map, "blocks") and
114-
is_list(map["blocks"]) and
115-
is_binary(map["version"]) and
116-
is_integer(map["time"])
117-
end
118-
119110
defp format_parse_error(type, error_list) when is_list(error_list) do
120111
{:error,
121112
Enum.map(error_list, fn error ->

lib/helper/validate_by_schema.ex renamed to lib/helper/validator/schema.ex

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
1-
defmodule Helper.ValidateBySchema.Matchers do
2-
@moduledoc """
3-
matchers for basic type, support required option
4-
"""
5-
6-
defmacro __using__(types) do
7-
# can not use Enum.each here, see https://elixirforum.com/t/define-multiple-modules-in-macro-only-last-one-gets-created/1654/4
8-
for type <- types do
9-
guard_name = if type == :string, do: "is_binary", else: "is_#{to_string(type)}"
10-
11-
quote do
12-
defp match(field, nil, [unquote(type), required: false]), do: done(field, nil)
13-
14-
defp match(field, value, [unquote(type), required: false])
15-
when unquote(:"#{guard_name}")(value) do
16-
done(field, value)
17-
end
18-
19-
defp match(field, value, [unquote(type)]) when unquote(:"#{guard_name}")(value),
20-
do: done(field, value)
21-
22-
defp match(field, value, [unquote(type), required: false]),
23-
do: error(field, value, unquote(type))
24-
25-
defp match(field, value, [unquote(type)]), do: error(field, value, unquote(type))
26-
end
27-
end
28-
end
29-
end
30-
31-
defmodule Helper.ValidateBySchema do
1+
defmodule Helper.Validator.Schema do
322
@moduledoc """
333
validate json data by given schema, mostly used in editorjs validator
344
355
currently support boolean / string / number / enum
366
"""
377

8+
use Helper.Validator.Schema.Matchers, [:string, :number, :list, :boolean]
9+
3810
@doc """
3911
cast data by given schema
4012
@@ -49,10 +21,8 @@ defmodule Helper.ValidateBySchema do
4921
}
5022
5123
data = %{checked: true, label: "done"}
52-
ValidateBySchema.cast(schema, data)
24+
Schema.cast(schema, data)
5325
"""
54-
use Helper.ValidateBySchema.Matchers, [:string, :number, :list, :boolean]
55-
5626
def cast(schema, data) do
5727
errors_info = cast_errors(schema, data)
5828

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule Helper.Validator.Schema.Matchers do
2+
@moduledoc """
3+
matchers for basic type, support required option
4+
"""
5+
6+
defmacro __using__(types) do
7+
# can not use Enum.each here, see https://elixirforum.com/t/define-multiple-modules-in-macro-only-last-one-gets-created/1654/4
8+
for type <- types do
9+
guard_name = if type == :string, do: "is_binary", else: "is_#{to_string(type)}"
10+
11+
quote do
12+
defp match(field, nil, [unquote(type), required: false]), do: done(field, nil)
13+
14+
defp match(field, value, [unquote(type), required: false])
15+
when unquote(:"#{guard_name}")(value) do
16+
done(field, value)
17+
end
18+
19+
defp match(field, value, [unquote(type)]) when unquote(:"#{guard_name}")(value),
20+
do: done(field, value)
21+
22+
defp match(field, value, [unquote(type), required: false]),
23+
do: error(field, value, unquote(type))
24+
25+
defp match(field, value, [unquote(type)]), do: error(field, value, unquote(type))
26+
end
27+
end
28+
end
29+
end

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

0 commit comments

Comments
 (0)