Module: Performify::Validation::InstanceMethods

Defined in:
lib/performify/validation.rb

Instance Method Summary collapse

Instance Method Details

#errorsObject



47
48
49
# File 'lib/performify/validation.rb', line 47

def errors
  @errors ||= {}
end

#errors!(new_errors) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/performify/validation.rb', line 39

def errors!(new_errors)
  raise ArgumentError, 'Errors should be a hash' if new_errors.nil? || !new_errors.respond_to?(:to_h)

  new_errors.to_h.each do |key, value|
    errors[key] = errors.key?(key) ? [errors[key]].flatten(1) + [value].flatten(1) : value
  end
end

#errors?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/performify/validation.rb', line 51

def errors?
  errors.any?
end

#schemaObject



23
24
25
# File 'lib/performify/validation.rb', line 23

def schema
  self.class.schema
end

#validateObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/performify/validation.rb', line 27

def validate
  return args if schema.nil?

  result = schema.with(with_options).call(args)
  if result.success?
    @inputs = result.output
  else
    errors!(result.errors)
  end
  result.output
end