1つの配列に10匹モンスターを返す必要ある、でもいない場合があって、そのときはundefを返してほしい!!とか配列にn個必ず返すみたいなのあるって聞いた。
id1, id2, id3, id4みたいなのが、多いと厳しい。
use strict; use warnings; use Mouse::Util::TypeConstraints; use Data::Validator; use Test::More; sub fill { my ($list) = @_; my $max = 10; die "fuck" if $max < scalar(@$list); [@$list, map { undef } 1..($max - scalar(@$list))] } is scalar(@{fill([1..10])}), 10; is scalar(@{fill([1..3])}), 10; is scalar(@{fill([])}), 10; my $MyArrayRef = subtype 'MyArrayRef', as 'ArrayRef', where { scalar(@$_) == 10 }; coerce $MyArrayRef, from 'ArrayRef', via { fill($_) }; my $v = Data::Validator->new( foo => $MyArrayRef, ); subtest "success" => sub { my $args = $v->validate({ foo => [] }); is scalar(@{$args->{foo}}), 10; $args = $v->validate({ foo => [1..3] }); is scalar(@{$args->{foo}}), 10; $args = $v->validate({ foo => [1..10] }); is scalar(@{$args->{foo}}), 10; }; subtest "err" => sub { eval { $v->validate({ foo => {} }) }; like $@, qr/Validation failed/; }; done_testing;