LIVR icon indicating copy to clipboard operation
LIVR copied to clipboard

Human-friendly validation messages for `like` rules

Open alanchrt opened this issue 8 years ago • 1 comments

I really appreciate the idea of semantic error codes as opposed to numeric codes or messages.

However, specifically in the case of like, exclusively returning the error code can lead to bad user experience. In the example,

https://github.com/koorchik/LIVR#like

displaying to the user "First name must contain only letters, numbers, or underscores" is a lot more helpful than saying "First name is in the wrong format."

Regexes can get complicated, and not having a way to communicate the constraints of the check back to the consuming code (whatever language is doing the validating) means that either the consuming code has to be aware of what the validation does and couple it to the LIVR rules, defeating the purpose of using LIVR, or it has to settle for degraded UX and un-informative validation messages.

Would you be open to considering optional human-friendly validation messages to be returned alongside the error codes? Consumers could check for a human-friendly message if they wanted, then fall back to the code when none is provided.

alanchrt avatar May 11 '16 17:05 alanchrt

You can register rule first_name, and return specific error codes for each cases:

LIVR.Validator.registerDefaultRules({
  first_name: function() {
    return function(name) {
      if (!name) {
        return;
      }

      if (typeof(name) != 'string') {
        return 'FORMAT_ERROR';
      }

      if (/\s/.test(name)) {
        return 'SPACES_NOT_ALLOWED'
      }

      if (/\n/.test(name)) {
        return 'LINEBREAKS_NOT_ALLOWED'
      }

      // any checks and related error codes

      return; //valid first name
    }
  }
})

Widely usage of like rule is not recommended.

DanielHreben avatar May 15 '16 20:05 DanielHreben