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

Custom error messages #14

Open
donpaul120 opened this issue Jul 24, 2017 · 2 comments
Open

Custom error messages #14

donpaul120 opened this issue Jul 24, 2017 · 2 comments

Comments

@donpaul120
Copy link

There should be a way to set custom error messages that the default error message.

@sitegui
Copy link
Member

sitegui commented Jul 25, 2017

Hello @donpaul120 ,

Can you give us some example of your use case, please?

I can see two paths to achieve that. The first is to redefine error messages globally, something like:

let validate = require('validate-fields')()
validate.rewriteError('I was expecting a value greater than {}', 'Gimme more! At least {}')

Or something more granular, defined at the field level, but with a big drawback of changing syntax in some non-backwards compatible manner. Like:

let validate = require('validate-fields')()
validate({
  a: {
    $type: 'uint(12)',
    $message: '{VALUE} is less than {1}. {PATH} demands change!'
  }
}, {a: 2})
validate.lastError = '2 is less than 12. a demands change!'

The second approach introduces a lot of space for future extensions.

@donpaul120
Copy link
Author

donpaul120 commented Jul 27, 2017

Sorry i'm a bit late..
Hmmmm... The second approach best fits my use case..

class model{
   constructor(){
    }

   rules(){
      return {
          name:String,
       };
   }
}
class modelService{
   createModel(data){
     let model = new Model(data);
     let isValid = validate(model.rules(), model);
      if(!isValid) return Promise.rejects(Util.buildApiResponse('fail', {message:validate.lastError}));
     .....
    }
}

So i wanted to have a custom error message than sticking to the defacto.
But it seems option 2 that you provided seems to be a perfect fix for me so i can define everything in my model.

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

No branches or pull requests

2 participants