Stannum

A library for defining and validating data structures.

Class: Stannum::Constraints::Properties::MatchProperty

Parent Namespace
Stannum::Constraints::Properties
Inherited Classes
Stannum::Constraints::Properties::Matching > Stannum::Constraints::Properties::Base > Stannum::Constraints::Base > Object
Defined In
lib/stannum/constraints/properties/match_property.rb

Table Of Contents

Overview

Compares the properties of the given object with the specified property.

If all of the property values equal the expected value, the constraint will match the object; otherwise, if there are any non-matching values, the constraint will not match.

Examples

Using an Properties::Match constraint

ConfirmPassword = Struct.new(:password, :confirmation)
constraint    = Stannum::Constraints::Properties::MatchProperty.new(
  :password,
  :confirmation
)

params = ConfirmPassword.new('tronlives', 'ifightfortheusers')
constraint.matches?(params)
#=> false
constraint.errors_for(params)
#=> [
  {
    path: [:confirmation],
    type: 'stannum.constraints.is_not_equal_to',
    data: { expected: '[FILTERED]', actual: '[FILTERED]' }
  }
]

params = ConfirmPassword.new('tronlives', 'tronlives')
constraint.matches?(params)
#=> true

Back To Top

Constants

FILTERED_PARAMETERS

= %i[ passw secret token _key crypt salt certificate otp ssn ].freeze

Inherited From
Stannum::Constraints::Properties::Base

Default parameter names to filter out of errors.

NEGATED_TYPE

= Stannum::Constraints::Equality::NEGATED_TYPE

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

TYPE

= Stannum::Constraints::Equality::TYPE

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

Back To Top

Constructor

#initialize(reference_name, *property_names, **options) => Matching

Inherited From
Stannum::Constraints::Properties::Matching

Parameters

Options Hash (options)

Returns

Back To Top

Instance Attributes

#options => Hash<Symbol, Object>

Inherited From
Stannum::Constraints::Base

Returns

#property_names => Array<String, Symbol> (readonly)

Inherited From
Stannum::Constraints::Properties::Base

Returns

#reference_name => String, Symbol (readonly)

Inherited From
Stannum::Constraints::Properties::Matching

Returns

Back To Top

Instance Methods

#==(other) => true, false

Inherited From
Stannum::Constraints::Base

Performs an equality comparison.

Parameters

Returns

#allow_empty? => true, false

Inherited From
Stannum::Constraints::Properties::Base

Returns

#allow_nil? => true, false

Inherited From
Stannum::Constraints::Properties::Base

Returns

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

Inherited From
Stannum::Constraints::Base

Produces a shallow copy of the constraint.

Parameters

Returns

#does_not_match?(actual) => true, false

Returns

#dup => Stannum::Constraints::Base

Inherited From
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

Inherited From
Stannum::Constraints::Base

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?

Returns

#message => String, nil

Inherited From
Stannum::Constraints::Base

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

Inherited From
Stannum::Constraints::Base

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

Inherited From
Stannum::Constraints::Base

Returns

#negated_type => String

Inherited From
Stannum::Constraints::Base

Returns

#type => String

Inherited From
Stannum::Constraints::Base

Returns

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

Inherited From
Stannum::Constraints::Base

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

Parameters

Returns

Back To Top


Back to Documentation | Versions | 0.4 | Reference | Stannum | Stannum::Constraints | Stannum::Constraints::Properties