Skip to content

Commit

Permalink
AnyOf: default value can be Schema (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and dg committed Oct 31, 2019
1 parent 5e30342 commit 337117d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Schema/Elements/AnyOf.php
Expand Up @@ -99,4 +99,17 @@ public function complete($value, Nette\Schema\Context $context)
$context->addError("The option %path% expects to be $hints, " . static::formatValue($value) . ' given.');
}
}


public function completeDefault(Context $context)
{
if ($this->required) {
$context->addError('The mandatory option %path% is missing.');
return null;
}
if ($this->default instanceof Schema) {
return $this->default->completeDefault($context);
}
return $this->default;
}
}
13 changes: 13 additions & 0 deletions tests/Schema/Expect.anyOf.phpt
Expand Up @@ -161,3 +161,16 @@ test(function () { // processing
Assert::same('two', $processor->processMultiple($schema, ['one', 'two']));
Assert::same(null, $processor->processMultiple($schema, ['one', null]));
});


test(function () { // Schema as default value
$default = Expect::structure([
'key2' => Expect::string(),
])->castTo('array');

$schema = Expect::structure([
'key1' => Expect::anyOf(false, $default)->default($default),
])->castTo('array');

Assert::same(['key1' => ['key2' => null]], (new Processor)->process($schema, null));
});

0 comments on commit 337117d

Please sign in to comment.