Module: HashParams::Validator

Included in:
HashParams
Defined in:
lib/hash_params/validator.rb

Defined Under Namespace

Classes: CoercionError, ValidationError

Instance Method Summary collapse

Instance Method Details

#validate(param, type = nil, validations = {}) ⇒ Object

Raises:



14
15
16
17
18
19
20
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hash_params/validator.rb', line 14

def validate(param, type=nil, validations={})

  coercions = Array(validations[:coerce]) << type

  if param.nil? && validations[:default]
    param = validations[:default].respond_to?(:call) ? validations[:default].call() : validations[:default]
  end

  #if there is a :validate lambda run that too
  if validations[:validate] && validations[:validate].respond_to?(:call)
    param = validations.delete(:validate).call(param)
  end

  #don't bother with the rest if required parameter is missing
  if validations[:required] && param.nil?
    raise ValidationError.new('Required Parameter missing and has no default specified')
  end

  if block_given? && !param.is_a?(Hash)
    param = yield(param, validations)
  end

  #  do all coercion and transformation first there could be an array of coersions they will be run in order
  coercions.each do |c|
    param = coerce(param, c, validations)
  end

  if param.is_a?(Hash)
    param = if block_given?
              HashParams::HashValidator.new.validate_hash(param, validations, &Proc.new)
            else
              HashParams::HashValidator.new.validate_hash(param, validations)
            end
  end

  #don't bother with the rest if required parameter is missing
  if validations[:required] && param.nil?
    raise ValidationError.new('Required Parameter missing and has no default specified')
  end
  errors = []
  error = nil
  validations.each do |key, value|
    error = case key
              when :blank
                'Parameter cannot be blank' if !value && (param.nil? || (param.respond_to?(:empty) && param.empty)) #)!value && blank?(value)
              when :format
                "#{param} must be a string if using the format validation" && next unless param.kind_of?(String)
                "#{param} must match format #{value}" unless param =~ value
              when :is
                "#{param} must be #{value}" unless param === value
              when :in, :within, :range
                "#{param} must be within #{value}" unless value.respond_to?(:include) ? value.include?(param) : Array(value).include?(param)
              when :min
                "#{param} cannot be less than #{value}" unless value <= param
              when :max
                "#{param} cannot be greater than #{value}" unless value >= param
              when :min_length
                "#{param} cannot have length less than #{value}" unless value <= param.length
              when :max_length
                "#{param} cannot have length greater than #{value}" unless value >= param.length
              else
                nil
            end
    if error
      error = validations[:errmsg] || error
      errors << error
    end

  end

  raise ValidationError.new(errors.join("\n")) unless errors.empty?
  param
end

#with_binding(&code) ⇒ Object



10
11
12
# File 'lib/hash_params/validator.rb', line 10

def with_binding(&code)
  BindingValidator.new.with_binding(&code)
end