A library for defining and validating data structures.
Constraint for matching objects by the methods they respond to.
constraint = Stannum::Constraints::Signature.new(:[], :keys)
constraint.matches?(Object.new) #=> false
constraint.matches?([]) #=> false
constraint.matches?({}) #=> true
Stannum::Constraints::Signatures::Map, Stannum::Constraints::Signatures::Tuple
NEGATED_TYPE
= 'stannum.constraints.has_methods'
The :type of the error generated for a matching object.
TYPE
= 'stannum.constraints.does_not_have_methods'
The :type of the error generated for a non-matching object.
#initialize(*expected_methods, **options) => Signature
#expected_methods => Array<String, Symbol> (readonly)
#options => Hash<Symbol, Object>
#==(other) => true, false
Performs an equality comparison.
#clone(freeze: nil) => Stannum::Constraints::Base
Produces a shallow copy of the constraint.
#does_not_match?(actual) => true, false
#dup => Stannum::Constraints::Base
Produces a shallow copy of the constraint.
#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.
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' }]
#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.
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' }]
#matches?(actual) => true, false
Also known as:
match?
#message => String, nil
#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.
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' }]
#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.
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
#negated_message => String, nil
#negated_type => String
#type => String
#with_options(**options) => Stannum::Constraints::Base
Creates a copy of the constraint and updates the copy’s options.
Back to Documentation | Reference | Stannum | Stannum::Constraints