Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we support custom error message for Enumeration Validation? #3825

Open
dungdq2002 opened this issue Jun 5, 2024 · 3 comments
Open

Can we support custom error message for Enumeration Validation? #3825

dungdq2002 opened this issue Jun 5, 2024 · 3 comments

Comments

@dungdq2002
Copy link

Feature request

Since we can validate an Enumeration using sttp.tapir.Validator.Enumeration, I want to have a custom message for the invalid case.

Use case

For example, when we want to validate a list of enumeration inputs, if one of them fails the validation, the error message is too generic to specify which one is wrong.

Proposed solution

We can add an optional field to sttp.tapir.Validator.Enumeration as showMessageOpt: Option[T => String] to handle the error message.

Besides this approach, do we have any workaround? If not, I can help to implement this.

@adamw
Copy link
Member

adamw commented Jun 7, 2024

Can you share some more detail as to what kind of messages you are seeing now, and what you'd like to improve?

The error messages can already be customised (although globally) using the DecodeFailureHandler (see https://tapir.softwaremill.com/en/latest/server/errors.html#decode-failures, and

def invalidValueMessage[T](ve: ValidationError[T], valueName: String): String = {
- you can customise the default decode value handler with custom message rendering)

@dungdq2002
Copy link
Author

For example, we have a field pets: List[Pet], and we already define an enum Pet = { Cat, Dog }.
If users pass in List(Dog, Lion), the error message is only "pets: Invalid", and I think "pets: Lion invalid" or something like that will be more explainable.

Currently, we implement the schema as: Schema[Pet]: Schema(ValidatorEnum[Pet]), then Schema[pets]: Schema[Pet].asIterable.

@adamw
Copy link
Member

adamw commented Jun 10, 2024

@dungdq2002 is that a field in a json body, or a query/path parameter? some more details on your setup would be helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants