This commit is contained in:
Javier Casares 2026-08-24 11:42:10 +00:00
commit 80a2202d6e
18 changed files with 305 additions and 122 deletions

View file

@ -39,6 +39,9 @@ final class Structure implements Schema
}
/**
* Not supported for structures; always throws.
*/
public function default(mixed $value): self
{
throw new Nette\InvalidStateException('Structure cannot have default value.');
@ -59,6 +62,9 @@ final class Structure implements Schema
}
/**
* Allows extra keys not defined in the shape, validating their values against the given type.
*/
public function otherItems(string|Schema $type = 'mixed'): self
{
$this->otherItems = $type instanceof Schema ? $type : new Type($type);
@ -66,6 +72,9 @@ final class Structure implements Schema
}
/**
* When enabled, properties whose value equals the default are omitted from the output.
*/
public function skipDefaults(bool $state = true): self
{
$this->skipDefaults = $state;
@ -73,7 +82,10 @@ final class Structure implements Schema
}
/** @param Schema[]|self $shape */
/**
* Creates a new structure by merging this shape with additional properties.
* @param Schema[]|self $shape
*/
public function extend(array|self $shape): self
{
$shape = $shape instanceof self ? $shape->items : $shape;
@ -203,8 +215,17 @@ final class Structure implements Schema
public function completeDefault(Context $context): mixed
{
return $this->required
? $this->complete([], $context)
: null;
if (!$this->required) {
return null;
}
// the item is missing in the input, do not report it as used deprecated
$deprecated = $this->deprecated;
$this->deprecated = null;
try {
return $this->complete([], $context);
} finally {
$this->deprecated = $deprecated;
}
}
}