diff --git a/FU/Validate.pm b/FU/Validate.pm index 6016b28..112d8cc 100644 --- a/FU/Validate.pm +++ b/FU/Validate.pm @@ -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; diff --git a/t/validate.t b/t/validate.t index 1481193..626dbbd 100644 --- a/t/validate.t +++ b/t/validate.t @@ -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';