Class: Validate::Assertions::Assertion

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

Instance Method Summary collapse

Constructor Details

#initialize(validation_context) ⇒ Assertion

Returns a new instance of Assertion.



13
14
15
# File 'lib/validate/assertions.rb', line 13

def initialize(validation_context)
  @constraints = validation_context
end

Instance Method Details

#assert(value, error_class: Error::ArgumentError, message: 'invalid value') ⇒ Object

Raises:

  • (error_class)


17
18
19
20
21
22
23
24
# File 'lib/validate/assertions.rb', line 17

def assert(value, error_class: Error::ArgumentError, message: 'invalid value')
  ctx = Constraints::ValidationContext.root(value)
  @constraints.evaluate(ctx)
  return value unless ctx.has_violations?

  raise error_class, message,
        cause: ctx.to_err
end