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

Commit bfc5688

Browse files
committed
fix(editor-export): schema eadge case
1 parent eb76895 commit bfc5688

File tree

6 files changed

+51
-31
lines changed

6 files changed

+51
-31
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
@@ -48,7 +48,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
4848

4949
def get("table") do
5050
[
51-
parent: %{"columnCount" => [:number], "items" => [:list]},
51+
parent: %{"columnCount" => [:number, min: 2], "items" => [:list]},
5252
item: %{
5353
"text" => [:string],
5454
"align" => [enum: @valid_table_align],

lib/helper/validator/schema.ex

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,36 +107,26 @@ defmodule Helper.Validator.Schema do
107107
# custom validate logic end
108108

109109
# main type
110-
defp match(field, value, :string, []) when is_binary(value) do
111-
done(field, value)
112-
end
113-
114-
defp match(field, value, :number, []) when is_integer(value) do
115-
done(field, value)
116-
end
117-
118-
defp match(field, value, :list, []) when is_list(value) do
119-
done(field, value)
120-
end
121-
122-
defp match(field, value, :boolean, []) when is_boolean(value) do
123-
done(field, value)
124-
end
125-
110+
defp match(field, value, :string, []) when is_binary(value), do: done(field, value)
111+
defp match(field, value, :number, []) when is_integer(value), do: done(field, value)
112+
defp match(field, value, :list, []) when is_list(value), do: done(field, value)
113+
defp match(field, value, :boolean, []) when is_boolean(value), do: done(field, value)
126114
# main type end
127115

128-
defp match(field, value, _type, [option]) when is_tuple(option) and not is_nil(value) do
129-
{k, v} = option
130-
error(field, value, option: "#{to_string(k)}: #{to_string(v)}")
131-
end
116+
# judge option
117+
defp match(field, value, type, [option]) when is_tuple(option) do
118+
# 如果这里不判断的话会和下面的 match 冲突,是否有更好的写法?
119+
case option_valid?(option) do
120+
true ->
121+
error(field, value, type)
132122

133-
defp match(field, value, _type, [_option]) when not is_nil(value) do
134-
error(field, value, :option)
123+
false ->
124+
{k, v} = option
125+
error(field, value, option: "#{to_string(k)}: #{to_string(v)}")
126+
end
135127
end
136128

137-
defp match(field, value, type, _) do
138-
error(field, value, type)
139-
end
129+
defp match(field, value, type, _), do: error(field, value, type)
140130

141131
defp done(field, value), do: {:ok, %{field: field, value: value}}
142132

@@ -155,4 +145,8 @@ defmodule Helper.Validator.Schema do
155145
defp error(field, value, schema) do
156146
{:error, %{field: field |> to_string, value: value, message: "should be: #{schema}"}}
157147
end
148+
149+
defp option_valid?({:min, v}) when is_integer(v), do: true
150+
defp option_valid?({:required, v}) when is_boolean(v), do: true
151+
defp option_valid?(_), do: false
158152
end

test/helper/converter/editor_to_html_test/header_test.exs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,6 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Header do
127127
{:ok, editor_string} = Jason.encode(json)
128128
{:ok, converted} = Parser.to_html(editor_string)
129129

130-
IO.inspect(converted, label: "converted --")
131-
132130
assert Utils.str_occurence(converted, @eyebrow_class) == 0
133131
assert Utils.str_occurence(converted, @footer_class) == 1
134132
end

test/helper/converter/editor_to_html_test/list_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
1212

1313
describe "[list block unit]" do
1414
defp set_items(mode, items) do
15-
editor_json = %{
15+
%{
1616
"time" => 1_567_250_876_713,
1717
"blocks" => [
1818
%{

test/helper/converter/editor_to_html_test/table_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Table do
119119
},
120120
%{block: "table", field: "items", message: "should be: list", value: "bb"}
121121
]
122+
123+
editor_json = set_items(-2, "bb")
124+
{:ok, editor_string} = Jason.encode(editor_json)
125+
{:error, err_msg} = Parser.to_html(editor_string)
126+
127+
assert err_msg == [
128+
%{block: "table", field: "columnCount", message: "min size: 2", value: -2},
129+
%{block: "table", field: "items", message: "should be: list", value: "bb"}
130+
]
122131
end
123132
end
124133
end

test/helper/validator/schema_test.exs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,32 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
8383
{:error, error} = Schema.cast(schema, data)
8484
assert error == [%{field: "text", message: "should be: number", value: nil}]
8585

86+
# IO.inspect(Schema.cast(schema, data), label: "schema result")
87+
# hello world
88+
end
89+
90+
@tag :wip
91+
test "number with wrong option" do
8692
schema = %{"text" => [:number, required: true, min: "5"]}
8793
data = %{"text" => 1}
94+
8895
{:error, error} = Schema.cast(schema, data)
8996
assert error == [%{field: "text", message: "unknow option: min: 5", value: 1}]
9097

91-
# IO.inspect(Schema.cast(schema, data), label: "schema result")
92-
# hello world
98+
schema = %{"text" => [:number, required: true, no_exsit_option: "xxx"]}
99+
data = %{"text" => 1}
100+
101+
{:error, error} = Schema.cast(schema, data)
102+
assert error == [%{field: "text", message: "unknow option: no_exsit_option: xxx", value: 1}]
103+
end
104+
105+
@tag :wip
106+
test "number with options edage case" do
107+
schema = %{"text" => [:number, min: 2]}
108+
data = %{"text" => "aa"}
109+
110+
{:error, error} = Schema.cast(schema, data)
111+
assert error == [%{field: "text", message: "should be: number", value: "aa"}]
93112
end
94113

95114
@tag :wip

0 commit comments

Comments
 (0)