diff --git a/lib/helper/validator/schema.ex b/lib/helper/validator/schema.ex index 7c0c9abfa..d30417937 100644 --- a/lib/helper/validator/schema.ex +++ b/lib/helper/validator/schema.ex @@ -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) @@ -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 @@ -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 -> @@ -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 -> @@ -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 @@ -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) diff --git a/test/helper/converter/editor_to_html_test/image_test.exs b/test/helper/converter/editor_to_html_test/image_test.exs index dbe3792ce..fb46f74a6 100644 --- a/test/helper/converter/editor_to_html_test/image_test.exs +++ b/test/helper/converter/editor_to_html_test/image_test.exs @@ -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", [ @@ -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", [ diff --git a/test/helper/validator/schema_test.exs b/test/helper/validator/schema_test.exs index 5a30a9eca..39ce8a5e6 100644 --- a/test/helper/validator/schema_test.exs +++ b/test/helper/validator/schema_test.exs @@ -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"} @@ -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} @@ -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} @@ -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"} @@ -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" => []} @@ -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} @@ -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} @@ -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} @@ -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