Skip to content

Commit

Permalink
change required:no handling for undefined data
Browse files Browse the repository at this point in the history
See #1
  • Loading branch information
eserte committed Jul 16, 2015
1 parent 54b410c commit dc8dd12
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Changes
@@ -1,5 +1,14 @@
Revision history for Perl extension Kwalify.

1.22_90
- change handling of required:no for existing undefined data.
Now the behavior is like in the ruby implementation:
hash entries with an undefined value are treated as
missing, and thus pass the "required:no" rule.
This addresses https://github.com/eserte/p5-Kwalify/issues/1
- more tests
- enable travis-ci and coveralls

1.22
- cease possible "unitinialized value" warnings

Expand Down
7 changes: 4 additions & 3 deletions lib/Kwalify.pm
Expand Up @@ -3,7 +3,7 @@
#
# Author: Slaven Rezic
#
# Copyright (C) 2006,2007,2008,2009,2010 Slaven Rezic. All rights reserved.
# Copyright (C) 2006,2007,2008,2009,2010,2015 Slaven Rezic. All rights reserved.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
Expand All @@ -19,7 +19,7 @@ use base qw(Exporter);
use vars qw(@EXPORT_OK $VERSION);
@EXPORT_OK = qw(validate);

$VERSION = '1.22';
$VERSION = '1.22_90';

BEGIN {
if ($] < 5.006) {
Expand Down Expand Up @@ -342,12 +342,13 @@ sub validate_map {
$self->_die("Expected subschema (a hash)");
}
my $required = _get_boolean($subschema->{required});
if (!exists $data->{$key}) {
if (!defined $data->{$key}) {
if ($required) {
$self->{path} = $path;
$self->_error("Expected required key '$key'");
next;
} else {
$seen_key{$key}++;
next;
}
}
Expand Down
63 changes: 63 additions & 0 deletions t/required-no.t
@@ -0,0 +1,63 @@
#!/usr/bin/perl -w
# -*- cperl -*-

#
# Author: Slaven Rezic
#

use strict;

BEGIN {
if (!eval q{
use Test::More;
1;
}) {
print "1..0 # skip: no Test::More module\n";
exit;
}

if ($] < 5.005) {
print "1..0 # skip: test works only with perl 5.005 or better\n";
exit;
}
}

use Kwalify qw(validate);

plan tests => 1;

# from https://github.com/eserte/p5-Kwalify/issues/1
# translated yaml to perl
my $schema = {
'mapping' => {
'foo' => {
'mapping' => {
'bar' => {
'sequence' => [
{
'required' => 'no',
'type' => 'str'
}
],
'required' => 'no',
'type' => 'seq'
}
},
'required' => 'yes',
'type' => 'map'
}
},
'desc' => 'test for tilde',
'name' => 'test',
'type' => 'map'
};

my $data = {
'foo' => {
'bar' => undef
}
};

ok validate $schema, $data;

__END__

0 comments on commit dc8dd12

Please sign in to comment.