Class: ValidateParams::Validatable::ParamValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/validate_params/param_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, field:, value:, errors:, options: {}) ⇒ ParamValidator

Returns a new instance of ParamValidator.



12
13
14
15
16
17
18
# File 'lib/validate_params/param_validator.rb', line 12

def initialize(type:, field:, value:, errors:, options: {})
  @type = type
  @field = field
  @value = value
  @errors = errors
  @options = options
end

Class Method Details

.call(**args) ⇒ Object



8
9
10
# File 'lib/validate_params/param_validator.rb', line 8

def self.call(**args)
  new(**args).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/validate_params/param_validator.rb', line 20

def call
  return if @value.blank? && !@options[:required]

  if @value.blank? && @options[:required]
    @errors << { message: required_error_message }
    return
  end

  send(@type.to_s.underscore)
end