Skip to content

Labels In Error Messages Instead of Property Names #209

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

Closed
valeeum opened this issue Dec 23, 2020 · 7 comments
Closed

Labels In Error Messages Instead of Property Names #209

valeeum opened this issue Dec 23, 2020 · 7 comments
Labels

Comments

@valeeum
Copy link

valeeum commented Dec 23, 2020

Here is an example of current error message:

The 'email_address' field must not be empty.

Is there a way to pass field label into schema so the error message is displayed like so:

The 'Email Address' field must not be empty.

I know we can use custom messages for each field but curious if there's a lazier approach here :)

@erfanium
Copy link
Collaborator

Hi
I think displaying label in APIs instead of real property name is not good idea in general. What is the main reason you need this feature? Do you use fastest-validator from server-side?

@valeeum
Copy link
Author

valeeum commented Dec 23, 2020

@erfanium

For this particular use case, I'm using it client side for form validation.

image

@icebob
Copy link
Owner

icebob commented Dec 31, 2020

Workaround: use custom messages in the rules.

@erfanium
Copy link
Collaborator

erfanium commented Dec 31, 2020

@icebob It's not a good solution, for example look at string rule messages:

Name Default text
string The '{field}' field must be a string.
stringEmpty The '{field}' field must not be empty.
stringMin The '{field}' field length must be greater than or equal to {expected} characters long.
stringMax The '{field}' field length must be less than or equal to {expected} characters long.
stringLength The '{field}' field length must be {expected} characters long.
stringPattern The '{field}' field fails to match the required pattern.
stringContains The '{field}' field must contain the '{expected}' text.
stringEnum The '{field}' field does not match any of the allowed values.
stringNumeric The '{field}' field must be a numeric string.
stringAlpha The '{field}' field must be an alphabetic string.
stringAlphanum The '{field}' field must be an alphanumeric string.
stringAlphadash The '{field}' field must be an alphadash string.
stringHex The '{field}' field must be a hex string.
stringSingleLine The '{field}' field must be a single line string.

You will probably have to customize a lot of these messages for a single field.
Labels are more convenient way:

schema = {
    emailAddress: { type: "email", label: "Email Address" }
}

@stfullstack
Copy link

stfullstack commented Jun 28, 2021

I also think this would be a useful feature. You could have it default to the {field} unless you have provided the label.

Rather than calling it {field} in the message you could call it {path}. And then it would resolve to the label if you provided the label or default to the field. That way we can retain the use of {field} as it works now.

eg.

// elsewhere in code:
messages = { emailEmpty: "The '{path}' field must not be empty." };
schema = {
    emailAddress: { type: "email" }
}
// would result in error: The 'emailAddress' field must not be empty.

But if we provide the label

schema = {
    emailAddress: { type: "email", label: "Email Address" }
}
// would result in error: The 'Email Address' field must not be empty.

@icebob
Copy link
Owner

icebob commented Jun 28, 2021

We can't change the {field} to {path} because it cause breaking change. But for the label feature, we welcome PRs.

@yousufiqbal
Copy link

This is a must feature. Many coders use table column name, form field attribute name and schema property name same. So that dynamic communication of fields and errors are easy. This is the reason that this feature is available in yup also. Please implement it. This only keeps me from fastest-validator which is brilliant library no-doubt.

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

No branches or pull requests

5 participants