@@ -5,22 +5,7 @@ module Validations
5
5
module Validators
6
6
class ValuesValidator < Base
7
7
def initialize ( attrs , options , required , scope , **opts )
8
- if options . is_a? ( Hash )
9
- @excepts = options [ :except ]
10
- @values = options [ :value ]
11
- @proc = options [ :proc ]
12
-
13
- Grape . deprecator . warn ( 'The values validator except option is deprecated. Use the except validator instead.' ) if @excepts
14
-
15
- raise ArgumentError , 'proc must be a Proc' if @proc && !@proc . is_a? ( Proc )
16
-
17
- Grape . deprecator . warn ( 'The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.' ) if @proc
18
- else
19
- @excepts = nil
20
- @values = nil
21
- @proc = nil
22
- @values = options
23
- end
8
+ @values = options . is_a? ( Hash ) ? options [ :value ] : options
24
9
super
25
10
end
26
11
@@ -34,54 +19,29 @@ def validate_param!(attr_name, params)
34
19
# don't forget that +false.blank?+ is true
35
20
return if val != false && val . blank? && @allow_blank
36
21
37
- param_array = val . nil? ? [ nil ] : Array . wrap ( val )
38
-
39
- raise validation_exception ( attr_name , except_message ) \
40
- unless check_excepts ( param_array )
41
-
42
- raise validation_exception ( attr_name , message ( :values ) ) \
43
- unless check_values ( param_array , attr_name )
22
+ return if check_values? ( val , attr_name )
44
23
45
- raise validation_exception ( attr_name , message ( :values ) ) \
46
- if @proc && !validate_proc ( @proc , param_array )
24
+ raise Grape ::Exceptions ::Validation . new (
25
+ params : [ @scope . full_name ( attr_name ) ] ,
26
+ message : message ( :values )
27
+ )
47
28
end
48
29
49
30
private
50
31
51
- def check_values ( param_array , attr_name )
32
+ def check_values? ( val , attr_name )
52
33
values = @values . is_a? ( Proc ) && @values . arity . zero? ? @values . call : @values
53
34
return true if values . nil?
54
35
36
+ param_array = val . nil? ? [ nil ] : Array . wrap ( val )
37
+ return param_array . all? { |param | values . include? ( param ) } unless values . is_a? ( Proc )
38
+
55
39
begin
56
- return param_array . all? { |param | values . call ( param ) } if values . is_a? Proc
40
+ param_array . all? { |param | values . call ( param ) }
57
41
rescue StandardError => e
58
42
warn "Error '#{ e } ' raised while validating attribute '#{ attr_name } '"
59
- return false
43
+ false
60
44
end
61
- param_array . all? { |param | values . include? ( param ) }
62
- end
63
-
64
- def check_excepts ( param_array )
65
- excepts = @excepts . is_a? ( Proc ) ? @excepts . call : @excepts
66
- return true if excepts . nil?
67
-
68
- param_array . none? { |param | excepts . include? ( param ) }
69
- end
70
-
71
- def validate_proc ( proc , param_array )
72
- case proc . arity
73
- when 0
74
- param_array . all? { |_param | proc . call }
75
- when 1
76
- param_array . all? { |param | proc . call ( param ) }
77
- else
78
- raise ArgumentError , 'proc arity must be 0 or 1'
79
- end
80
- end
81
-
82
- def except_message
83
- options = instance_variable_get ( :@option )
84
- options_key? ( :except_message ) ? options [ :except_message ] : message ( :except_values )
85
45
end
86
46
87
47
def required_for_root_scope?
@@ -92,10 +52,6 @@ def required_for_root_scope?
92
52
93
53
scope . root?
94
54
end
95
-
96
- def validation_exception ( attr_name , message )
97
- Grape ::Exceptions ::Validation . new ( params : [ @scope . full_name ( attr_name ) ] , message : message )
98
- end
99
55
end
100
56
end
101
57
end
0 commit comments