You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can the invokable commands use validation? That is
Since you're looking at Closures for auto-completion, I wanted to brainstorm about the idea of assertions / validations in the commands.
One approach:
#[Argument(description: 'path to image directory', assert: fn(string$path) => is_dir($path), msg: "%s is not a valid path")]
string $path,
// to replace
): void {
if (!is_dir($path)) {
thrownew \LogicException("$path is not a directory");
}
Perhaps the assertion could also take a Symfony Expression?
It's not just individual elements that need validation, it might be the whole set (like a form validation).
#[Argument(description: 'image to process', assert: [$this, 'fileExists'])]
?string $imageFilename,
privatefunction fileExists() {
$pathToImage = $this->path . $this->imageFilename;
if (!file_exists($pathToImage)) {
thrownew \LogicException("$pathToImage is not a file");
}
Of course, even better would be to use the Symfony validators that already exist:
#[AsCommand('app:create-record', 'Create an image record with associated PDF')]
finalclassMyCommandextendsInvokableServiceCommand
{
useConfigureWithAttributes, RunsCommands, RunsProcesses;
publicfunction __invoke(
IO$io,
#[Argument(description: 'path to pdf')]
#[Assert\File(
mimeTypes: ['application/pdf', 'application/x-pdf'],
mimeTypesMessage: 'Please provide a valid PDF',
)]
string$pdf,
#[Argument(description: 'image filename')]
#[Assert\Image()]
string$image,
#[Option(description: 'path to pdf')]
#[Assert\File()]
?string$pdfPath="~/Documents/",
The Constraint would need to be extended, to allow for the property: \Attribute::TARGET_PARAMETER
Can the invokable commands use validation? That is
Since you're looking at Closures for auto-completion, I wanted to brainstorm about the idea of assertions / validations in the commands.
One approach:
Perhaps the assertion could also take a Symfony Expression?
It's not just individual elements that need validation, it might be the whole set (like a form validation).
Of course, even better would be to use the Symfony validators that already exist:
The Constraint would need to be extended, to allow for the property: \Attribute::TARGET_PARAMETER
Again, just brainstorming. It feels like there's an elegant solution in here somewhere.
The text was updated successfully, but these errors were encountered: