class Linearly::Validation::Validator
{Validator} is a stateful helper applying expecations to a {State}
Public Class Methods
new(source, expectations, state)
click to toggle source
Constructor method for a {Validator}
@param source [Object] source of the validation, to be passed to errors
for better messaging.
@param expectations [Hash<Symbol, Expectation>] @param state [Statefully::State]
@api private
# File lib/linearly/validation.rb, line 82 def initialize(source, expectations, state) @source = source @expectations = expectations @state = state end
Public Instance Methods
validate(error_class)
click to toggle source
Validate wrapped {State}, failing it with an error class if needed
@param error_class [Class]
@return [Statefully::State] @api private
# File lib/linearly/validation.rb, line 94 def validate(error_class) failures = invalid.merge(missing).freeze return @state if failures.empty? @state.fail(error_class.new(@source, failures)) end
Private Instance Methods
invalid()
click to toggle source
Return the invalid fields
@return [Hash<Field, Failure::Unexpected>] @api private
# File lib/linearly/validation.rb, line 106 def invalid @invalid ||= @expectations.map do |key, expectation| next nil if missing.key?(key) value = @state.fetch(key) next nil if expectation.call(value) [key, Failure::Unexpected.instance] end.compact.to_h end
missing()
click to toggle source
Return the missing fields
@return [Hash<Field, Failure::Missing>] @api private
# File lib/linearly/validation.rb, line 119 def missing @missing ||= @expectations .keys .reject { |key| @state.key?(key) } .map { |key| [key, Failure::Missing.instance] } .to_h end