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 Error). 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.


453
454
455
456
457
458
# File 'lib/ippon/validate.rb', line 453

def initialize(value)
  @value = value
  @is_halted = false
  @errors = []
  @path = [].freeze
end

Instance Attribute Details

#errorsArray<Error> (readonly)

Returns the errors.

Returns:

  • (Array<Error>)

    the errors


446
447
448
# File 'lib/ippon/validate.rb', line 446

def errors
  @errors
end

#pathArray (readonly)

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.

Returns the current path.

Returns:

  • (Array)

    the current path


450
451
452
# File 'lib/ippon/validate.rb', line 450

def path
  @path
end

#valueObject

Returns the current value.

Returns:

  • the current value


443
444
445
# File 'lib/ippon/validate.rb', line 443

def value
  @value
end

Instance Method Details

#add_error(step) ⇒ 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.

Add an error from a step.

Parameters:

Returns:

  • (self)

509
510
511
512
# File 'lib/ippon/validate.rb', line 509

def add_error(step)
  @errors << Error.new(@path, step)
  self
end

#add_errors_from(result) ⇒ 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.

Copy over errors from another result.

Parameters:

Returns:

  • (self)

519
520
521
522
# File 'lib/ippon/validate.rb', line 519

def add_errors_from(result)
  @errors.concat(result.errors)
  self
end

#error?Boolean

Returns true if the result contains any errors.

Returns:

  • (Boolean)

    true if the result contains any errors.


461
462
463
# File 'lib/ippon/validate.rb', line 461

def error?
  @errors.any?
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)

481
482
483
484
# File 'lib/ippon/validate.rb', line 481

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.


473
474
475
# File 'lib/ippon/validate.rb', line 473

def halted?
  @is_halted
end

#push_path(key) ⇒ 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.

Push a new element to the #path.

Returns:

  • (self)

499
500
501
502
# File 'lib/ippon/validate.rb', line 499

def push_path(key)
  @path = [*@path, key].freeze
  self
end

#success?Boolean Also known as: valid?

Returns true if the result contains zero errors.

Returns:

  • (Boolean)

    true if the result contains zero errors.


466
467
468
# File 'lib/ippon/validate.rb', line 466

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)

490
491
492
493
# File 'lib/ippon/validate.rb', line 490

def unhalt
  @is_halted = false
  self
end