Module: FatZebra::Validation

Included in:
FatZebraObject
Defined in:
lib/fat_zebra/validation.rb

Instance Method Summary collapse

Instance Method Details

#errorsObject



28
29
30
# File 'lib/fat_zebra/validation.rb', line 28

def errors
  @errors ||= []
end

#valid!(params, action = nil) ⇒ Object



24
25
26
# File 'lib/fat_zebra/validation.rb', line 24

def valid!(params, action = nil)
  raise RequestValidationError, "The following errors prevent the transaction from being submitted: #{errors.join(', ')}" unless valid?(params, action)
end

#valid?(params, action = nil) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fat_zebra/validation.rb', line 10

def valid?(params, action = nil)
  @errors = []

  validations.each do |(field, options)|
    next unless options[:on] == action

    Util.hash_except(options, :on).each do |(validation_attribute, validation_options)|
      send("validate_#{validation_attribute}", field, validation_options, params)
    end
  end

  errors.empty?
end

#validates(name, options = {}) ⇒ Object



6
7
8
# File 'lib/fat_zebra/validation.rb', line 6

def validates(name, options = {})
  validations[name] = options
end