Class: Philiprehberger::SchemaValidator::Result
- Inherits:
-
Object
- Object
- Philiprehberger::SchemaValidator::Result
- Defined in:
- lib/philiprehberger/schema_validator/result.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
-
#error_count ⇒ Integer
Number of errors in the result.
-
#errors_by_field ⇒ Hash{String => Array<String>}
Group error messages by the leading field name (e.g. “address.zip” => “address”).
-
#initialize(errors: []) ⇒ Result
constructor
A new instance of Result.
-
#to_h ⇒ Hash{Symbol => Object}
Structured hash representation of the result.
- #valid? ⇒ Boolean
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
#errors ⇒ Object (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_count ⇒ Integer
Number of errors in the result
19 20 21 |
# File 'lib/philiprehberger/schema_validator/result.rb', line 19 def error_count @errors.length end |
#errors_by_field ⇒ Hash{String => Array<String>}
Group error messages by the leading field name (e.g. “address.zip” => “address”)
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_h ⇒ Hash{Symbol => Object}
Structured hash representation of the result
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
12 13 14 |
# File 'lib/philiprehberger/schema_validator/result.rb', line 12 def valid? @errors.empty? end |