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

Commit d035a4b

Browse files
authored
feat(editor-validator): more spec error hint on invalid schema options (#300)
* feat(editor-validator): add allow_empty to string * refactor(editor-validator): more spec on invalid schema opt error hint
1 parent f60bf10 commit d035a4b

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

lib/helper/validator/schema.ex

+24-18
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ defmodule Helper.Validator.Schema do
5656
end)
5757
end
5858

59-
defp option_valid?({:min, v}) when is_integer(v), do: true
60-
defp option_valid?({:required, v}) when is_boolean(v), do: true
61-
defp option_valid?({:starts_with, v}) when is_binary(v), do: true
62-
defp option_valid?({:type, :map}), do: true
63-
defp option_valid?({:allow_empty, v}) when is_boolean(v), do: true
59+
defp option_valid?(:string, {:min, v}) when is_integer(v), do: true
60+
defp option_valid?(:number, {:min, v}) when is_integer(v), do: true
6461

65-
defp option_valid?(_), do: false
62+
defp option_valid?(_, {:required, v}) when is_boolean(v), do: true
63+
defp option_valid?(:string, {:starts_with, v}) when is_binary(v), do: true
64+
defp option_valid?(:list, {:type, :map}), do: true
65+
defp option_valid?(:string, {:allow_empty, v}) when is_boolean(v), do: true
66+
defp option_valid?(:list, {:allow_empty, v}) when is_boolean(v), do: true
67+
68+
defp option_valid?(_, _), do: false
6669

6770
defp match(field, nil, enum: _, required: false), do: done(field, nil)
6871
defp match(field, value, enum: enum, required: _), do: match(field, value, enum: enum)
@@ -86,7 +89,7 @@ defmodule Helper.Validator.Schema do
8689
end
8790

8891
# custom validate logic
89-
# min option for @support_min types
92+
## min option for @support_min types
9093
defp match(field, value, type, [{:min, min} | options])
9194
when type in @support_min and g_not_nil(value) and g_pos_int(min) do
9295
case Utils.large_than(value, min) do
@@ -98,7 +101,7 @@ defmodule Helper.Validator.Schema do
98101
end
99102
end
100103

101-
# starts_with option for string
104+
## starts_with option for string
102105
defp match(field, value, type, [{:starts_with, starts} | options]) when is_binary(value) do
103106
case String.starts_with?(value, starts) do
104107
true ->
@@ -109,7 +112,7 @@ defmodule Helper.Validator.Schema do
109112
end
110113
end
111114

112-
# item type for list
115+
## item type for list
113116
defp match(field, value, type, [{:type, :map} | options]) when is_list(value) do
114117
case Enum.all?(value, &is_map(&1)) do
115118
true ->
@@ -120,17 +123,20 @@ defmodule Helper.Validator.Schema do
120123
end
121124
end
122125

123-
defp match(field, value, type, [{:allow_empty, false} | options]) when is_list(value) do
124-
case length(value) do
125-
0 ->
126-
error(field, value, :allow_empty)
126+
# allow empty for list
127+
defp match(field, value, _type, [{:allow_empty, false} | _options])
128+
when is_list(value) and value == [] do
129+
error(field, value, :allow_empty)
130+
end
127131

128-
_ ->
129-
match(field, value, type, options)
130-
end
132+
# allow empty for string
133+
defp match(field, value, _type, [{:allow_empty, false} | _options])
134+
when is_binary(value) and byte_size(value) == 0 do
135+
error(field, value, :allow_empty)
131136
end
132137

133-
defp match(field, value, type, [{:allow_empty, true} | options]) when is_list(value) do
138+
defp match(field, value, type, [{:allow_empty, _} | options])
139+
when is_binary(value) or is_list(value) do
134140
match(field, value, type, options)
135141
end
136142

@@ -146,7 +152,7 @@ defmodule Helper.Validator.Schema do
146152
# error for option
147153
defp match(field, value, type, [option]) when is_tuple(option) do
148154
# 如果这里不判断的话会和下面的 match 冲突,是否有更好的写法?
149-
case option_valid?(option) do
155+
case option_valid?(type, option) do
150156
true ->
151157
error(field, value, type)
152158

test/helper/converter/editor_to_html_test/image_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
208208
] == err_msg
209209
end
210210

211-
@tag :wip2
211+
@tag :wip
212212
test "invalid data parse should raise error message" do
213213
editor_json =
214214
set_items("single", [
@@ -231,7 +231,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
231231
]
232232
end
233233

234-
@tag :wip2
234+
@tag :wip
235235
test "src should starts with https://" do
236236
editor_json =
237237
set_items("single", [

test/helper/validator/schema_test.exs

+31-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
1515
schema = %{"text" => [:string, required: true]}
1616
data = %{"no_exsit" => "text"}
1717
{:error, error} = Schema.cast(schema, data)
18-
assert error == [%{field: "text", message: "should be: string", value: nil}]
18+
assert [%{field: "text", message: "should be: string", value: nil}] == error
1919

2020
schema = %{"text" => [:string, required: true]}
2121
data = %{"text" => "text"}
@@ -24,36 +24,42 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
2424
schema = %{"text" => [:string, min: 5]}
2525
data = %{"text" => "text"}
2626
{:error, error} = Schema.cast(schema, data)
27-
assert error == [%{field: "text", message: "min size: 5", value: "text"}]
27+
assert [%{field: "text", message: "min size: 5", value: "text"}] == error
2828

2929
schema = %{"text" => [:string, required: false, min: 5]}
3030
data = %{"text" => "text"}
3131
{:error, error} = Schema.cast(schema, data)
32-
assert error == [%{field: "text", message: "min size: 5", value: "text"}]
32+
assert [%{field: "text", message: "min size: 5", value: "text"}] === error
3333

3434
schema = %{"text" => [:string, min: 5]}
3535
data = %{"no_exsit" => "text"}
3636
{:error, error} = Schema.cast(schema, data)
37-
assert error == [%{field: "text", message: "should be: string", value: nil}]
37+
assert [%{field: "text", message: "should be: string", value: nil}] == error
3838

3939
schema = %{"text" => [:string, required: true, min: 5]}
4040
data = %{"no_exsit" => "text"}
4141
{:error, error} = Schema.cast(schema, data)
42-
assert error == [%{field: "text", message: "should be: string", value: nil}]
42+
assert [%{field: "text", message: "should be: string", value: nil}] == error
4343

4444
schema = %{"text" => [:string, required: true, min: "5"]}
4545
data = %{"text" => "text"}
4646
{:error, error} = Schema.cast(schema, data)
47-
assert error == [%{field: "text", message: "unknow option: min: 5", value: "text"}]
47+
assert [%{field: "text", message: "unknow option: min: 5", value: "text"}] == error
4848

4949
schema = %{"text" => [:string, starts_with: "https://"]}
5050
data = %{"text" => "text"}
5151
assert {:error, error} = Schema.cast(schema, data)
52-
assert error == [%{field: "text", message: "should starts with: https://", value: "text"}]
52+
assert [%{field: "text", message: "should starts with: https://", value: "text"}] == error
53+
54+
schema = %{"text" => [:string, allow_empty: false]}
55+
data = %{"text" => ""}
56+
assert {:error, error} = Schema.cast(schema, data)
57+
assert [%{field: "text", message: "empty is not allowed", value: ""}] == error
58+
5359
# IO.inspect(Schema.cast(schema, data), label: "schema result")
5460
end
5561

56-
@tag :wip2
62+
@tag :wip
5763
test "number with options" do
5864
schema = %{"text" => [:number, required: false]}
5965
data = %{"no_exsit" => 1}
@@ -92,7 +98,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
9298
# hello world
9399
end
94100

95-
@tag :wip2
101+
@tag :wip
96102
test "number with wrong option" do
97103
schema = %{"text" => [:number, required: true, min: "5"]}
98104
data = %{"text" => 1}
@@ -107,7 +113,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
107113
assert error == [%{field: "text", message: "unknow option: no_exsit_option: xxx", value: 1}]
108114
end
109115

110-
@tag :wip2
116+
@tag :wip
111117
test "number with options edage case" do
112118
schema = %{"text" => [:number, min: 2]}
113119
data = %{"text" => "aa"}
@@ -143,8 +149,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
143149
data = %{"text" => [1, 2, 3]}
144150
{:error, error} = Schema.cast(schema, data)
145151

146-
assert error ==
147-
[%{field: "text", message: "item should be map", value: [1, 2, 3]}]
152+
assert [%{field: "text", message: "item should be map", value: [1, 2, 3]}] == error
148153

149154
schema = %{"text" => [:list, allow_empty: false]}
150155
data = %{"text" => []}
@@ -154,7 +159,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
154159
# IO.inspect(Schema.cast(schema, data), label: "schema result")
155160
end
156161

157-
@tag :wip2
162+
@tag :wip
158163
test "boolean with options" do
159164
schema = %{"text" => [:boolean, required: false]}
160165
data = %{"no_exsit" => false}
@@ -163,14 +168,14 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
163168
schema = %{"text" => [:boolean, required: true]}
164169
data = %{"no_exsit" => false}
165170
{:error, error} = Schema.cast(schema, data)
166-
assert error == [%{field: "text", message: "should be: boolean", value: nil}]
171+
assert [%{field: "text", message: "should be: boolean", value: nil}] == error
167172

168173
schema = %{"text" => [:boolean, required: true]}
169174
data = %{"text" => false}
170175
assert {:ok, _} = Schema.cast(schema, data)
171176
end
172177

173-
@tag :wip2
178+
@tag :wip
174179
test "enum with options" do
175180
schema = %{"text" => [enum: [1, 2, 3], required: false]}
176181
data = %{"no_exsit" => false}
@@ -179,7 +184,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
179184
schema = %{"text" => [enum: [1, 2, 3], required: true]}
180185
data = %{"no_exsit" => false}
181186
{:error, error} = Schema.cast(schema, data)
182-
assert error == [%{field: "text", message: "should be: 1 | 2 | 3"}]
187+
assert [%{field: "text", message: "should be: 1 | 2 | 3"}] == error
183188

184189
schema = %{"text" => [enum: [1, 2, 3]]}
185190
data = %{"text" => 1}
@@ -188,5 +193,15 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
188193
# IO.inspect(Schema.cast(schema, data), label: "schema result")
189194
# hello world
190195
end
196+
197+
@tag :wip
198+
test "schema invalid option should got error" do
199+
schema = %{"text" => [:number, allow_empty: false]}
200+
data = %{"text" => 1}
201+
202+
{:error, error} = Schema.cast(schema, data)
203+
204+
assert [%{field: "text", message: "unknow option: allow_empty: false", value: 1}] == error
205+
end
191206
end
192207
end

0 commit comments

Comments
 (0)