Class: Philiprehberger::SchemaValidator::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/schema_validator/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors: []) ⇒ Result

Returns a new instance of Result.



8
9
10
# File 'lib/philiprehberger/schema_validator/result.rb', line 8

def initialize(errors: [])
  @errors = errors
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/philiprehberger/schema_validator/result.rb', line 6

def errors
  @errors
end

Instance Method Details

#error_countInteger

Number of errors in the result

Returns:

  • (Integer)


19
20
21
# File 'lib/philiprehberger/schema_validator/result.rb', line 19

def error_count
  @errors.length
end

#errors_by_fieldHash{String => Array<String>}

Group error messages by the leading field name (e.g. “address.zip” => “address”)

Returns:

  • (Hash{String => Array<String>})


33
34
35
36
37
38
# File 'lib/philiprehberger/schema_validator/result.rb', line 33

def errors_by_field
  @errors.each_with_object({}) do |err, acc|
    field = err.split(/[\s.]/, 2).first.to_s
    (acc[field] ||= []) << err
  end
end

#to_hHash{Symbol => Object}

Structured hash representation of the result

Returns:

  • (Hash{Symbol => Object})


26
27
28
# File 'lib/philiprehberger/schema_validator/result.rb', line 26

def to_h
  { valid: valid?, errors: @errors.dup, error_count: error_count }
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/philiprehberger/schema_validator/result.rb', line 12

def valid?
  @errors.empty?
end