Skip to content

Definition generated from Schema #1

Description

@ricco24

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();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions