Class: Ippon::Validate::Result

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#errorsArray<StepError | NestedError> (readonly)

Returns the errors.

Returns:

  • (Array<StepError | NestedError>)

    the errors



517
518
519
# File 'lib/ippon/validate.rb', line 517

def errors
  @errors
end

#valueObject

Returns the current value.

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.

Parameters:

Returns:

  • (self)


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.

Yields:

  • (step, path)

Yield Parameters:

  • step (Step)
  • path (Array)

Returns:

  • (self | Enumerator)

    an enumerator if no block is given.



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.

Returns:

  • (Boolean)

    true if the result contains any errors.



586
587
588
# File 'lib/ippon/validate.rb', line 586

def error?
  !@errors.empty?
end

#error_messagesArray<String>

Returns array of error messages.

Returns:

  • (Array<String>)

    array of error messages.



575
576
577
578
579
580
581
582
583
# File 'lib/ippon/validate.rb', line 575

def error_messages
  each_step_error.map do |step, path|
    if path.any?
      "#{path.join('.')}: #{step.message}"
    else
      step.message
    end
  end
end

#haltself

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.

Returns:

  • (self)


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.

Returns:

  • (Boolean)

    true if the result has been halted.



598
599
600
# File 'lib/ippon/validate.rb', line 598

def halted?
  @is_halted
end

#mutable_errorsObject

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_errorsObject

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.

Returns:

  • (Boolean)

    true if the result contains zero errors.



591
592
593
# File 'lib/ippon/validate.rb', line 591

def success?
  !error?
end

#unhaltself

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.

Returns:

  • (self)


615
616
617
618
# File 'lib/ippon/validate.rb', line 615

def unhalt
  @is_halted = false
  self
end