digitalrevolution / symfony-console-validation
Symfony command input argument and option validation
Installs: 25 267
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 0
Forks: 1
Open Issues: 2
Type:symfony-bundle
Requires
- php: >=8.1
- digitalrevolution/symfony-validation-shorthand: ^1.0.4
- symfony/console: ^6.2 || ^7.0
- symfony/validator: ^6.2 || ^7.0
Requires (Dev)
- digitalrevolution/phpunit-file-coverage-inspection: ^v2.0.0
- phpmd/phpmd: ^2.14
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.6
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-10-31 00:23:18 UTC
README
Symfony Console Validation
An input validation component for Symfony Console. Ease the validation of input arguments and options.
Installation
Include the library as dependency in your own project via:
composer require digitalrevolution/symfony-console-validation
Usage
- Create your own
ExampleInput
class which extends theAbstractValidatedInput
class. - Configure your own
ValidationRules
. See the Validation shorthand library for more information about the rules. - Ensure the
InputValidator
class is registered as service in your Symfony project.
use DigitalRevolution\SymfonyConsoleValidation\AbstractValidatedInput; use DigitalRevolution\SymfonyConsoleValidation\ValidationRules; class ExampleInput extends AbstractValidatedInput { public static function getValidationRules(): ValidationRules { return new ValidationRules([ 'arguments' => [ 'email' => 'required|string|email' ], 'options' => [ 'projectId' => 'int:min:1' ] ]); } public function getEmail(): string { return $this->input->getArgument('email'); } public function getProjectId(): ?int { $value = $this->input->getOption('projectId'); return $value === null ? null : (int)$value; } }
All that remains is using your ExampleInput
class in your Command
to validate the input.
class ExampleCommand extends Command { public function __construct(private InputValidator $inputValidator, ?string $name = null) { parent::__construct($name); } protected function execute(InputInterface $input, OutputInterface $output): int { // third argument will throw exception if input is invalid. Set to `false` if you want to handle the validation yourself. $validatedInput = $this->inputValidator->validate($input, ExampleInput::class, true); ... } }
Manual invalid input handling
The validate
method will by default throw a ViolationException
. To handle the violations yourself:
class ExampleCommand extends Command { ... protected function execute(InputInterface $input, OutputInterface $output): int { $validatedInput = $this->inputValidator->validate($input, ExampleInput::class, false); if ($validatedInput->isValid() === false) { $violations = $validatedInput->getViolations(); ... } ... } }
About us
At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.