Subject: | Optional constraints ignored if wrapped in Dict |
See the test in attachment.
Subject: | validation-test.pl |
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use Type::Params qw(validate compile);
use Types::Standard qw(ArrayRef Dict Optional Str);
sub f {
validate(\@_, Optional[Str]);
}
is exception { f("foo") }, undef;
is exception { f() }, undef;
like exception { f(["abc"]) }, qr/type constraint/;
sub g {
validate(\@_, Dict[
foo => Optional[Str]
]);
}
is exception { g({ foo => "foo" }) }, undef;
is exception { g({}) }, undef;
like exception { g({ foo => ["abc"] }) }, qr/type constraint/;
done_testing;
=cut
Test output:
ok 1
ok 2
ok 3
ok 4
ok 5
Use of uninitialized value $this in pattern match (m//) at validation-test.pl line 27.
not ok 6
# Failed test at validation-test.pl line 27.
# undef
# doesn't match '(?^:type constraint)'
1..6
# Looks like you failed 1 test of 6.