express - Customize validation message in node.js -


I am new to node. Js and I had a problem in the required verification message in the node. I use a simplified CRUD operation with the compound JS app in my app "field id" field "id" field only accepts the integer value then I use the following code in model / user.js I agree.

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

This works fine using the code above. But I also want to see if the field is empty or not. Then I change the code a little bit. The modified code looks like this:

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

The verification message will appear when the field is empty, because "id can not be empty" but when I enter values ​​in the id field other than numbers, the verification error message "id [ Object object] "will be. I think the keyword is not supported with it. To use any other keyword here, "blank" or "m" like I can get the verification message "id number is not there".

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

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

The problem with the above code is that it is displaying two validity messages at the same time i.e. The default and custom empty message is appearing at the same time. I need to show only one verification message for a field at a time, is it possible?

This juggling looks like a bug in DB (ORM used by CompoundJS).

Consider the following code:

  var juggling = require ('jugglingdb'); Var Schema = Slippers Sama; Var schema = new schema ('memory'); Var user = schema.define ('user'); Var num = / ^ \ s * \ d + \ s * $ / / User.validatesPresenceOf ('id', {message: "can not be empty"}); User.validatesFormatOf ('id', {with: num, message: "no number is"}); User.validatesFormatOf ('id', {with: num, message: "no number is"}); Var user = new user ({id: ''}); User.isValid (function (valid) {if (valid!) {Console.log ('invalid', user.error);} and {console.log ('valid');}});   

This will actually generate 3 errors:

  invalid {id: ['' can not be empty '', 'is empty', ' empty ']}   

If you add another User.validatesFormat , it will generate 4 errors it looks like a scoping problem elsewhere In the verification code for jugglingDB, I will post a bug report.

For now, maybe you're just the first error ( user.errors [0] ). Edit: Seeing this, it is also what you also have to do, because JugglingDB will run all verification tests and will produce an error for all unsuccessful tests.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -