npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loopback-validations-mixin

v1.0.1

Published

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

Downloads

13

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