Class: Ippon::Validate::Result
- Inherits:
-
Object
- Object
- Ippon::Validate::Result
- Defined in:
- lib/ippon/validate.rb
Overview
Represents a result from a validation (Schema#validate).
A result consists of a #value and a list of #errors (of StepError or NestedError). A result which contains zero errors is considered #valid? (or a #success?), while a result which has some errors is an #error?.
In addition, a result may or may not be #halted?. This is used by various schemas (e.g. Form and Sequence) to avoid continue processing. See Schema#unhalt for how to avoid halting in schemas.
module Schemas
extend Ippon::Validate::Builder
MaybeNumber = trim | optional | number
result = MaybeNumber.validate(" ")
result.valid? # => true; there are no errors
result.halted? # => true; but it was halted by `optional`
result.value # => nil
result = MaybeNumber.validate("123")
result.valid? # => true; there are no errors
result.halted? # => false; nothing caused this to halt
result.value # => 123
result = MaybeNumber.validate(" 12b3")
result.valid? # => false; it's not a valid number
result.halted? # => true; and thus it was halted
result.value # => "12b3"; and the value is not fully formed
end
Instance Attribute Summary collapse
-
#errors ⇒ Array<StepError | NestedError>
readonly
The errors.
-
#value ⇒ Object
The current value.
Instance Method Summary collapse
-
#add_nested(key, result) ⇒ self
Adds the state from a nested result:.
-
#each_step_error {|step, path| ... } ⇒ self | Enumerator
Yields every step which has produced an error, together with the path where it happened.
-
#error? ⇒ Boolean
True if the result contains any errors.
-
#error_messages ⇒ Array<String>
Array of error messages.
-
#halt ⇒ self
private
Halt the result.
-
#halted? ⇒ Boolean
True if the result has been halted.
-
#initialize(value) ⇒ Result
constructor
Creates a new Result with the given
value
. - #mutable_errors ⇒ Object private
-
#step_errors ⇒ Object
Returns a flat array of all steps which has produced an error, together with the path where it happened.
-
#success? ⇒ Boolean
(also: #valid?)
True if the result contains zero errors.
-
#unhalt ⇒ self
private
Unhalt the result.
Constructor Details
#initialize(value) ⇒ Result
Creates a new Result with the given value
.
520 521 522 523 524 |
# File 'lib/ippon/validate.rb', line 520 def initialize(value) @value = value @is_halted = false @errors = EMPTY_ERRORS end |
Instance Attribute Details
#errors ⇒ Array<StepError | NestedError> (readonly)
Returns the errors.
517 518 519 |
# File 'lib/ippon/validate.rb', line 517 def errors @errors end |
#value ⇒ Object
Returns the current value.
514 515 516 |
# File 'lib/ippon/validate.rb', line 514 def value @value end |
Instance Method Details
#add_nested(key, result) ⇒ self
Adds the state from a nested result:
-
If the
result
has any errors, it will be addded as a nested error under the given key. -
If the
result
is halted, this result will be halted as well.
543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/ippon/validate.rb', line 543 def add_nested(key, result) if result.error? mutable_errors.add_child(key, result) end if result.halted? halt end self end |
#each_step_error {|step, path| ... } ⇒ self | Enumerator
Yields every step which has produced an error, together with the path where it happened.
562 563 564 565 566 |
# File 'lib/ippon/validate.rb', line 562 def each_step_error(&blk) return enum_for(:each_step_error) if blk.nil? @errors.each_step_with_path(&blk) self end |
#error? ⇒ Boolean
Returns true if the result contains any errors.
586 587 588 |
# File 'lib/ippon/validate.rb', line 586 def error? !@errors.empty? end |
#error_messages ⇒ Array<String>
Returns array of error messages.
575 576 577 578 579 580 581 582 583 |
# File 'lib/ippon/validate.rb', line 575 def each_step_error.map do |step, path| if path.any? "#{path.join('.')}: #{step.}" else step. end end end |
#halt ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Halt the result.
606 607 608 609 |
# File 'lib/ippon/validate.rb', line 606 def halt @is_halted = true self end |
#halted? ⇒ Boolean
Returns true if the result has been halted.
598 599 600 |
# File 'lib/ippon/validate.rb', line 598 def halted? @is_halted end |
#mutable_errors ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
527 528 529 530 531 532 533 |
# File 'lib/ippon/validate.rb', line 527 def mutable_errors if EMPTY_ERRORS.equal?(@errors) @errors = Errors.new end @errors end |
#step_errors ⇒ Object
Returns a flat array of all steps which has produced an error, together with the path where it happened.
570 571 572 |
# File 'lib/ippon/validate.rb', line 570 def step_errors each_step_error.to_a end |
#success? ⇒ Boolean Also known as: valid?
Returns true if the result contains zero errors.
591 592 593 |
# File 'lib/ippon/validate.rb', line 591 def success? !error? end |
#unhalt ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Unhalt the result.
615 616 617 618 |
# File 'lib/ippon/validate.rb', line 615 def unhalt @is_halted = false self end |