Module: Flexirest::Validation

Included in:
Base
Defined in:
lib/flexirest/validation.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
# File 'lib/flexirest/validation.rb', line 15

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#_errorsObject



101
102
103
104
# File 'lib/flexirest/validation.rb', line 101

def _errors
  @errors ||= Hash.new {|h,k| h[k] = []}
  @errors
end

#full_error_messagesObject



94
95
96
97
98
99
# File 'lib/flexirest/validation.rb', line 94

def full_error_messages
  return "" unless _errors.present?
  _errors.reduce([]) do |memo, (field, errors)|
    memo << "#{field.to_s} #{errors.join(' and ')}"
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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
87
88
89
90
91
92
# File 'lib/flexirest/validation.rb', line 19

def valid?
  @errors = Hash.new {|h,k| h[k] = []}
  self.class._validations.each do |validation|
    value = self.send(validation[:field_name])
    allow_nil = validation[:options].fetch(:allow_nil, false)
    validation[:options].each do |type, options|
      if type == :presence
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be present")
        elsif value.blank?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be present")
        end
      elsif type == :existence
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil")
        end
      elsif type == :length
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
        else
          if options[:within]
            @errors[validation[:field_name]] << (validation[:options][:message] || "must be within range #{options[:within]}") unless options[:within].include?(value.to_s.length )
          end
          if options[:minimum]
            @errors[validation[:field_name]] << (validation[:options][:message] || "must be at least #{options[:minimum]} characters long") unless value.to_s.length >= options[:minimum]
          end
          if options[:maximum]
            @errors[validation[:field_name]] << (validation[:options][:message] || "must be no more than #{options[:maximum]} characters long") unless value.to_s.length <= options[:maximum]
          end
        end
      elsif type == :numericality
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
        else
          numeric = (true if Float(value) rescue false)
          if !numeric
            @errors[validation[:field_name]] << (validation[:options][:message] || "must be numeric")
          else
            if options.is_a?(Hash)
              if options[:minimum]
                @errors[validation[:field_name]] << (validation[:options][:message] || "must be at least #{options[:minimum]}") unless value.to_f >= options[:minimum]
              end
              if options[:maximum]
                @errors[validation[:field_name]] << (validation[:options][:message] || "must be no more than #{options[:maximum]}") unless value.to_f <= options[:maximum]
              end
            end
          end
        end
      elsif type == :minimum
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
        else
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be at least #{options}") unless value.to_f >= options.to_f
        end
      elsif type == :maximum
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
        else
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be no more than #{options}") unless value.to_f <= options.to_f
        end
      elsif type == :inclusion
        if value.nil?
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
        else
          @errors[validation[:field_name]] << (validation[:options][:message] || "must be included in #{options[:in].join(", ")}") unless options[:in].include?(value)
        end
      end
    end
    if validation[:block]
      validation[:block].call(self, validation[:field_name], value)
    end
  end
  @errors.empty?
end