Skip to content

Commit

Permalink
Support Junction as StructureValidator schema node
Browse files Browse the repository at this point in the history
  • Loading branch information
japhb committed Aug 29, 2021
1 parent a7e9357 commit f63f26d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/MUGS/Util/StructureValidator.rakumod
Expand Up @@ -46,6 +46,44 @@ sub validate-structure($type, $data, $schema, $path = 'root') is export {

validate(data{$_}, schema{$_}, path ~ "/$_") for schema.keys;
}
elsif nqp::istype(schema, Junction) {
my str $jtype = nqp::getattr(nqp::decont(schema), Junction, '$!type');
my $jstates := nqp::getattr(nqp::decont(schema), Junction, '$!eigenstates');

my $path = path ~ "/$jtype\(…)";
if $jtype eq 'any' {
for $jstates -> $state {
try validate(data, $state, $path);
last unless $!;
}
X::MUGS::InvalidStructure.new(:$type, :$path, :data(data),
:error('no matching any junction variant')).throw if $!;
}
elsif $jtype eq 'all' {
validate(data, $_, $path) for $jstates;
}
elsif $jtype eq 'none' {
for $jstates -> $state {
try validate(data, $state, $path);
X::MUGS::InvalidStructure.new(:$type, :$path, :data(data),
:error('matched a none junction variant')).throw unless $!;
}
}
elsif $jtype eq 'one' {
my int $count = 0;
for $jstates -> $state {
try validate(data, $state, $path);
X::MUGS::InvalidStructure.new(:$type, :$path, :data(data),
:error('matched too many one junction variants')).throw if !$! && $count++;
}
X::MUGS::InvalidStructure.new(:$type, :$path, :data(data),
:error('no matching one junction variant')).throw;
}
else {
X::MUGS::InvalidStructure.new(:$type, :$path, :data(data),
:error("schema uses unknown Junction type '$jtype'")).throw;
}
}
else {
X::MUGS::InvalidStructure.new(:$type, :path(path), :data(data),
:error("must be {schema.raku}")).throw
Expand Down

0 comments on commit f63f26d

Please sign in to comment.