Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

loopback-validations-mixin

1.0.1 • Public • Published

loopback-validations-mixin

Mixin for Loopback to easily configure validations from the model configuration file. It works with both Loopback 2 and 3.

Installation

npm install --save loopback-validations-mixin

Configuration

First, modify your server/model-config.json to include the path to this module:

{
  "mixins": [
    "loopback/common/mixins",
    "loopback/server/mixins",
    "../common/mixins",
    "./mixins",
    "../node_modules/loopback-validations-mixin"
  ]
}

Then you use the mixin from your model definition files:

...
"mixins"{
  "SetupValidations": {
    "validatesPresenceOf": [
      "name",
      { "propertyName": "address", "errMsg": { "message": "Error property cannot be blank" } }
    ]
  }
}
...

Options

  • All available methods in Loopback Validate
  • methodsFile (optional)
  • source (optional)
  • include (optional)

If source is defined, it will take into account only the validations defined in such file, and ignore the ones in the JSON configuration file.

Usage

You can define the validations directly in the model JSON configuration file, or in a JS file.

From JS File

You can use this variant when you need the properties to have some value calculated on runtime.

You can add all the validations defined on a file:

...
"mixins"{
  "SetupValidations": {
    "source": "./common/models/employee-validations.js"
  }
}
...

Or just some of them:

...
"mixins"{
  "SetupValidations": {
    "source": "./common/models/employee-validations.js",
    "include": [ "validatesPresenceOf" ]
  }
}
...

In the Javascript file (employee-validations.js in our example):

const validatesPresenceOf = [
  'name',
  {
    'propertyName': 'lastName',
    'errMsg': {'message': 'Error property cant be blank'}
  }
];
 
const validatesLengthOf = [
  {
    'propertyName': 'name',
    'options': {'min': 6}
  },
  {
    'propertyName': 'address',
    'options': {
      'max': 10,
      'allowNull': true,
      'allowBlank': true,
      'message': {'max': 'Invalid size'},
    },
  },
];
 
const validatesAsync = [
  {
    'propertyName': 'name',
    'validatorFn': validatePropertyName,
    'options': {
      'message': 'error message',
      'allowNull': false,
    },
  },
];
 
function validatePropertyName(err, done) {
  let data = this;
  if (data.name === 'Invalid') err();
  done();
}
 
module.exports = {
  validatesPresenceOf,
  validatesLengthOf,
  validatesAsync,
};

Do not forget to add allowNull and allowBlank if the property is optional. Otherwise it will fail when the parameter is not passed.

From JSON

In the model.json file:

...
"mixins"{
  "SetupValidations": {
 
    "validatesPresenceOf": [
      "name",
      {
        "propertyName": "lastName",
        "errMsg": { "message": "You should have a lastname" }
      }
    ],
 
    "validatesLengthOf": [{
      "propertyName": "name",
      "options": {
        "min": 10
      }
    }, {
      "propertyName": "address",
      "options": {
        "max": 10,
        "allowNull": true,
        "allowBlank": true,
        "message": { "max": "invalid size" }
      }
    }],
 
    "validatesAsync": [{
      "propertyName": "name",
      "validatorFn": "validatePropertyName",
      "options": {
        "message": "this is invalid"
      }
    }],
    "methodsFile": "./common/models/employee-validation-methods.js"
  }
}
...

As you can see, you can set an optional methodsFile option, to define the file that contains the methods needed by validatesAsync and validates options.

As mentioned above, make sure you include the allowNull and allowBlank properties in the options of the non-required properties.

In the employee-validation-methods.js file:

module.exports = {
  validatePropertyName
};
 
function validatePropertyName (err, done) {
  let data = this;
  if (data.name === 'Invalid') err();
  done();
}

Credits

Created by Devsu.

Copyright Devsu LLC, 2016.

License: MIT

install

npm i loopback-validations-mixin

Downloadsweekly downloads

17

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar