schema icon indicating copy to clipboard operation
schema copied to clipboard

Definition generated from Schema

Open ricco24 opened this issue 6 years ago • 0 comments

What about some kind of Definition generation direct from Schema? It can be useful for implement some business logic depend on defined schema. Maybe something like this:

Basic type:

$schema = Expect::string()->required()->nullable();
$definition = $schema->getDefinition();
$definition->getType();
$definition->isRequired();
$definition->isNullable();

Array:

$schema = Expect::structure([
    'name' => Expect::string()->required(),
    'surname' => Expect::string()
]);
$definition = $schema->getDefinition();
foreach ($definition->getItems() as $item) {
    $item->isRequired();
    ...
}

Example usage:

// Some kind of input processing in presenter
$input = (new UberUserInput('id', $_GET))->setSchema(Expect::integer()->required());

try {
    $input->validate();
} catch (...) { ... }
// Generating form for user input
$form = new Form();
$form->addText($input->getName());
if ($input->getSchemaDefinition()->isRequired()) {
    $form[$input->getName()]->setRequired();
}

ricco24 avatar Apr 19 '19 21:04 ricco24