line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Declare::Constraints::Simple::Library::Hash - Hash Constraints |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Declare::Constraints::Simple::Library::Hash; |
8
|
12
|
|
|
12
|
|
73
|
use warnings; |
|
12
|
|
|
|
|
22
|
|
|
12
|
|
|
|
|
363
|
|
9
|
12
|
|
|
12
|
|
64
|
use strict; |
|
12
|
|
|
|
|
23
|
|
|
12
|
|
|
|
|
441
|
|
10
|
|
|
|
|
|
|
|
11
|
12
|
|
|
12
|
|
65
|
use Declare::Constraints::Simple-Library; |
|
12
|
|
|
|
|
18
|
|
|
12
|
|
|
|
|
185
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $constraint = And( |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# make sure all keys are present |
18
|
|
|
|
|
|
|
HasAllKeys( qw(foo bar) ), |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# constraints for the keys |
21
|
|
|
|
|
|
|
OnHashKeys( foo => IsInt, bar => HasLength ) |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
This module contains all constraints that can be applied to hash |
28
|
|
|
|
|
|
|
references. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head2 HasAllKeys(@keys) |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The value has to be a hashref, and contain all keys listed in |
33
|
|
|
|
|
|
|
C<@keys> to pass this constraint. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
The stack or path part of C is C where |
36
|
|
|
|
|
|
|
C<$key> is the missing key. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
constraint 'HasAllKeys', |
41
|
|
|
|
|
|
|
sub { |
42
|
|
|
|
|
|
|
my @vk = @_; |
43
|
|
|
|
|
|
|
return sub { |
44
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
45
|
|
|
|
|
|
|
return _false('Not a HashRef') unless ref($_[0]) eq 'HASH'; |
46
|
|
|
|
|
|
|
for (@vk) { |
47
|
|
|
|
|
|
|
unless (exists $_[0]{$_}) { |
48
|
|
|
|
|
|
|
_info($_); |
49
|
|
|
|
|
|
|
return _false("No '$_' key present"); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
return _true; |
53
|
|
|
|
|
|
|
}; |
54
|
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 OnHashKeys(key => $constraint, key => $constraint, ...) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
This allows you to pass a constraint for each specific key in |
59
|
|
|
|
|
|
|
a hash reference. If a specified key is not in the validated |
60
|
|
|
|
|
|
|
hash reference, the validation for this key is not done. To make |
61
|
|
|
|
|
|
|
a key a requirement, use L above in combination |
62
|
|
|
|
|
|
|
with this, e.g. like: |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
And( HasAllKeys( qw(foo bar baz) ) |
65
|
|
|
|
|
|
|
OnHashKeys( foo => IsInt, |
66
|
|
|
|
|
|
|
bar => Matches(qr/bar/), |
67
|
|
|
|
|
|
|
baz => IsArrayRef( HasLength ))); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Also, as you might see, you don't have to check for C |
70
|
|
|
|
|
|
|
validity here. The hash constraints are already doing that by |
71
|
|
|
|
|
|
|
themselves. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
The stack or path part of C looks like C |
74
|
|
|
|
|
|
|
where C<$key> is the key of the failing value. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
constraint 'OnHashKeys', |
79
|
|
|
|
|
|
|
sub { |
80
|
|
|
|
|
|
|
my %def = my @def = @_; |
81
|
|
|
|
|
|
|
my @key_order; |
82
|
|
|
|
|
|
|
while (my $key = shift @def) { |
83
|
|
|
|
|
|
|
my $val = shift @def; |
84
|
|
|
|
|
|
|
push @key_order, $key; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
return sub { |
87
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
88
|
|
|
|
|
|
|
return _false('Not a HashRef') unless ref($_[0]) eq 'HASH'; |
89
|
|
|
|
|
|
|
for (@key_order) { |
90
|
|
|
|
|
|
|
my @vc = @{_listify($def{$_})}; |
91
|
|
|
|
|
|
|
next unless exists $_[0]{$_}; |
92
|
|
|
|
|
|
|
my $r = _apply_checks($_[0]{$_}, \@vc, $_); |
93
|
|
|
|
|
|
|
return $r unless $r->is_valid; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
return _true; |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head1 SEE ALSO |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
L, L |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 AUTHOR |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Robert 'phaylon' Sedlacek Cphaylon@dunkelheit.atE> |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
This module is free software, you can redistribute it and/or modify it |
110
|
|
|
|
|
|
|
under the same terms as perl itself. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |