Skip to content

Add extend() method to Structure class #66

@gauthierm

Description

@gauthierm

An extend() method would be nice for Structure so you could define a base schema and then define variants with additional fields. Right now you can kind of do this like:

$baseItems = [
  'foo' => Expect::string(),
  'bar' => Expect::string(),
];

$base = Expect::structure($baseItems);
$variant = Expect::structure([
  ...$baseItems,
  'baz' => Expect::string()
];

This has a downside in that you need to expose the array and pass around the array rather than passing around Structure objects. When composing schemas it would be really nice to have the following API:

$base = Expect::structure([
  'foo' => Expect::string(),
  'bar' => Expect::string(),
]);

// make a new structure with additional or overridden fields
$variant = $base->extend([
  'baz' => Expect::string(),
]);

This would be similar to a feature present in Zod https://zod.dev/?id=extend

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions