@@ -475,39 +475,29 @@ defmodule Request.Validator.Rules do
475
475
476
476
def min ( bound ) when is_number ( bound ) do
477
477
# TODO: check for `Plug.Upload` size.
478
- validator_fn = fn
479
- bound , attr , value when is_number ( value ) ->
480
- message =
481
- gettext ( "The %{attribute} field must be at least %{min}." ,
482
- attribute: attr ,
483
- min: bound
484
- )
485
-
486
- check ( value >= bound , message )
487
-
488
- bound , attr , value when is_binary ( value ) ->
489
- message =
490
- gettext ( "The %{attribute} field must be at least %{min} characters." ,
491
- attribute: attr ,
492
- min: bound
493
- )
494
-
495
- value
496
- |> String . length ( )
497
- |> Kernel . >= ( bound )
498
- |> check ( message )
499
-
500
- bound , attr , value when is_list ( value ) ->
501
- message =
502
- gettext ( "The %{attribute} field must be at least %{min} items." ,
503
- attribute: attr ,
504
- min: bound
505
- )
478
+ validator_fn = fn bound , attr , value ->
479
+ message =
480
+ case get_type ( value ) do
481
+ :numeric ->
482
+ gettext ( "The %{attribute} field must be at least %{min}." ,
483
+ attribute: attr ,
484
+ min: bound
485
+ )
486
+
487
+ :string ->
488
+ gettext ( "The %{attribute} field must be at least %{min} characters." ,
489
+ attribute: attr ,
490
+ min: bound
491
+ )
492
+
493
+ :list ->
494
+ gettext ( "The %{attribute} field must be at least %{min} items." ,
495
+ attribute: attr ,
496
+ min: bound
497
+ )
498
+ end
506
499
507
- value
508
- |> Enum . count ( )
509
- |> Kernel . >= ( bound )
510
- |> check ( message )
500
+ check ( get_size ( value ) >= bound , message )
511
501
end
512
502
513
503
& validator_fn . ( bound , & 1 , & 2 )
@@ -539,39 +529,29 @@ defmodule Request.Validator.Rules do
539
529
540
530
def max ( bound ) when is_number ( bound ) do
541
531
# TODO: check for `Plug.Upload` size.
542
- validator_fn = fn
543
- bound , attr , value when is_number ( value ) ->
544
- message =
545
- gettext ( "The %{attribute} field must not be greater than %{max}." ,
546
- attribute: attr ,
547
- max: bound
548
- )
549
-
550
- check ( value <= bound , message )
551
-
552
- bound , attr , value when is_binary ( value ) ->
553
- message =
554
- gettext ( "The %{attribute} field must not be greater than %{max} characters." ,
555
- attribute: attr ,
556
- max: bound
557
- )
558
-
559
- value
560
- |> String . length ( )
561
- |> Kernel . <= ( bound )
562
- |> check ( message )
563
-
564
- bound , attr , value when is_list ( value ) ->
565
- message =
566
- gettext ( "The %{attribute} field must not be greater than %{max} items." ,
567
- attribute: attr ,
568
- max: bound
569
- )
532
+ validator_fn = fn bound , attr , value ->
533
+ message =
534
+ case get_type ( value ) do
535
+ :numeric ->
536
+ gettext ( "The %{attribute} field must not be greater than %{max}." ,
537
+ attribute: attr ,
538
+ max: bound
539
+ )
540
+
541
+ :list ->
542
+ gettext ( "The %{attribute} field must not be greater than %{max} items." ,
543
+ attribute: attr ,
544
+ max: bound
545
+ )
546
+
547
+ :string ->
548
+ gettext ( "The %{attribute} field must not be greater than %{max} characters." ,
549
+ attribute: attr ,
550
+ max: bound
551
+ )
552
+ end
570
553
571
- value
572
- |> Enum . count ( )
573
- |> Kernel . <= ( bound )
574
- |> check ( message )
554
+ check ( get_size ( value ) <= bound , message )
575
555
end
576
556
577
557
& validator_fn . ( bound , & 1 , & 2 )
@@ -583,4 +563,12 @@ defmodule Request.Validator.Rules do
583
563
false -> { :error , message }
584
564
end
585
565
end
566
+
567
+ defp get_size ( num ) when is_number ( num ) , do: num
568
+ defp get_size ( value ) when is_binary ( value ) , do: String . length ( value )
569
+ defp get_size ( list ) when is_list ( list ) , do: Enum . count ( list )
570
+
571
+ defp get_type ( num ) when is_number ( num ) , do: :numeric
572
+ defp get_type ( value ) when is_binary ( value ) , do: :string
573
+ defp get_type ( list ) when is_list ( list ) , do: :list
586
574
end
0 commit comments