mirror of
https://codeberg.org/scip/Data-Validate-Struct.git
synced 2025-12-16 20:21:02 +01:00
Merge pull request #8 from ambs/PR/Rework_hash_derreference_bug
Rework hash vs array derreference
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
0.13
|
||||
o rework commit 495fcbc
|
||||
|
||||
0.12
|
||||
o revert commit 495fcbc, see #7: breaks backwards
|
||||
compatibility.
|
||||
|
||||
35
Struct.pm
35
Struct.pm
@@ -196,21 +196,30 @@ sub _traverse {
|
||||
|
||||
foreach my $key (keys %{$reference}) {
|
||||
if (ref($reference->{$key}) eq 'ARRAY') {
|
||||
# just use the 1st one, more elements in array are expected to be the same
|
||||
foreach my $item (@{$hash->{$key}}) {
|
||||
if (ref($item) eq q(HASH)) {
|
||||
# traverse the structure pushing our key to the @tree
|
||||
$self->_traverse($reference->{$key}->[0], $item, @tree, $key);
|
||||
}
|
||||
else {
|
||||
# a value, this is tricky
|
||||
$self->_traverse(
|
||||
{ item => $reference->{$key}->[0] },
|
||||
{ item => $item },
|
||||
@tree, $key
|
||||
);
|
||||
|
||||
# either it is undefined (optional values)
|
||||
# or it should be an array, so we can derreference it.
|
||||
if (!defined($hash->{$key}) || ref($hash->{$key}) eq "ARRAY") {
|
||||
|
||||
# just use the 1st one, more elements in array are expected to be the same
|
||||
foreach my $item (@{$hash->{$key}}) {
|
||||
if (ref($item) eq q(HASH)) {
|
||||
# traverse the structure pushing our key to the @tree
|
||||
$self->_traverse($reference->{$key}->[0], $item, @tree, $key);
|
||||
}
|
||||
else {
|
||||
# a value, this is tricky
|
||||
$self->_traverse(
|
||||
{ item => $reference->{$key}->[0] },
|
||||
{ item => $item },
|
||||
@tree, $key
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
push @{$self->{errors}}, "$key is not an array";
|
||||
}
|
||||
}
|
||||
elsif (ref($reference->{$key}) eq 'HASH') {
|
||||
$self->_traverse($reference->{$key}, $hash->{$key}, @tree, $key);
|
||||
|
||||
13
t/run.t
13
t/run.t
@@ -409,14 +409,14 @@ ok($v4->validate({age => 8}), "cache check second run, no error");
|
||||
|
||||
# optional array, see:
|
||||
# https://github.com/TLINDEN/Data-Validate-Struct/issues/7
|
||||
my $ref4 = {
|
||||
my $ref5 = {
|
||||
routers => [ {
|
||||
stubs => [ {
|
||||
network => 'ipv4',
|
||||
}, {} ],
|
||||
}, {}, ],
|
||||
};
|
||||
my $test4 = {
|
||||
my $test5 = {
|
||||
'routers' => [
|
||||
{
|
||||
'stubs' => [
|
||||
@@ -431,6 +431,11 @@ my $test4 = {
|
||||
},
|
||||
],
|
||||
};
|
||||
my $v4 = Data::Validate::Struct->new($ref4);
|
||||
ok($v4->validate($test4), "check optional " . $Data::Validate::Struct::VERSION);
|
||||
my $v5 = Data::Validate::Struct->new($ref5);
|
||||
ok($v5->validate($test5), "check optional " . $Data::Validate::Struct::VERSION);
|
||||
|
||||
# different references
|
||||
my $v6 = Data::Validate::Struct->new({ foo => [{bar => 'int'}]});
|
||||
ok(!$v6->validate({foo=>{bar=>10}}));
|
||||
|
||||
done_testing();
|
||||
|
||||
Reference in New Issue
Block a user