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:


605
606
607
# File 'lib/ippon/validate.rb', line 605

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)

561
562
563
564
565
# File 'lib/ippon/validate.rb', line 561

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:


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

def unhalt
  Unhalt.new(self)
end

#validate(value) ⇒ Result

Validates the input value and return a result.

Parameters:

  • value

    An untrusted input value

Returns:


539
540
541
542
543
# File 'lib/ippon/validate.rb', line 539

def validate(value)
  result = Result.new(value)
  process(result)
  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:


551
552
553
554
555
# File 'lib/ippon/validate.rb', line 551

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:


578
579
580
# File 'lib/ippon/validate.rb', line 578

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