Class: Validaform::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/validaform.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:) ⇒ Base

Returns a new instance of Base.



9
10
11
12
# File 'lib/validaform.rb', line 9

def initialize(params:)
  @fields = params.permit(fields: %i[name value]).to_h[:fields]
  @status_code = 200
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#status_codeObject

Returns the value of attribute status_code.



7
8
9
# File 'lib/validaform.rb', line 7

def status_code
  @status_code
end

Instance Method Details

#errorsObject

fields => [

{ name: 'users/first_name', value: 'Asterix' }

] errors will return {

fields:[
  {
    name: 'users/first_name',
    errors: ['too_short', 'too_long'],
    count: 2
  }
],
count: 2

}



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/validaform.rb', line 29

def errors
  errors_hash = { fields: [] }
  parse_fields.map do |resource, fields|
    resource_class = resource_class(resource)
    dev_errors = handle_development_errors(resource, resource_class, fields)
    return dev_errors if dev_errors.present?
    generate_errors_hash(resource, resource_class, fields, errors_hash)
  end
  errors_hash[:count] = total_errors_count(errors_hash)
  errors_hash
end