Skip to content
Permalink
Browse files

AnyOf: default value can be Schema (#12)

  • Loading branch information...
mabar authored and dg committed Oct 31, 2019
1 parent 5e30342 commit 337117df1dade22e2ba1fdc4a4b832c1e9b06b76
Showing with 26 additions and 0 deletions.
  1. +13 −0 src/Schema/Elements/AnyOf.php
  2. +13 −0 tests/Schema/Expect.anyOf.phpt
@@ -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;
}
}
@@ -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.
You can’t perform that action at this time.