Declare::Constraints::Simple::Library::Hash - Hash Constraints
my $constraint = And( # make sure all keys are present HasAllKeys( qw(foo bar) ), # constraints for the keys OnHashKeys( foo => IsInt, bar => HasLength ) );
This module contains all constraints that can be applied to hash references.
The value has to be a hashref, and contain all keys listed in @keys to pass this constraint.
@keys
The stack or path part of HasAllKeys is HasAllKeys[$key] where $key is the missing key.
HasAllKeys
HasAllKeys[$key]
$key
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.
IsHashRef
The stack or path part of OnHashKeys looks like OnHashKeys[$key] where $key is the key of the failing value.
OnHashKeys
OnHashKeys[$key]
Declare::Constraints::Simple, Declare::Constraints::Simple::Library
Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at>
<phaylon@dunkelheit.at>
This module is free software, you can redistribute it and/or modify it under the same terms as perl itself.
To install Declare::Constraints::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Declare::Constraints::Simple
CPAN shell
perl -MCPAN -e shell install Declare::Constraints::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.