loopback-validations-mixin

Mixin for Loopback to easily configure validations from the model configuration file.

Showing:

Popularity

Downloads/wk

11

GitHub Stars

6

Maintenance

Last Commit

5yrs ago

Contributors

3

Package

Dependencies

1

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial