Skip to content

Make ValidationFailed throwable without rules #2194

Description

@NeoIsRecursive

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions