#!perl -w
use strict;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
# schema: EITHER username, email, OR userid is specified, but not both or all.
my $schema = [hash => {
some_of => [[
[str => {one_of => [qw/username email userid/]}],
[str => {required => 1}],
1, 1
]],
"some_of.errmsg" => "Please specify either username, email, OR userid",
}];
my @data;
# valid
$data[@data] = {email => 'foo@bar'};
# invalid because email is undefined
$data[@data] = {email => undef};
# invalid because none of username/email/userid is specified
$data[@data] = {};
# invalid because both email and username are specified
$data[@data] = {email => 'foo@bar', username => 'foo'};
for (0..$#data) {
print "data$_: ", Dumper($data[$_]), ": ";
my $res = ds_validate($data[$_], $schema);
# strip "data@... schema@... " prefix in error messages
for (@{ $res->{errors} }) {
s/^data\@\S* schema\@\S* //;
}
if ($res->{success}) {
print "valid";
} else {
print "invalid: ";
print join(", ", @{ $res->{errors} });
}
print "\n";
}