fastest-validator icon indicating copy to clipboard operation
fastest-validator copied to clipboard

Labels In Error Messages Instead of Property Names

Open valeeum opened this issue 4 years ago • 7 comments

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 :)

valeeum avatar Dec 23 '20 19:12 valeeum

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?

erfanium avatar Dec 23 '20 19:12 erfanium

@erfanium

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

image

valeeum avatar Dec 23 '20 19:12 valeeum

Workaround: use custom messages in the rules.

icebob avatar Dec 31 '20 10:12 icebob

@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" }
}

erfanium avatar Dec 31 '20 22:12 erfanium

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.

stfullstack avatar Jun 28 '21 15:06 stfullstack

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

icebob avatar Jun 28 '21 17:06 icebob

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.

yousufiqbal avatar Oct 26 '21 15:10 yousufiqbal