Skip to content

Parameter validation middleware for actionhero.js built on chriso's validator.js library

License

Notifications You must be signed in to change notification settings

dangerdespain/ah-validator-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ah-validator-plugin

Parameter validation middleware plugin for the actionhero.js API server built on Chris O'Hara's fantastic validator.js library and leaning on Sequelize's model validation definition/usage and validator extensions.

Installation

cd [actionhero-base-directory]
npm install ah-validator-plugin --save

Enable the plugin in Actionhero by adding 'ah-validator-plugin' to the plugins array in config/api.js

Usage

Validators are either defined locally in the action definitions or globally in the api.config.validation configuration object.

Each key in the object will validate the values of matching keys from the connection.params object. The validation methods and usage match the Sequelize implementation at http://sequelizejs.com/docs/1.7.8/models#validations

Local definitions override global definitions for each key.

A custom error message can also be added to a validation method by going a level deeper in the value and adding the keys 'args' and 'msg'. An example can be seen below in validate.email.notIn.

local example (inside an action definition)

inputs: {
    required: ['email', 'username'],
    optional: [],
},
validate: {
    'username' : {
      isAlpha : true,
    },
    'email' : {
      isEmail : true,
      notIn   : {
        args : ['[email protected]'],
        msg : '[email protected] is a reserved email. Please choose another.'
    }
},

global example (sample config/validate.js file)

exports.default = {
  validate: function (api) {
    return {
      global: {
        'uuid' : {
          isUUID : 4
        }
      }
    }
  }
};

License

MIT

About

Parameter validation middleware for actionhero.js built on chriso's validator.js library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published