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 {
foreach ($this->argumentsValidator as $argumentName => $argumentValidator) {
if ($input->hasArgument($argumentName)) {
if (!$argumentValidator->validate($input->getArgument($argumentName))) {
throw new InvalidValueException(sprintf('The "%s" argument has not a valid value', $argumentName));
}
$input->setArgument($argumentName, $argumentValidator->getValue());
//region Check the argument still exists
if (!$input->hasArgument($argumentName)) {
continue;
}
//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 {
foreach ($this->optionsValidator as $optionName => $optionValidator) {
if ($input->hasOption($optionName)) {
if (!$optionValidator->validate($input->getOption($optionName))) {
throw new InvalidValueException(sprintf('The "--%s" option has not a valid value', $optionName));
}
$input->setOption($optionName, $optionValidator->getValue());
//region Check the option still exists
if (!$input->hasOption($optionName)) {
continue;
}
//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\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Hello extends CommandWithValidation {
@ -43,7 +44,7 @@ class Hello extends CommandWithValidation {
$this->addOption(
'repeat',
'r',
InputArgument::OPTIONAL,
InputOption::VALUE_OPTIONAL,
'The number of repeat',
1,
new IntegerValidator(1, null)

Loading…
Cancel
Save