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

Commit eb76895

Browse files
committed
refactor(validator): make schama validator more flexable
1 parent 9d6cb01 commit eb76895

File tree

3 files changed

+229
-10
lines changed

3 files changed

+229
-10
lines changed

lib/helper/converter/editor_to_html/index.ex

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
# defmodule Helper.Converter.EditorToHTML.Parser do
2-
# @moduledoc false
3-
4-
# # TODO: map should be editor_block
5-
# @callback parse_block(editor_json :: Map.t()) :: String.t()
6-
# end
7-
81
defmodule Helper.Converter.EditorToHTML do
92
@moduledoc """
103
parse editor.js's json data to raw html and sanitize it

lib/helper/validator/schema.ex

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Helper.Validator.Schema do
55
currently support boolean / string / number / enum
66
"""
77

8-
use Helper.Validator.Schema.Matchers, [:string, :number, :list, :boolean]
8+
# use Helper.Validator.Schema.Matchers, [:string, :number, :list, :boolean]
99

1010
@doc """
1111
cast data by given schema
@@ -49,10 +49,9 @@ defmodule Helper.Validator.Schema do
4949
end)
5050
end
5151

52-
# enum
5352
defp match(field, nil, enum: _, required: false), do: done(field, nil)
5453

55-
defp match(field, value, enum: enum, required: false) do
54+
defp match(field, value, enum: enum, required: _) do
5655
match(field, value, enum: enum)
5756
end
5857

@@ -70,8 +69,89 @@ defmodule Helper.Validator.Schema do
7069
end
7170
end
7271

72+
defp match(field, value, [type | options]) do
73+
match(field, value, type, options)
74+
end
75+
76+
defp match(field, nil, _type, [{:required, false} | _options]) do
77+
done(field, nil)
78+
end
79+
80+
defp match(field, value, type, [{:required, _} | options]) do
81+
match(field, value, type, options)
82+
end
83+
84+
# custom validate logic
85+
defp match(field, value, :string, [{:min, min} | options])
86+
when is_binary(value) and is_integer(min) do
87+
case String.length(value) >= min do
88+
true ->
89+
match(field, value, :string, options)
90+
91+
false ->
92+
error(field, value, :min, min)
93+
end
94+
end
95+
96+
defp match(field, value, :number, [{:min, min} | options])
97+
when is_integer(value) and is_integer(min) do
98+
case value >= min do
99+
true ->
100+
match(field, value, :number, options)
101+
102+
false ->
103+
error(field, value, :min, min)
104+
end
105+
end
106+
107+
# custom validate logic end
108+
109+
# main type
110+
defp match(field, value, :string, []) when is_binary(value) do
111+
done(field, value)
112+
end
113+
114+
defp match(field, value, :number, []) when is_integer(value) do
115+
done(field, value)
116+
end
117+
118+
defp match(field, value, :list, []) when is_list(value) do
119+
done(field, value)
120+
end
121+
122+
defp match(field, value, :boolean, []) when is_boolean(value) do
123+
done(field, value)
124+
end
125+
126+
# main type end
127+
128+
defp match(field, value, _type, [option]) when is_tuple(option) and not is_nil(value) do
129+
{k, v} = option
130+
error(field, value, option: "#{to_string(k)}: #{to_string(v)}")
131+
end
132+
133+
defp match(field, value, _type, [_option]) when not is_nil(value) do
134+
error(field, value, :option)
135+
end
136+
137+
defp match(field, value, type, _) do
138+
error(field, value, type)
139+
end
140+
73141
defp done(field, value), do: {:ok, %{field: field, value: value}}
74142

143+
defp error(field, value, :min, min) do
144+
{:error, %{field: field |> to_string, value: value, message: "min size: #{min}"}}
145+
end
146+
147+
defp error(field, value, option: option) do
148+
{:error, %{field: field |> to_string, value: value, message: "unknow option: #{option}"}}
149+
end
150+
151+
defp error(field, value, :option) do
152+
{:error, %{field: field |> to_string, value: value, message: "unknow option"}}
153+
end
154+
75155
defp error(field, value, schema) do
76156
{:error, %{field: field |> to_string, value: value, message: "should be: #{schema}"}}
77157
end
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
defmodule GroupherServer.Test.Helper.Validator.Schema do
2+
@moduledoc false
3+
4+
use GroupherServerWeb.ConnCase, async: true
5+
6+
alias Helper.Validator.Schema
7+
8+
describe "[basic schema]" do
9+
@tag :wip
10+
test "string with options" do
11+
schema = %{"text" => [:string, required: false]}
12+
data = %{"no_exsit" => "text"}
13+
assert {:ok, _} = Schema.cast(schema, data)
14+
15+
schema = %{"text" => [:string, required: true]}
16+
data = %{"no_exsit" => "text"}
17+
{:error, error} = Schema.cast(schema, data)
18+
assert error == [%{field: "text", message: "should be: string", value: nil}]
19+
20+
schema = %{"text" => [:string, required: true]}
21+
data = %{"text" => "text"}
22+
assert {:ok, _} = Schema.cast(schema, data)
23+
24+
schema = %{"text" => [:string, min: 5]}
25+
data = %{"text" => "text"}
26+
{:error, error} = Schema.cast(schema, data)
27+
assert error == [%{field: "text", message: "min size: 5", value: "text"}]
28+
29+
schema = %{"text" => [:string, required: false, min: 5]}
30+
data = %{"text" => "text"}
31+
{:error, error} = Schema.cast(schema, data)
32+
assert error == [%{field: "text", message: "min size: 5", value: "text"}]
33+
34+
schema = %{"text" => [:string, min: 5]}
35+
data = %{"no_exsit" => "text"}
36+
{:error, error} = Schema.cast(schema, data)
37+
assert error == [%{field: "text", message: "should be: string", value: nil}]
38+
39+
schema = %{"text" => [:string, required: true, min: 5]}
40+
data = %{"no_exsit" => "text"}
41+
{:error, error} = Schema.cast(schema, data)
42+
assert error == [%{field: "text", message: "should be: string", value: nil}]
43+
44+
schema = %{"text" => [:string, required: true, min: "5"]}
45+
data = %{"text" => "text"}
46+
{:error, error} = Schema.cast(schema, data)
47+
assert error == [%{field: "text", message: "unknow option: min: 5", value: "text"}]
48+
# IO.inspect(Schema.cast(schema, data), label: "schema result")
49+
end
50+
51+
@tag :wip
52+
test "number with options" do
53+
schema = %{"text" => [:number, required: false]}
54+
data = %{"no_exsit" => 1}
55+
assert {:ok, _} = Schema.cast(schema, data)
56+
57+
schema = %{"text" => [:number, required: true]}
58+
data = %{"no_exsit" => 1}
59+
{:error, error} = Schema.cast(schema, data)
60+
assert error == [%{field: "text", message: "should be: number", value: nil}]
61+
62+
schema = %{"text" => [:number, required: true]}
63+
data = %{"text" => 1}
64+
assert {:ok, _} = Schema.cast(schema, data)
65+
66+
schema = %{"text" => [:number, min: 5]}
67+
data = %{"text" => 4}
68+
{:error, error} = Schema.cast(schema, data)
69+
assert error == [%{field: "text", message: "min size: 5", value: 4}]
70+
71+
schema = %{"text" => [:number, required: false, min: 5]}
72+
data = %{"text" => 4}
73+
{:error, error} = Schema.cast(schema, data)
74+
assert error == [%{field: "text", message: "min size: 5", value: 4}]
75+
76+
schema = %{"text" => [:number, min: 5]}
77+
data = %{"no_exsit" => 4}
78+
{:error, error} = Schema.cast(schema, data)
79+
assert error == [%{field: "text", message: "should be: number", value: nil}]
80+
81+
schema = %{"text" => [:number, required: true, min: 5]}
82+
data = %{"no_exsit" => 1}
83+
{:error, error} = Schema.cast(schema, data)
84+
assert error == [%{field: "text", message: "should be: number", value: nil}]
85+
86+
schema = %{"text" => [:number, required: true, min: "5"]}
87+
data = %{"text" => 1}
88+
{:error, error} = Schema.cast(schema, data)
89+
assert error == [%{field: "text", message: "unknow option: min: 5", value: 1}]
90+
91+
# IO.inspect(Schema.cast(schema, data), label: "schema result")
92+
# hello world
93+
end
94+
95+
@tag :wip
96+
test "list with options" do
97+
schema = %{"text" => [:list, required: false]}
98+
data = %{"no_exsit" => []}
99+
assert {:ok, _} = Schema.cast(schema, data)
100+
101+
schema = %{"text" => [:list, required: true]}
102+
data = %{"no_exsit" => []}
103+
{:error, error} = Schema.cast(schema, data)
104+
assert error == [%{field: "text", message: "should be: list", value: nil}]
105+
106+
schema = %{"text" => [:list, required: true]}
107+
data = %{"text" => []}
108+
assert {:ok, _} = Schema.cast(schema, data)
109+
end
110+
111+
@tag :wip
112+
test "boolean with options" do
113+
schema = %{"text" => [:boolean, required: false]}
114+
data = %{"no_exsit" => false}
115+
assert {:ok, _} = Schema.cast(schema, data)
116+
117+
schema = %{"text" => [:boolean, required: true]}
118+
data = %{"no_exsit" => false}
119+
{:error, error} = Schema.cast(schema, data)
120+
assert error == [%{field: "text", message: "should be: boolean", value: nil}]
121+
122+
schema = %{"text" => [:boolean, required: true]}
123+
data = %{"text" => false}
124+
assert {:ok, _} = Schema.cast(schema, data)
125+
end
126+
127+
@tag :wip
128+
test "enum with options" do
129+
schema = %{"text" => [enum: [1, 2, 3], required: false]}
130+
data = %{"no_exsit" => false}
131+
assert {:ok, _} = Schema.cast(schema, data)
132+
133+
schema = %{"text" => [enum: [1, 2, 3], required: true]}
134+
data = %{"no_exsit" => false}
135+
{:error, error} = Schema.cast(schema, data)
136+
assert error == [%{field: "text", message: "should be: 1 | 2 | 3"}]
137+
138+
schema = %{"text" => [enum: [1, 2, 3]]}
139+
data = %{"text" => 1}
140+
assert {:ok, _} = Schema.cast(schema, data)
141+
142+
# IO.inspect(Schema.cast(schema, data), label: "schema result")
143+
# hello world
144+
end
145+
end
146+
end

0 commit comments

Comments
 (0)