mirror of
https://codeberg.org/scip/Data-Validate-Struct.git
synced 2025-12-16 20:21:02 +01:00
revert 495fcbc, added test case for issue#7
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
0.12
|
||||
o revert commit 495fcbc, see #7: breaks backwards
|
||||
compatibility.
|
||||
|
||||
0.11
|
||||
o typos
|
||||
|
||||
|
||||
15
Struct.pm
15
Struct.pm
@@ -21,7 +21,7 @@ use File::stat;
|
||||
use Data::Validate qw(:math is_printable);
|
||||
use Data::Validate::IP qw(is_ipv4 is_ipv6);
|
||||
|
||||
our $VERSION = 0.11;
|
||||
our $VERSION = 0.12;
|
||||
|
||||
use vars qw(@ISA);
|
||||
|
||||
@@ -195,14 +195,7 @@ sub _traverse {
|
||||
my ($self, $reference, $hash, @tree) = @_;
|
||||
|
||||
foreach my $key (keys %{$reference}) {
|
||||
my $reference_ref = ref $reference->{$key};
|
||||
my $hash_ref = ref $hash->{$key};
|
||||
if ($reference_ref =~ /^ARRAY|HASH$/ && $reference_ref ne $hash_ref)
|
||||
{
|
||||
my $err = sprintf("Different structure types %s vs %s", $reference_ref, $hash_ref);
|
||||
push @{$self->{errors}}, sprintf(q{%s at '%s'}, $err, join(' => ', @tree));
|
||||
}
|
||||
elsif ($reference_ref eq 'ARRAY') {
|
||||
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)) {
|
||||
@@ -219,10 +212,10 @@ sub _traverse {
|
||||
}
|
||||
}
|
||||
}
|
||||
elsif ($reference_ref eq 'HASH') {
|
||||
elsif (ref($reference->{$key}) eq 'HASH') {
|
||||
$self->_traverse($reference->{$key}, $hash->{$key}, @tree, $key);
|
||||
}
|
||||
elsif ($reference_ref eq '') {
|
||||
elsif (ref($reference->{$key}) eq '') {
|
||||
$self->_debug("Checking $key at " . join(', ', @tree));
|
||||
if (my $err = $self->_check_type($key, $reference, $hash)) {
|
||||
push @{$self->{errors}}, sprintf(q{%s at '%s'}, $err, join(' => ', @tree));
|
||||
|
||||
73
t/run.t
73
t/run.t
@@ -48,7 +48,8 @@ my $ref = {
|
||||
'AoA' => [ [ 'int' ] ],
|
||||
|
||||
'AoH' => [
|
||||
{ fullname => 'text', user => 'word', uid => 'int' }
|
||||
{
|
||||
fullname => 'text', user => 'word', uid => 'int' }
|
||||
],
|
||||
|
||||
'HoH' => {
|
||||
@@ -110,9 +111,12 @@ my $cfg = {
|
||||
],
|
||||
|
||||
'AoH' => [
|
||||
{ fullname => 'Homer Simpson', user => 'homer', uid => 100 },
|
||||
{ fullname => 'Bart Simpson', user => 'bart', uid => 101 },
|
||||
{ fullname => 'Lisa Simpson', user => 'lisa', uid => 102 },
|
||||
{
|
||||
fullname => 'Homer Simpson', user => 'homer', uid => 100 },
|
||||
{
|
||||
fullname => 'Bart Simpson', user => 'bart', uid => 101 },
|
||||
{
|
||||
fullname => 'Lisa Simpson', user => 'lisa', uid => 102 },
|
||||
],
|
||||
|
||||
'HoH' => {
|
||||
@@ -128,7 +132,6 @@ my $v = new_ok('Data::Validate::Struct', [ $ref ]);
|
||||
ok ($v->validate($cfg), "validate a reference against a OK config");
|
||||
|
||||
|
||||
|
||||
# check failure matching
|
||||
my @failure = (
|
||||
{
|
||||
@@ -272,13 +275,17 @@ my @failure = (
|
||||
|
||||
{
|
||||
cfg => [
|
||||
{ fullname => 'Homer Simpson', user => 'homer', uid => 100 },
|
||||
{ fullname => 'Bart Simpson', user => ':bart', uid => 101 },
|
||||
{ fullname => 'Lisa Simpson', user => 'lisa', uid => '102' },
|
||||
{
|
||||
fullname => 'Homer Simpson', user => 'homer', uid => 100 },
|
||||
{
|
||||
fullname => 'Bart Simpson', user => ':bart', uid => 101 },
|
||||
{
|
||||
fullname => 'Lisa Simpson', user => 'lisa', uid => '102' },
|
||||
],
|
||||
|
||||
type => [
|
||||
{ fullname => 'text', user => 'word', uid => 'int' }
|
||||
{
|
||||
fullname => 'text', user => 'word', uid => 'int' }
|
||||
],
|
||||
|
||||
descr => 'array of hashes',
|
||||
@@ -325,7 +332,7 @@ my @failure = (
|
||||
errors => 1,
|
||||
},
|
||||
|
||||
);
|
||||
);
|
||||
|
||||
foreach my $test (@failure) {
|
||||
my $ref = { v => $test->{type} };
|
||||
@@ -339,8 +346,7 @@ foreach my $test (@failure) {
|
||||
my $errors = exists $test->{errors} ? $test->{errors} : 1;
|
||||
unless ($result) {
|
||||
is @{$v->errors}, $errors, "Caught failure for '$descr'";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
fail("Couldn't catch invalid '$test->{descr}'");
|
||||
}
|
||||
}
|
||||
@@ -350,10 +356,10 @@ foreach my $test (@failure) {
|
||||
undef $v;
|
||||
$v = Data::Validate::Struct->new({
|
||||
h1 => { h2 => { item => 'int' } }
|
||||
});
|
||||
});
|
||||
ok !$v->validate({
|
||||
h1 => { h2 => { item => 'qux' } }
|
||||
}), 'item is not an h1 => h2 => int';
|
||||
}), 'item is not an h1 => h2 => int';
|
||||
is $v->errstr, q{'qux' doesn't match 'int' at 'h1 => h2'}, 'correct error trace';
|
||||
|
||||
|
||||
@@ -363,20 +369,20 @@ my $ref3 = {
|
||||
v2 => 'list',
|
||||
v3 => 'noob',
|
||||
v4 => 'nonoob',
|
||||
};
|
||||
};
|
||||
my $cfg3 = {
|
||||
v1 => 'Marblestreet 15',
|
||||
v2 => 'a1, b2, b3',
|
||||
v3 => 42,
|
||||
v4 => 43,
|
||||
};
|
||||
};
|
||||
|
||||
my $v3 = new Data::Validate::Struct($ref3);
|
||||
# add via hash
|
||||
note('added via hash');
|
||||
my %h = (
|
||||
address => qr(^\w+\s\s*\d+$)
|
||||
);
|
||||
);
|
||||
$v3->type(%h);
|
||||
|
||||
# add via hash ref
|
||||
@@ -387,7 +393,7 @@ $v3->type({ list =>
|
||||
my @list = split /\s*,\s*/, $list;
|
||||
return scalar @list > 1;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
# add via key => value
|
||||
note('added via key => val');
|
||||
@@ -401,9 +407,30 @@ my $v4 = Data::Validate::Struct->new({age => 'int'});
|
||||
ok(!$v4->validate({age => 'eight'}), "cache check first run, error");
|
||||
ok($v4->validate({age => 8}), "cache check second run, no error");
|
||||
|
||||
# different references
|
||||
my $v5 = Data::Validate::Struct->new({ foo => [{bar => 'int'}]});
|
||||
ok(!$v5->validate({foo=>{bar=>10}}));
|
||||
|
||||
# optional array, see:
|
||||
# https://github.com/TLINDEN/Data-Validate-Struct/issues/7
|
||||
my $ref4 = {
|
||||
routers => [ {
|
||||
stubs => [ {
|
||||
network => 'ipv4',
|
||||
}, {} ],
|
||||
}, {}, ],
|
||||
};
|
||||
my $test4 = {
|
||||
'routers' => [
|
||||
{
|
||||
'stubs' => [
|
||||
{
|
||||
'network' => '172.31.199.0',
|
||||
}
|
||||
],
|
||||
'router' => '172.31.199.2', # optional, ignored by validate
|
||||
},
|
||||
{ # optional as well
|
||||
'router' => '172.30.5.5',
|
||||
},
|
||||
],
|
||||
};
|
||||
my $v4 = Data::Validate::Struct->new($ref4);
|
||||
ok($v4->validate($test4), "check optional " . $Data::Validate::Struct::VERSION);
|
||||
done_testing();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user