Search code examples
validationloopbackjs

Change User default validation in Loopback


I'm developping a Loopback application extending base User model to UserCode model where each user is identified by an email plus a code fields.

So that a user can register with the same email twice but with different code.

I've seen that in node_modules/loopback/common/models/user.js at line 691 there is:

UserModel.validatesUniquenessOf('email', {message: 'Email already exists'});

I want to delete this restriction/validation but without change loopback code, of course.

How can I do it? Maybe in the boot script I can loop through all validation and delete this one?


Solution

  • Figured it out

    In this case you need to remove the default validations set by the User model

    common/models/userCode.js

    module.exports = function(UserCode){
       //Add this line and it will start receiving multiple email.
       delete UserCode.validations.email;
    }
    

    Also you can play with the required:true|false property to make any default defined property required or not.

    common/models/userCode.json

    {
      "name": "UserCode",
      "base": "User",
      "idInjection": true,
      "properties": {
        "password": {
          "type": "string",
          "required": true
        },
        ....
        ....
    }