Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Create custom message #45

Open
tolbard opened this issue Jul 8, 2016 · 3 comments
Open

Create custom message #45

tolbard opened this issue Jul 8, 2016 · 3 comments

Comments

@tolbard
Copy link

tolbard commented Jul 8, 2016

Can we be able to override the specific validation error message by a other message.

For example:
username: s.optional(s.regex(/^(?=.*[a-z])[1-9a-z][0-9a-z]{3,19}$/i))

change to :
username: s.optional(s.regex(/^(?=.*[a-z])[1-9a-z][0-9a-z]{3,19}$/i, message:'Please follow this rules for your username: Alphanumeric characters only, maximum of 20 characters, minimum of 4 characters, at least one letter, first character cannot be 0, whitespace is not allowed'))

@rprieto
Copy link
Contributor

rprieto commented Jul 8, 2016

That would be nice, especially for regexes. In your example, another option that's quite clean is to wrap it into a higher-level matcher, e.g. something like

var Username = s.createMatcher({
  initialize: function() {
    this.regex = s.regex(/^(?=.*[a-z])[1-9a-z][0-9a-z]{3,19}$/i);
  },
  match: function(path, value) {
    var errors = this.regex.match(path, value);
    if (errors) {
      return 'Please follow these rules for your username: ....';
    }
  }
});

// and then
var schema = {
  username: s.optional(new Username())
};

@lyricnz
Copy link

lyricnz commented Nov 11, 2019

There's a PR on this - please consider
#118

@lyricnz
Copy link

lyricnz commented Apr 13, 2020

#118 was merged. This can be closed.

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

No branches or pull requests

3 participants