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

Commit 1c6672c

Browse files
committed
refactor(editor-validator): more spec on invalid schema opt error hint
1 parent 2a7293e commit 1c6672c

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

lib/helper/validator/schema.ex

Lines changed: 10 additions & 7 deletions
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)
@@ -149,7 +152,7 @@ defmodule Helper.Validator.Schema do
149152
# error for option
150153
defp match(field, value, type, [option]) when is_tuple(option) do
151154
# 如果这里不判断的话会和下面的 match 冲突,是否有更好的写法?
152-
case option_valid?(option) do
155+
case option_valid?(type, option) do
153156
true ->
154157
error(field, value, type)
155158

test/helper/validator/schema_test.exs

Lines changed: 17 additions & 18 deletions
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,32 +24,32 @@ 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
5353

5454
schema = %{"text" => [:string, allow_empty: false]}
5555
data = %{"text" => ""}
@@ -149,8 +149,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
149149
data = %{"text" => [1, 2, 3]}
150150
{:error, error} = Schema.cast(schema, data)
151151

152-
assert error ==
153-
[%{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
154153

155154
schema = %{"text" => [:list, allow_empty: false]}
156155
data = %{"text" => []}
@@ -169,7 +168,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
169168
schema = %{"text" => [:boolean, required: true]}
170169
data = %{"no_exsit" => false}
171170
{:error, error} = Schema.cast(schema, data)
172-
assert error == [%{field: "text", message: "should be: boolean", value: nil}]
171+
assert [%{field: "text", message: "should be: boolean", value: nil}] == error
173172

174173
schema = %{"text" => [:boolean, required: true]}
175174
data = %{"text" => false}
@@ -185,7 +184,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
185184
schema = %{"text" => [enum: [1, 2, 3], required: true]}
186185
data = %{"no_exsit" => false}
187186
{:error, error} = Schema.cast(schema, data)
188-
assert error == [%{field: "text", message: "should be: 1 | 2 | 3"}]
187+
assert [%{field: "text", message: "should be: 1 | 2 | 3"}] == error
189188

190189
schema = %{"text" => [enum: [1, 2, 3]]}
191190
data = %{"text" => 1}
@@ -195,14 +194,14 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
195194
# hello world
196195
end
197196

198-
@tag :wip2
199-
test "schema invalid option test" do
200-
schema = %{"text" => [:string, allow_empty: false]}
201-
data = %{"text" => ""}
202-
# assert {:ok, _} = Schema.cast(schema, data)
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)
203203

204-
IO.inspect(Schema.cast(schema, data), label: "schema result")
205-
#
204+
assert [%{field: "text", message: "unknow option: allow_empty: false", value: 1}] == error
206205
end
207206
end
208207
end

0 commit comments

Comments
 (0)