Validate: Fix referencing & merging already compiled schemas
This commit is contained in:
parent
65cf842500
commit
a7bfe146b1
2 changed files with 16 additions and 1 deletions
|
|
@ -103,12 +103,14 @@ sub _new { bless { validations => [], @_ }, __PACKAGE__ }
|
|||
|
||||
|
||||
sub _compile($schema, $custom, $rec, $top, $validations=$top->{validations}) {
|
||||
my $iscompiled = $schema isa __PACKAGE__;
|
||||
|
||||
# For hashref schemas, builtins always override other validations
|
||||
$schema = [
|
||||
map +($_, $schema->{$_}),
|
||||
(grep !$builtin{$_}, keys %$schema),
|
||||
(grep $builtin{$_}, keys %$schema),
|
||||
] if ref $schema eq 'HASH';
|
||||
] if ref $schema ne 'ARRAY';
|
||||
|
||||
for my($name, $val) (@$schema) {
|
||||
if ($name eq 'type') {
|
||||
|
|
@ -159,6 +161,11 @@ sub _compile($schema, $custom, $rec, $top, $validations=$top->{validations}) {
|
|||
next;
|
||||
}
|
||||
|
||||
if ($iscompiled && $name eq 'validations') {
|
||||
push @$validations, @$val;
|
||||
next;
|
||||
}
|
||||
|
||||
my $t = $custom->{$name} || $default_validations{$name};
|
||||
confess "Unknown validation: $name" if !$t;
|
||||
confess "Recursion limit exceeded while resolving validation '$name'" if $rec < 1;
|
||||
|
|
|
|||
|
|
@ -275,6 +275,14 @@ t { weburl => 1}, $_, $_ for (
|
|||
'https://blicky.net/?#Who\'d%20ever%22makeaurl_like-this/!idont.know',
|
||||
);
|
||||
|
||||
# Merging nested schemas
|
||||
my $pa = FU::Validate->compile({ regex => '^a' });
|
||||
my $pz = FU::Validate->compile({ regex => 'z$' });
|
||||
my $com = FU::Validate->compile([ elems => $pa, elems => $pz ]);
|
||||
is_deeply $com->validate(['axz']), ['axz'];
|
||||
ok !eval { $com->validate(['bz', 'axz', 'ax']) };
|
||||
is [$@->errors]->[0], "[0]: failed validation 'regex'";
|
||||
is [$@->errors]->[1], "[2]: failed validation 'regex'";
|
||||
|
||||
# Things that should fail
|
||||
ok !eval { FU::Validate->compile({ recursive => 1 }, { recursive => { recursive => 1 } }); 1 }, 'recursive';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue