Stannum

A library for defining and validating data structures.

Class: Stannum::Constraints::Base

Parent Namespace
Stannum::Constraints
Defined In
lib/stannum/constraints/base.rb

Table Of Contents

Overview

A constraint codifies a particular expectation about an object.

Back To Top

Direct Subclasses

Stannum::Constraint, Stannum::Constraints::Absence, Stannum::Constraints::Anything, Stannum::Constraints::Boolean, Stannum::Constraints::Delegator, Stannum::Constraints::Enum, Stannum::Constraints::Equality, Stannum::Constraints::Format, Stannum::Constraints::Hashes::ExtraKeys, Stannum::Constraints::Hashes::IndifferentKey, Stannum::Constraints::Identity, Stannum::Constraints::Nothing, Stannum::Constraints::Presence, Stannum::Constraints::Properties::Base, Stannum::Constraints::Signature, Stannum::Constraints::Tuples::ExtraItems, Stannum::Constraints::Type, Stannum::Constraints::Union, Stannum::Contracts::Base

Back To Top

Defined Under Namespace

Classes
Builder

Back To Top

Constants

NEGATED_TYPE

= 'stannum.constraints.valid'

The :type of the error generated for a matching object.

TYPE

= 'stannum.constraints.invalid'

The :type of the error generated for a non-matching object.

Back To Top

Constructor

#initialize(**options) => Base

Parameters

Options Hash (options)

Returns

Back To Top

Instance Attributes

#options => Hash<Symbol, Object>

Returns

Back To Top

Instance Methods

#==(other) => true, false

Performs an equality comparison.

Parameters

Returns

#clone(freeze: nil) => Stannum::Constraints::Base

Produces a shallow copy of the constraint.

Parameters

Returns

#does_not_match?(actual) => true, false

Checks that the given object does not match the constraint.

Examples

Checking a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new

constraint.does_not_match?(object) #=> false

Checking a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new

constraint.does_not_match?(object) #=> true

Parameters

Returns

See Also

#dup => Stannum::Constraints::Base

Produces a shallow copy of the constraint.

Returns

#errors_for(actual, errors: nil) => Stannum::Errors

Note: This method should only be called for an object that does not match the constraint. Generating errors for a matching object can result in undefined behavior.

Generates an errors object for the given object.

The errors object represents the difference between the given object and the expected properties or behavior. It may be the same for all objects, or different based on the details of the object or the constraint.

Examples

Generating errors for a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new
errors     = constraint.errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters

Returns

See Also

#match(actual) => Object

Checks the given object against the constraint and returns errors, if any.

This method checks the given object against the expected properties or behavior. If the object matches the constraint, #match will return true. If the object does not match the constraint, #match will return false and the generated errors for that object.

Examples

Checking a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new

success, errors = constraint.match(object)
success #=> true
errors  #=> nil

Checking a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new

success, errors = constraint.match(object)
success      #=> false
errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters

See Also

#matches?(actual) => true, false

Also known as: match?

Examples

Checking a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new

constraint.matches?(object) #=> true

Checking a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new

constraint.matches?(object) #=> false

Overloads

#matches?(actual) => true, false

Checks that the given object matches the constraint.

Parameters
  • actual (Object) — The object to match.
Returns
  • (true, false) — true if the object matches the expected properties or behavior, otherwise false.

See Also

#message => String, nil

Returns

#negated_errors_for(actual, errors: nil) => Stannum::Errors

Note: This method should only be called for an object that matches the constraint. Generating errors for a matching object can result in undefined behavior.

Generates an errors object for the given object when negated.

The errors object represents the difference between the given object and the expected properties or behavior when the constraint is negated. It may be the same for all objects, or different based on the details of the object or the constraint.

Examples

Generating errors for a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new
errors     = constraint.negated_errors_for(object)

errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Parameters

Returns

See Also

#negated_match(actual) => Object

Checks the given object against the constraint and returns errors, if any.

This method checks the given object against the expected properties or behavior. If the object matches the constraint, #negated_match will return false and the generated errors for that object. If the object does not match the constraint, #negated_match will return true.

Examples

Checking a matching object.

constraint = CustomConstraint.new
object     = MatchingObject.new

success, errors = constraint.negated_match(object)
success      #=> false
errors.class #=> Stannum::Errors
errors.to_a  #=> [{ type: 'some_error', message: 'some error message' }]

Checking a non-matching object.

constraint = CustomConstraint.new
object     = NonMatchingObject.new

success, errors = constraint.negated_match(object)
success #=> true
errors  #=> nil

Parameters

See Also

#negated_message => String, nil

Returns

#negated_type => String

Returns

#type => String

Returns

#with_options(**options) => Stannum::Constraints::Base

Creates a copy of the constraint and updates the copy’s options.

Parameters

Returns

Back To Top


Back to Documentation | Reference | Stannum | Stannum::Constraints