Validate: Allow multiple func validations in arrayref schema

This commit is contained in:
Yorhel 2025-03-14 09:25:56 +01:00
parent 1363e11269
commit f248a33c1c
2 changed files with 41 additions and 28 deletions

View file

@ -17,6 +17,7 @@ my %validations = (
onerrorsub => { onerror => sub { ref $_[1] } },
collapsews => { trim => 0, func => sub { $_[0] =~ s/\s+/ /g; 1 } },
neverfails => { onerror => 'err' },
doublefunc => [ func => sub { $_[0] == 0 }, func => sub { $_[0] = 2; 1; } ],
revnum => { type => 'array', sort => sub($x,$y) { $y <=> $x } },
uniquelength => { type => 'array', values => { type => 'array' }, unique => sub { scalar @{$_[0]} } },
person => {
@ -38,7 +39,7 @@ sub t($schema, $input, $output) {
my $input_copy = dclone([$input])->[0];
my $res = FU::Validate->compile($schema, \%validations)->validate($input);
#diag explain FU::Validate->compile($schema, \%validations) if $line == 139;
#diag explain FU::Validate->compile($schema, \%validations) if $line == 98;
is_deeply $schema, $schema_copy, "schema modification $line";
is_deeply $input, $input_copy, "input modification $line";
is_deeply $res, $output, "data ok $line";
@ -51,6 +52,7 @@ sub f($schema, $input, $error, @msg) {
my $input_copy = dclone([$input])->[0];
ok !eval { FU::Validate->compile($schema, \%validations)->validate($input); 1 }, "eval $line";
#diag explain FU::Validate->compile($schema, \%validations) if $line == 162;
is_deeply $schema, $schema_copy, "schema modification $line";
is_deeply $input, $input_copy, "input modification $line";
delete $@->{longmess};
@ -179,6 +181,8 @@ t { person => 1, unknown => 'remove' }, {name => 'x', sex => 'y', extra => 1}, {
t { neverfails => 1, int => 1 }, undef, 'err';
t { neverfails => 1, int => 1 }, 'x', 'err';
t { neverfails => 1, int => 1, onerror => undef }, 'x', undef; # XXX: no way to 'unset' an inherited onerror clause, hmm.
t { doublefunc => 1 }, 0, 2;
f { doublefunc => 1 }, 1, { validation => 'doublefunc', error => { validation => 'func', result => '' } }, "validation 'doublefunc': failed validation 'func'";
# numbers
sub nerr { ({ validation => 'num', got => $_[0] }, "failed validation 'num'") }