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

feat(editor-validator): more spec error hint on invalid schema options #300

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions lib/helper/validator/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ defmodule Helper.Validator.Schema do
end)
end

defp option_valid?({:min, v}) when is_integer(v), do: true
defp option_valid?({:required, v}) when is_boolean(v), do: true
defp option_valid?({:starts_with, v}) when is_binary(v), do: true
defp option_valid?({:type, :map}), do: true
defp option_valid?({:allow_empty, v}) when is_boolean(v), do: true
defp option_valid?(:string, {:min, v}) when is_integer(v), do: true
defp option_valid?(:number, {:min, v}) when is_integer(v), do: true

defp option_valid?(_), do: false
defp option_valid?(_, {:required, v}) when is_boolean(v), do: true
defp option_valid?(:string, {:starts_with, v}) when is_binary(v), do: true
defp option_valid?(:list, {:type, :map}), do: true
defp option_valid?(:string, {:allow_empty, v}) when is_boolean(v), do: true
defp option_valid?(:list, {:allow_empty, v}) when is_boolean(v), do: true

defp option_valid?(_, _), do: false

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

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

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

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

defp match(field, value, type, [{:allow_empty, false} | options]) when is_list(value) do
case length(value) do
0 ->
error(field, value, :allow_empty)
# allow empty for list
defp match(field, value, _type, [{:allow_empty, false} | _options])
when is_list(value) and value == [] do
error(field, value, :allow_empty)
end

_ ->
match(field, value, type, options)
end
# allow empty for string
defp match(field, value, _type, [{:allow_empty, false} | _options])
when is_binary(value) and byte_size(value) == 0 do
error(field, value, :allow_empty)
end

defp match(field, value, type, [{:allow_empty, true} | options]) when is_list(value) do
defp match(field, value, type, [{:allow_empty, _} | options])
when is_binary(value) or is_list(value) do
match(field, value, type, options)
end

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

Expand Down
4 changes: 2 additions & 2 deletions test/helper/converter/editor_to_html_test/image_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
] == err_msg
end

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

@tag :wip2
@tag :wip
test "src should starts with https://" do
editor_json =
set_items("single", [
Expand Down
47 changes: 31 additions & 16 deletions test/helper/validator/schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
schema = %{"text" => [:string, required: true]}
data = %{"no_exsit" => "text"}
{:error, error} = Schema.cast(schema, data)
assert error == [%{field: "text", message: "should be: string", value: nil}]
assert [%{field: "text", message: "should be: string", value: nil}] == error

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

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

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

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

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

schema = %{"text" => [:string, starts_with: "https://"]}
data = %{"text" => "text"}
assert {:error, error} = Schema.cast(schema, data)
assert error == [%{field: "text", message: "should starts with: https://", value: "text"}]
assert [%{field: "text", message: "should starts with: https://", value: "text"}] == error

schema = %{"text" => [:string, allow_empty: false]}
data = %{"text" => ""}
assert {:error, error} = Schema.cast(schema, data)
assert [%{field: "text", message: "empty is not allowed", value: ""}] == error

# IO.inspect(Schema.cast(schema, data), label: "schema result")
end

@tag :wip2
@tag :wip
test "number with options" do
schema = %{"text" => [:number, required: false]}
data = %{"no_exsit" => 1}
Expand Down Expand Up @@ -92,7 +98,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
# hello world
end

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

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

assert error ==
[%{field: "text", message: "item should be map", value: [1, 2, 3]}]
assert [%{field: "text", message: "item should be map", value: [1, 2, 3]}] == error

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

@tag :wip2
@tag :wip
test "boolean with options" do
schema = %{"text" => [:boolean, required: false]}
data = %{"no_exsit" => false}
Expand All @@ -163,14 +168,14 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
schema = %{"text" => [:boolean, required: true]}
data = %{"no_exsit" => false}
{:error, error} = Schema.cast(schema, data)
assert error == [%{field: "text", message: "should be: boolean", value: nil}]
assert [%{field: "text", message: "should be: boolean", value: nil}] == error

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

@tag :wip2
@tag :wip
test "enum with options" do
schema = %{"text" => [enum: [1, 2, 3], required: false]}
data = %{"no_exsit" => false}
Expand All @@ -179,7 +184,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
schema = %{"text" => [enum: [1, 2, 3], required: true]}
data = %{"no_exsit" => false}
{:error, error} = Schema.cast(schema, data)
assert error == [%{field: "text", message: "should be: 1 | 2 | 3"}]
assert [%{field: "text", message: "should be: 1 | 2 | 3"}] == error

schema = %{"text" => [enum: [1, 2, 3]]}
data = %{"text" => 1}
Expand All @@ -188,5 +193,15 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
# IO.inspect(Schema.cast(schema, data), label: "schema result")
# hello world
end

@tag :wip
test "schema invalid option should got error" do
schema = %{"text" => [:number, allow_empty: false]}
data = %{"text" => 1}

{:error, error} = Schema.cast(schema, data)

assert [%{field: "text", message: "unknow option: allow_empty: false", value: 1}] == error
end
end
end