Class: RequestParamsValidation::Params::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/request_params_validation/params/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param_definition, value) ⇒ Validator

Returns a new instance of Validator.



15
16
17
18
19
# File 'lib/request_params_validation/params/validator.rb', line 15

def initialize(param_definition, value)
  @param = param_definition
  @value = value
  @original_value = value
end

Instance Attribute Details

#original_valueObject (readonly)

Returns the value of attribute original_value.



13
14
15
# File 'lib/request_params_validation/params/validator.rb', line 13

def original_value
  @original_value
end

#paramObject (readonly)

Returns the value of attribute param.



13
14
15
# File 'lib/request_params_validation/params/validator.rb', line 13

def param
  @param
end

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'lib/request_params_validation/params/validator.rb', line 13

def value
  @value
end

Instance Method Details

#validate_and_coerceObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/request_params_validation/params/validator.rb', line 21

def validate_and_coerce
  validate_presence! if param.validate_presence?

  if value.blank? && param.has_default?
    @value = param.default
    return value
  end

  return value if value.nil?

  validate_type! if param.validate_type?

  case param.type
  when Params::ARRAY_TYPE
    iterate_array
  when Params::HASH_TYPE
    iterate_hash
  end

  @value = Params::Converter.coerce(param, value)

  validate_inclusion!         if param.validate_inclusion?
  validate_length!            if param.validate_length?
  validate_value!             if param.validate_value?
  validate_format!            if param.validate_format?
  validate_custom_validation! if param.validate_custom_validation?

  @value
end