Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

feat(editor-schema): add new option#299

Merged
mydearxym merged 2 commits intodevfromeditor-validator-new-opt
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -58,7 +58,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
parent: %{
"id" => [:string, required: false],
"mode" => [enum: @valid_list_mode],
"items" => [:list]
"items" => [:list, type: :map, allow_empty: false]
},
item: %{
"checked" => [:boolean],
Expand All@@ -77,7 +77,7 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
parent: %{
"id" => [:string, required: false],
"columnCount" => [:number, min: 2],
"items" => [:list]
"items" => [:list, type: :map, allow_empty: false]
},
item: %{
"text" => [:string],
Expand All@@ -94,10 +94,10 @@ defmodule Helper.Converter.EditorToHTML.Validator.EditorSchema do
parent: %{
"id" => [:string, required: false],
"mode" => [enum: @valid_image_mode],
"items" => [:list]
"items" => [:list, type: :map, allow_empty: false]
},
item: %{
"src" => [:string],
"src" => [:string, starts_with: "https://"],
"index" => [:number],
"caption" => [:string, required: false],
"height" => [:string, required: false],
Expand Down
60 changes: 58 additions & 2 deletionslib/helper/validator/schema.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -58,6 +58,10 @@ defmodule Helper.Validator.Schema do

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?(_), do: false

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

# custom validate logic
# 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@@ -93,6 +98,42 @@ defmodule Helper.Validator.Schema do
end
end

# 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 ->
match(field, value, type, options)

false ->
error(field, value, :starts_with, starts)
end
end

# 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 ->
match(field, value, type, options)

false ->
error(field, value, :list_type_map)
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)

_ ->
match(field, value, type, options)
end
end

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

# custom validate logic end

# main type
Expand DownExpand Up@@ -120,10 +161,25 @@ defmodule Helper.Validator.Schema do

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

defp error(field, value, :min, min) do
{:error, %{field: field |> to_string, value: value, message: "min size: #{min}"}}
# custom error hint
defp error(field, value, :min, expect) do
{:error, %{field: field |> to_string, value: value, message: "min size: #{expect}"}}
end

defp error(field, value, :starts_with, expect) do
{:error, %{field: field |> to_string, value: value, message: "should starts with: #{expect}"}}
end

defp error(field, value, :list_type_map) do
{:error, %{field: field |> to_string, value: value, message: "item should be map"}}
end

defp error(field, value, :allow_empty) do
{:error, %{field: field |> to_string, value: value, message: "empty is not allowed"}}
end

# custom error hint end

defp error(field, value, option: option) do
{:error, %{field: field |> to_string, value: value, message: "unknow option: #{option}"}}
end
Expand Down
59 changes: 30 additions & 29 deletionstest/helper/converter/editor_to_html_test/image_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -117,7 +117,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
assert Utils.str_occurence(converted, image_caption_class) == 0
end

@tag :wip2
@tag :wip
test "jiugongge image parse should work" do
editor_json =
set_items(
Expand All@@ -143,7 +143,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
assert Utils.str_occurence(converted, jiugongge_image_class) == length(@images)
end

@tag :wip2
@tag :wip
test "gallery image parse should work" do
editor_json =
set_items(
Expand DownExpand Up@@ -192,19 +192,20 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
assert Utils.str_occurence(converted, "id=\"exsit\"") == 1
end

@tag :wip2
@tag :wip
test "invalid mode parse should raise error message" do
editor_json = set_items("invalid-mode", [])
{:ok, editor_string} = Jason.encode(editor_json)
{:error, err_msg} = Parser.to_html(editor_string)

assert err_msg == [
assert [
%{block: "image", field: "items", message: "empty is not allowed", value: []},
%{
block: "image",
field: "mode",
message: "should be: single | jiugongge | gallery"
}
]
] == err_msg
end

@tag :wip2
Expand All@@ -213,7 +214,7 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
set_items("single", [
%{
"index" => "invalid",
"src" => "src"
"src" => "https://xxx"
}
])

Expand All@@ -230,28 +231,28 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.Image do
]
end

#@tag :wip2
#test "invalidsrcparseshouldraise error message" do
# editor_json =
# set_items("single", [
# %{
# "index" => 0,
# "src" => "src"
# }
# ])

# {:ok, editor_string} = Jason.encode(editor_json)
# {:error, err_msg} = Parser.to_html(editor_string)
# IO.inspect(err_msg, label: "err_msg")

#assert err_msg == [
# %{
# block: "image(single)",
# field: "index",
# message: "shouldbe: number",
# value: "invalid"
# }
# ]
#end
@tag :wip2
test "src shouldstarts with https://" do
editor_json =
set_items("single", [
%{
"index" => 0,
"src" => "src"
}
])

{:ok, editor_string} = Jason.encode(editor_json)
{:error, err_msg} = Parser.to_html(editor_string)

assert err_msg ==
[
%{
block: "image(single)",
field: "src",
message: "shouldstarts with: https://",
value: "src"
}
]
end
end
end
5 changes: 3 additions & 2 deletionstest/helper/converter/editor_to_html_test/list_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -215,13 +215,14 @@ defmodule GroupherServer.Test.Helper.Converter.EditorToHTML.List do
{:ok, editor_string} = Jason.encode(editor_json)
{:error, err_msg} = Parser.to_html(editor_string)

assert err_msg == [
assert [
%{block: "list", field: "items", message: "empty is not allowed", value: []},
%{
block: "list",
field: "mode",
message: "should be: checklist | order_list | unorder_list"
}
]
] == err_msg
end

@tag :wip
Expand Down
34 changes: 34 additions & 0 deletionstest/helper/validator/schema_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
alias Helper.Validator.Schema

describe "[basic schema]" do
@tag :wip2
test "string with options" do
schema = %{"text" => [:string, required: false]}
data = %{"no_exsit" => "text"}
Expand DownExpand Up@@ -44,9 +45,15 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
data = %{"text" => "text"}
{:error, error} = Schema.cast(schema, data)
assert error == [%{field: "text", message: "unknow option: min: 5", value: "text"}]

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"}]
# IO.inspect(Schema.cast(schema, data), label: "schema result")
end

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

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

@tag :wip2
test "number with options edage case" do
schema = %{"text" => [:number, min: 2]}
data = %{"text" => "aa"}
Expand All@@ -107,6 +116,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
assert error == [%{field: "text", message: "should be: number", value: "aa"}]
end

@tag :wip2
test "list with options" do
schema = %{"text" => [:list, required: false]}
data = %{"no_exsit" => []}
Expand All@@ -120,8 +130,31 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
schema = %{"text" => [:list, required: true]}
data = %{"text" => []}
assert {:ok, _} = Schema.cast(schema, data)

schema = %{"text" => [:list]}
data = %{"text" => []}
assert {:ok, _} = Schema.cast(schema, data)

schema = %{"text" => [:list, allow_empty: true]}
data = %{"text" => []}
assert {:ok, _} = Schema.cast(schema, data)

schema = %{"text" => [:list, type: :map]}
data = %{"text" => [1, 2, 3]}
{:error, error} = Schema.cast(schema, data)

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

schema = %{"text" => [:list, allow_empty: false]}
data = %{"text" => []}
{: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
test "boolean with options" do
schema = %{"text" => [:boolean, required: false]}
data = %{"no_exsit" => false}
Expand All@@ -137,6 +170,7 @@ defmodule GroupherServer.Test.Helper.Validator.Schema do
assert {:ok, _} = Schema.cast(schema, data)
end

@tag :wip2
test "enum with options" do
schema = %{"text" => [enum: [1, 2, 3], required: false]}
data = %{"no_exsit" => false}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp