Class: Ippon::Validate::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/ippon/validate.rb

Overview

The base class for all schemas.

See Also:

Direct Known Subclasses

ForEach, Form, Merge, Sequence, Step, Unhalt

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ Merge

The merge operator applies its left and right schema (which must return Hashes) and merges the result into a combined value.

This is most commonly used together with form.

module Schemas
  extend Ippon::Validate::Builder

  Basic = form(
    username: fetch("username") | trim | required,
  )

  Advanced = form(
    karma: fetch("karma") | optional | number,
  )

  Both = Basic & Advanced
end

If either the left or the right schema causes the result to be halted, the final result will be halted as well.

Returns:



694
695
696
# File 'lib/ippon/validate.rb', line 694

def &(other)
  Merge.new(self, other)
end

#process(result) ⇒ Object

This method is abstract.

Process a result for the given schema. This must be overriden by subclasses to provide the expected behvior.

Raises:

  • (NotImplementedError)


650
651
652
653
654
# File 'lib/ippon/validate.rb', line 650

def process(result)
  # :nocov:
  raise NotImplementedError
  # :nocov:
end

#unhaltUnhalt

The unhalt schema applies its child schema and immediately unhalts the result.

This can be useful if you have non-critical validations and want to be able to provide multiple errors for the same value.

module Schemas
  extend Ippon::Validate::Builder

  Even = number | match(1..20).unhalt | validate { |val| val.even? }
end

result = Schemas::Even.validate("44")
result.errors.size  # => 2

In the example above the match(1..20) schema produced an error and halted the result, but due to the unhalt schema it was undone and validation continued.

Returns:



718
719
720
# File 'lib/ippon/validate.rb', line 718

def unhalt
  Unhalt.new(self)
end

#validate(value) ⇒ Result

Validates the input value and return a result.

Parameters:

  • value

    An untrusted input value

Returns:



629
630
631
632
# File 'lib/ippon/validate.rb', line 629

def validate(value)
  result = Result.new(value)
  process(result)
end

#validate!(value) ⇒ Object

Validates the input value and return the output value, raising an exception if an error occurs.

Parameters:

  • value

    An untrusted input value

Returns:

  • output value

Raises:



640
641
642
643
644
# File 'lib/ippon/validate.rb', line 640

def validate!(value)
  result = validate(value)
  raise ValidationError.new(result) if result.error?
  result.value
end

#|(other) ⇒ Sequence

The pipe operator applies its left schema first and if the result is not halted then it applies the right schema as well.

This let’s chain together multiple schemas that will be applied in order while short-circuiting when an error is produced.

(required | number).validate!(nil)  # => Error from required
(required | number).validate!("a")  # => Error from number
(required | number).validate!("1")  # => 1

Returns:



667
668
669
# File 'lib/ippon/validate.rb', line 667

def |(other)
  Sequence.new(self, other)
end