Description
Perhaps a bad title, but I think this would be nice to have (standardizing "failure" responses in tempest apps).
For example, here, where the request validations passed, but we need to do additional "validation":
#[Post('/webauthn/authenticate')]
public function authenticate(AuthenticatePasskeyRequest $request, Authenticator $authenticator)
{
// some deserialization and other logic
$passkey = query(Passkey::class)->find(credential_id: $publicKeyCredential->rawId)->first();
if (!$passkey) {
// here id like to throw a validation exception
}
In laravel there is the ValidationException::withMessages() which is what I am missing I suppose. It is possible to do something like that with ValidationFailed but it requires a lot of boilerplate + a rule to make the message.
throw new ValidationFailed([
'answer' => [
new FailingRule(rule: new MessageRule('Passkey not valid')),
],
]);
//
final readonly class MessageRule implements Rule, HasErrorMessage
{
public function __construct(
private string $message,
) {}
public function isValid(mixed $value): bool
{
return false;
}
public function getErrorMessage(): string
{
return $this->message;
}
}
Perhaps this is fine, and all that is needed is some GenericRule that takes a message?
(Another thing I found when testing this is that Validator::getErrorMessage doesn't check the HasErrorMessage if the rule is in a FailingRule)
Or is there a better way to handle these cases that I am missing?
Description
Perhaps a bad title, but I think this would be nice to have (standardizing "failure" responses in tempest apps).
For example, here, where the request validations passed, but we need to do additional "validation":
In laravel there is the
ValidationException::withMessages()which is what I am missing I suppose. It is possible to do something like that withValidationFailedbut it requires a lot of boilerplate + a rule to make the message.Perhaps this is fine, and all that is needed is some GenericRule that takes a message?
(Another thing I found when testing this is that
Validator::getErrorMessagedoesn't check theHasErrorMessageif the rule is in aFailingRule)Or is there a better way to handle these cases that I am missing?