Customize validation message in node.js

Problem

I'm new to Node.js and I had a problem in customize validation message in node.js.I created a simple app with Compound JS using CRUD operation. In my app there is a field called "id". The "id" field only accept integer value only. Then I validate the field by using the following code in model/user.js.

module.exports = function (compound, User) {
 var num = /^\s*\d+\s*$/;
 User.validatesFormatOf('id', {with: num, message:"is not a number"});
};

By using the above code it work fine. But I also want to check whether the field is blank or not. Then I change the code a little bit. The modified code is look like this:

module.exports = function (compound, User) {
     var num = /^\s*\d+\s*$/;
     User.validatesFormatOf('id', {with: num, message: {blank: "can't be blank", with: "is not a number"}});
    };

Then the validation message if the field is blank will be displayed as this "Id can't be blank". But when I enter the value in the id field other than numbers the validation error message will be "Id [object Object]". I think the keyword with is not supported. Is here any other keyword like "blank" or "min" to use so I can get the validation message as "Id is not a number".

I found a solution for this so the modified version of user.js is like this:

 module.exports = function (compound, User) {
     var num = /^\s*\d+\s*$/;
     User.validatesPresenceOf('id', {message: "can't be blank"});
     User.validatesFormatOf('id', {with: num, message:"is not a number"});
    };

The problem with the above code is that it is displaying two validation message at the same time i.e. the default and custom blank message is displaying at the same time. My requirement is to show only one validation message for a field at a time. Is this possible?

Problem courtesy of: Jetson John

Solution

It looks like a bug in JugglingDB (the ORM used by CompoundJS).

Consider the following code:

var juggling = require('jugglingdb');
var Schema = juggling.Schema;

var schema = new Schema('memory');
var User = schema.define('User');
var num = /^\s*\d+\s*$/;

User.validatesPresenceOf('id', {message: "can't be blank"});
User.validatesFormatOf('id', {with: num, message:"is not a number"});
User.validatesFormatOf('id', {with: num, message:"is not a number"});

var user = new User({ id : '' });

user.isValid(function(valid) {
  if (! valid)
  {
    console.log('invalid', user.errors);
  }
  else
  {
    console.log('valid');
  }
});

This will actually generate 3 errors:

invalid { id: [ 'can\'t be blank', 'is blank', 'is blank' ] }

If you add another User.validatesFormat, it will generate 4 errors. It looks like a scoping problem somewhere in the validation code for JugglingDB, I'll post a bug report.

For now, perhaps you could just use the first error that occurs (user.errors[0]). EDIT: by the looks of it, that's what you also have to do if it would have worked, since JugglingDB will run all validation tests and generate an error for all failing tests.

Solution courtesy of: robertklep

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.