Validator : do not check Null or False (default value / not provided)

2.x 2.0.1
Julien Rosset 2 years ago
parent 0ef982f8b7
commit f83533fc69

@ -58,12 +58,29 @@ trait TCommandWithValidation {
*/ */
protected function validateArguments (InputInterface $input, OutputInterface $output): void { protected function validateArguments (InputInterface $input, OutputInterface $output): void {
foreach ($this->argumentsValidator as $argumentName => $argumentValidator) { foreach ($this->argumentsValidator as $argumentName => $argumentValidator) {
if ($input->hasArgument($argumentName)) { //region Check the argument still exists
if (!$argumentValidator->validate($input->getArgument($argumentName))) { if (!$input->hasArgument($argumentName)) {
throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName)); continue;
}
$input->setArgument($argumentName, $argumentValidator->getValue());
} }
//endregion
//region Get the argument value
$argumentValue = $input->getArgument($argumentName);
//endregion
//region Check the argument value is not Null
if ($argumentValue === null) {
// If the value is strictly Null (the default value), don't check it → skip
continue;
}
//endregion
//region Check the value is valid
if (!$argumentValidator->validate($argumentValue)) {
throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName));
}
//endregion
//region Replace the value by the one extracted from the validator
$input->setArgument($argumentName, $argumentValidator->getValue());
//endregion
} }
} }
/** /**
@ -78,12 +95,29 @@ trait TCommandWithValidation {
*/ */
protected function validateOptions (InputInterface $input, OutputInterface $output): void { protected function validateOptions (InputInterface $input, OutputInterface $output): void {
foreach ($this->optionsValidator as $optionName => $optionValidator) { foreach ($this->optionsValidator as $optionName => $optionValidator) {
if ($input->hasOption($optionName)) { //region Check the option still exists
if (!$optionValidator->validate($input->getOption($optionName))) { if (!$input->hasOption($optionName)) {
throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName)); continue;
} }
$input->setOption($optionName, $optionValidator->getValue()); //endregion
//region Get and check the option value
$optionValue = $input->getOption($optionName);
//endregion
//region Check the option value is NOT Null or False
if ($optionValue === null || $optionValue === false) {
// If the value is strictly Null (the default value or not provided) or False (the default value), don't check it → skip
continue;
}
//endregion
//region Check the value is valid
if (!$optionValidator->validate($optionValue)) {
throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName));
} }
//endregion
//region Replace the value by the one extracted from the validator
$input->setOption($optionName, $optionValidator->getValue());
//endregion
} }
} }

@ -11,6 +11,7 @@ use jrosset\CliProgram\Validation\Validators\IntegerValidator;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
class Hello extends CommandWithValidation { class Hello extends CommandWithValidation {
@ -43,7 +44,7 @@ class Hello extends CommandWithValidation {
$this->addOption( $this->addOption(
'repeat', 'repeat',
'r', 'r',
InputArgument::OPTIONAL, InputOption::VALUE_OPTIONAL,
'The number of repeat', 'The number of repeat',
1, 1,
new IntegerValidator(1, null) new IntegerValidator(1, null)

Loading…
Cancel
Save