NAME
Declare::Constraints::Simple::Library - Constraint Library
DESCRIPTION
This is the constraint library for the Declare::Constraints::Simple module.
SCALAR CONSTRAINTS
Matches(@regex)
my $c = Matches(qr/foo/, qr/bar/);
If one of the parameters matches the expression, this is true.
IsDefined()
True if the value is defined
HasLength([$min, [$max]])
Is true if the value has a length above $min
(which defaults to 1> and, if supplied, under the value of $max
. A simple.
my $c = HasLength->($value);
Checks if the value has a length of at least 1.
IsOneOf(@values)
True if one of the @values
equals the passed value. undef
values work with this too, so
my $c = IsOneOf(1, 2, undef);
will return true on an undefined value.
IsTrue()
True if the value evulates to true.
NUMERICAL CONSTRAINTS
IsNumber()
True if the value is a number according to Scalar::Utils looks_like_number
.
IsInt()
True if the value is an integer.
OO CONSTRAINTS
IsA(@classes)
Is true if the passed object or class is a subclass of one of the classes mentioned in @classes
.
IsClass()
Valid if value is a loaded class.
HasMethods(@methods)
Returns true if the value is an object or class that can
all the specified @methods
.
The stack or path part of HasMethods
looks like HasMethods[$method]
where $method
is the first found missing method.
IsObject()
True if the value is blessed.
REFERENCIAL CONSTRAINTS
IsRefType(@types)
Valid if the value is a reference of a kind in @types
.
IsScalarRef($constraint)
This is true if the value is a scalar reference. A possible constraint for the scalar references target value can be passed. E.g.
IsScalarRef(IsInt)
IsArrayRef($constraint)
The value is valid if the value is an array reference. The contents of the array can be validated by passing an other $constraint
as argument.
The stack or path part of IsArrayRef
is IsArrayRef[$index]
where $index
is the index of the failing element.
IsHashRef(-keys => $constraint, -values => $constraint)
True if the value is a hash reference. It can also take two named parameters: -keys
can pass a constraint to check the hashes keys, -values
does the same for its values.
The stack or path part of IsHashRef
looks like IsHashRef[$type $key]
where $type
is either val
or key
depending on what was validated, and $key
being the key that didn't pass validation.
IsCodeRef()
Code references have to be valid to pass this constraint.
IsRegex()
True if the value is a regular expression built with qr
. Note however, that a simple string that could be used like /$rx/
will not pass this constraint. You can combine multiple constraints with And(@constraints) though.
ARRAY CONSTRAINTS
These deal with array references.
HasArraySize([$min, [$max]])
With $min
defaulting to 1. So a specification of
my $profile = HasArraySize;
Checks for at least one value. To force an exact size of the array, specify the same values for both.
my $profile = HasArraySize(3, 3);
HASH CONSTRAINTS
These are the constraints that are only for hash reference validation.
HasAllKeys(@keys)
The value has to be a hashref, and contain all keys listed in @keys
to pass this constraint.
The stack or path part of HasAllKeys
is HasAllKeys[$key]
where $key
is the missing key.
OnHashKeys(key => $constraint, key => $constraint, ...)
This allows you to pass a constraint for each specific key in a hash reference. If a specified key is not in the validated hash reference, the validation for this key is not done. To make a key a requirement, use HasAllKeys(@keys) above in combination with this, e.g. like:
And( HasAllKeys( qw(foo bar baz) )
OnHashKeys( foo => IsInt,
bar => Matches(qr/bar/),
baz => IsArrayRef( HasLength )));
Also, as you might see, you don't have to check for IsHashRef
validity here. The hash constraints are already doing that by themselves.
The stack or path part of OnHashKeys
looks like OnHashKeys[$key]
where $key
is the key of the failing value.
OPERATORS
Operators can be used in any place a constraint can be used, as their implementations are similar.
And(@constraints)
Is true if all passed @constraints
are true on the value.
Or(@constraints)
Is true if at least one of the passed @contraints
is true.
XOr(@constraints)
Valid only if a single one of the passed @constraints
is valid.
Not($constraint)
This is valid if the passed $constraint
is false. The main purpose of this operator is to allow the easy reversion of a constraint's trueness.
OTHER CONSTRAINT LIKE GENERATORS
Message($message, $constraint)
Overrides the message
set on the result object for failures in $constraint
.
METHODS AND SUBROUTINES
_result($bool, $message)
Internal subroutine. Creates a new result object. Validity is determined by the $bool
argument. The $message
is the message to be set if the validation failed.
_true()
Internal subroutine. Returns a true result.
_false($message)
Internal subroutine. Creates a new false result object with $message
.
_info($value)
Internal subroutine. Sets the info value that is used to determine current hash keys, array indexes and the like.
_listify($value)
Internal helper to force $value
into an array reference, if it isn't already one.
_apply_checks($value, $checks, $info)
Internal subroutine. This runs the $checks
on $value
and uses $info
to set the current info value on failure.
fetch_constraint_declarations()
Class method. This returns all constraints provided by this class.
fetch_constraint_generator($constraint_name)
Class method. Returns the constraint generator for this class with the name passed as $constraint_name
.
prepare_generator($constraint, $generator)
Class method. This prepares a constraint generator with the collapsing facilities needed for the stack and info data.
SEE ALSO
AUTHOR
Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at>
LICENSE AND COPYRIGHT
This module is free software, you can redistribute it and/or modify it under the same terms as perl itself.