Skip to content

Commit 55ea45d

Browse files
committed
refactor: allow function return
1 parent d4e8e64 commit 55ea45d

File tree

3 files changed

+45
-47
lines changed

3 files changed

+45
-47
lines changed

lib/helper.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ defmodule Request.Validator.Helper do
88
iex> import Request.Validator.Helper
99
iex> import Request.Validator.Rulex
1010
iex> rules = ~V[required|email:format]
11-
iex> match?([%{name: :required}, %{name: :email}], rules)
11+
iex> [%{validator: _}, rule] = rules
12+
iex> is_function(rule, 2)
1213
true
1314
"""
1415
defmacro sigil_V({:<<>>, _, [rules]}, []) do

lib/validator/rules.ex

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ defmodule Request.Validator.Rulex do
77

88
require Decimal
99

10-
@type rule :: %{
11-
required(:name) => atom(),
12-
required(:validator) => (... -> :ok | {:error, String.t()}),
13-
optional(:implicit?) => boolean()
14-
}
10+
@type validator :: (... -> :ok | {:error, String.t()})
11+
@type rule ::
12+
validator()
13+
| %{
14+
required(:validator) => validator(),
15+
optional(:implicit?) => boolean()
16+
}
1517

1618
@backend Application.compile_env(
1719
:request_validator,
@@ -59,11 +61,7 @@ defmodule Request.Validator.Rulex do
5961
end
6062
end
6163

62-
%{
63-
name: :required,
64-
implicit?: true,
65-
validator: validator_fn
66-
}
64+
%{implicit?: true, validator: validator_fn}
6765
end
6866

6967
@doc """
@@ -96,7 +94,6 @@ defmodule Request.Validator.Rulex do
9694
end
9795

9896
%{
99-
name: :required_if,
10097
implicit?: true,
10198
validator: &validator_fn.(condition, &1, &2, validator_fn)
10299
}
@@ -134,7 +131,6 @@ defmodule Request.Validator.Rulex do
134131
end
135132

136133
%{
137-
name: :required_if,
138134
implicit?: true,
139135
validator: &(check_fn.(other, value, op, &3) |> validator_fn.(&1, &2, other, value))
140136
}
@@ -144,7 +140,7 @@ defmodule Request.Validator.Rulex do
144140
## Examples
145141
146142
iex> import Request.Validator.Rulex
147-
iex> %{validator: fun} = string()
143+
iex> fun = string()
148144
iex> fun.("content", "")
149145
:ok
150146
iex> fun.("content", 1)
@@ -164,14 +160,14 @@ defmodule Request.Validator.Rulex do
164160
check(is_binary(value), message)
165161
end
166162

167-
%{name: :string, validator: &validator_fn.(&1, &2)}
163+
&validator_fn.(&1, &2)
168164
end
169165

170166
@doc """
171167
## Examples
172168
173169
iex> import Request.Validator.Rulex
174-
iex> %{validator: fun} = alpha()
170+
iex> fun = alpha()
175171
iex> fun.("uid", "abcde")
176172
:ok
177173
iex> fun.("uid", 1)
@@ -194,14 +190,14 @@ defmodule Request.Validator.Rulex do
194190
|> check(message)
195191
end
196192

197-
%{name: :alpha, validator: &validator_fn.(&1, &2)}
193+
&validator_fn.(&1, &2)
198194
end
199195

200196
@doc """
201197
## Examples
202198
203199
iex> import Request.Validator.Rulex
204-
iex> %{validator: fun} = alpha_num()
200+
iex> fun = alpha_num()
205201
iex> fun.("ref", "1ab2de3")
206202
:ok
207203
iex> fun.("ref", 1)
@@ -229,14 +225,14 @@ defmodule Request.Validator.Rulex do
229225
|> check(message)
230226
end
231227

232-
%{name: :alpha_num, validator: &validator_fn.(&1, &2)}
228+
&validator_fn.(&1, &2)
233229
end
234230

235231
@doc """
236232
## Examples
237233
238234
iex> import Request.Validator.Rulex
239-
iex> %{validator: fun} = alpha_dash()
235+
iex> fun = alpha_dash()
240236
iex> fun.("username", "abcde2")
241237
:ok
242238
iex> fun.("username", "ab_d-2")
@@ -265,14 +261,14 @@ defmodule Request.Validator.Rulex do
265261
|> check(message)
266262
end
267263

268-
%{name: :alpha_dash, validator: &validator_fn.(&1, &2)}
264+
&validator_fn.(&1, &2)
269265
end
270266

271267
@doc """
272268
## Examples
273269
274270
iex> import Request.Validator.Rulex
275-
iex> %{validator: fun} = integer()
271+
iex> fun = integer()
276272
iex> fun.("age", 1)
277273
:ok
278274
iex> fun.("age", 2.0)
@@ -294,14 +290,14 @@ defmodule Request.Validator.Rulex do
294290
check(is_integer(value), message)
295291
end
296292

297-
%{name: :integer, validator: &validator_fn.(&1, &2)}
293+
&validator_fn.(&1, &2)
298294
end
299295

300296
@doc """
301297
## Examples
302298
303299
iex> import Request.Validator.Rulex
304-
iex> %{validator: fun} = decimal()
300+
iex> fun = decimal()
305301
iex> fun.("amount", 2.0)
306302
:ok
307303
iex> fun.("amount", Decimal.new("9.999"))
@@ -319,20 +315,21 @@ defmodule Request.Validator.Rulex do
319315
"""
320316
@spec decimal() :: rule()
321317
def decimal do
318+
# TODO: support decimal places validation.
322319
validator_fn = fn attr, value ->
323320
message = gettext("The %{attribute} field must be a decimal.", attribute: attr)
324321

325322
check(is_float(value) or Decimal.is_decimal(value), message)
326323
end
327324

328-
%{name: :decimal, validator: &validator_fn.(&1, &2)}
325+
&validator_fn.(&1, &2)
329326
end
330327

331328
@doc """
332329
## Examples
333330
334331
iex> import Request.Validator.Rulex
335-
iex> %{validator: fun} = numeric()
332+
iex> fun = numeric()
336333
iex> fun.("width", 2.0)
337334
:ok
338335
iex> fun.("width", 1)
@@ -354,20 +351,20 @@ defmodule Request.Validator.Rulex do
354351
check(is_number(value), message)
355352
end
356353

357-
%{name: :numeric, validator: &validator_fn.(&1, &2)}
354+
&validator_fn.(&1, &2)
358355
end
359356

360357
@doc """
361358
## Examples
362359
363360
iex> import Request.Validator.Rulex
364-
iex> %{validator: fun} = email()
361+
iex> fun = email()
365362
iex> fun.("email", "test@gmail.com")
366363
:ok
367-
iex> %{validator: fun} = email([:format])
364+
iex> fun = email([:format])
368365
iex> fun.("email", "a@b.com")
369366
:ok
370-
iex> %{validator: fun} = email(["mx"])
367+
iex> fun = email(["mx"])
371368
iex> fun.("email", "a@b.com")
372369
{:error, "The email field must be a valid email address."}
373370
iex> fun.("email", 2.0)
@@ -394,7 +391,7 @@ defmodule Request.Validator.Rulex do
394391
check(is_binary(value) and EmailChecker.valid?(value, validations), message)
395392
end
396393

397-
%{name: :email, validator: &validator_fn.(validations, &1, &2)}
394+
&validator_fn.(validations, &1, &2)
398395
end
399396

400397
@doc """
@@ -406,7 +403,7 @@ defmodule Request.Validator.Rulex do
406403
...> "password_confirmation" => 12345678,
407404
...> "list" => [%{"a" => 1, "a_confirmation" => 1}]
408405
...> }
409-
iex> %{validator: fun} = confirmed()
406+
iex> fun = confirmed()
410407
iex> fun.("password", 12345678, data)
411408
:ok
412409
iex> fun.("list.0.a", 1, data)
@@ -436,14 +433,14 @@ defmodule Request.Validator.Rulex do
436433
|> check(message)
437434
end
438435

439-
%{name: :confirmed, validator: &validator_fn.(confirmation, &1, &2, &3)}
436+
&validator_fn.(confirmation, &1, &2, &3)
440437
end
441438

442439
@doc """
443440
## Examples
444441
445442
iex> import Request.Validator.Rulex
446-
iex> %{validator: fun} = allowed(["male", "female"])
443+
iex> fun = allowed(["male", "female"])
447444
iex> fun.("gender", "male")
448445
:ok
449446
iex> fun.("gender", "female")
@@ -463,7 +460,7 @@ defmodule Request.Validator.Rulex do
463460
|> check(message)
464461
end
465462

466-
%{name: :allowed, validator: &validator_fn.(options, &1, &2)}
463+
&validator_fn.(options, &1, &2)
467464
end
468465

469466
defp check(cond, message) do

priv/gettext/default.pot

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,62 @@
1111
msgid ""
1212
msgstr ""
1313

14-
#: lib/validator/rules.ex:52
14+
#: lib/validator/rules.ex:55
1515
#, elixir-autogen, elixir-format
1616
msgid "The %{attribute} field is required."
1717
msgstr ""
1818

19-
#: lib/validator/rules.ex:126
19+
#: lib/validator/rules.ex:124
2020
#, elixir-autogen, elixir-format
2121
msgid "The %{attribute} field is required when %{other} is %{value}."
2222
msgstr ""
2323

24-
#: lib/validator/rules.ex:162
24+
#: lib/validator/rules.ex:159
2525
#, elixir-autogen, elixir-format
2626
msgid "The %{attribute} field must be a string."
2727
msgstr ""
2828

29-
#: lib/validator/rules.ex:189
29+
#: lib/validator/rules.ex:186
3030
#, elixir-autogen, elixir-format
3131
msgid "The %{attribute} field must only contain letters."
3232
msgstr ""
3333

34-
#: lib/validator/rules.ex:222
34+
#: lib/validator/rules.ex:219
3535
#, elixir-autogen, elixir-format
3636
msgid "The %{attribute} field must only contain letters and numbers."
3737
msgstr ""
3838

39-
#: lib/validator/rules.ex:257
39+
#: lib/validator/rules.ex:254
4040
#, elixir-autogen, elixir-format
4141
msgid "The %{attribute} field must only contain letters, numbers, dashes, and underscores."
4242
msgstr ""
4343

44-
#: lib/validator/rules.ex:292
44+
#: lib/validator/rules.ex:289
4545
#, elixir-autogen, elixir-format
4646
msgid "The %{attribute} field must be an integer."
4747
msgstr ""
4848

49-
#: lib/validator/rules.ex:323
49+
#: lib/validator/rules.ex:321
5050
#, elixir-autogen, elixir-format
5151
msgid "The %{attribute} field must be a decimal."
5252
msgstr ""
5353

54-
#: lib/validator/rules.ex:352
54+
#: lib/validator/rules.ex:350
5555
#, elixir-autogen, elixir-format
5656
msgid "The %{attribute} field must be a number."
5757
msgstr ""
5858

59-
#: lib/validator/rules.ex:392
59+
#: lib/validator/rules.ex:390
6060
#, elixir-autogen, elixir-format
6161
msgid "The %{attribute} field must be a valid email address."
6262
msgstr ""
6363

64-
#: lib/validator/rules.ex:431
64+
#: lib/validator/rules.ex:429
6565
#, elixir-autogen, elixir-format
6666
msgid "The %{attribute} field confirmation does not match."
6767
msgstr ""
6868

69-
#: lib/validator/rules.ex:459
69+
#: lib/validator/rules.ex:457
7070
#, elixir-autogen, elixir-format
7171
msgid "The selected %{attribute} is invalid."
7272
msgstr ""

0 commit comments

Comments
 (0)