A multipurpose param validator for SailsJS
A Param Validator in Middleware for Sailsjs/Express.js
note: \
this project is under heavy developments.
Install node module
npm i param-validator
Add it as a middelware to your sails http.js .
paramValidator: function (req, res, next) {
let paramValidator = new ParamValidator(sails.config.ParamValidator
, sails.log, sails.config.routes, sails.config.environment);
paramValidator.validator(req, res, next);
},
Create a config file under Config folder with any name and put this content on it.
module.exports.ParamValidator = {
projectBaseDIR: require('path').resolve(__dirname, '../'),
validatorBaseRepo: 'api/ParamValidator/ValidatorBank',
scriptBaseRepo: 'api/ParamValidator/ValidatorBank/script',
excludePrefix: '^(\/upload\/center).*$',
}
This config is required, and point to the path of your JSON defenition of param validation
This cinfig is required, and point to the path of your script defenition of param validation
A regex that exclude any path from param validator
The URL that should validate ( should be like route config )
The METHOD should be GET, PUT, DELETE or POST
BODY or SCRIPT \
The way of validations :D
BODY : In this method, you can define a JSON file to validate your param automatically
Independed properties
type
: number, date, string, array, object, email, phone, boolean`|
required
: define the param is required or not,default value is truenumber
string
array
object
Addition operation
compareWithFiled
: compare one pram with another onesoperator:param_name
regex64
: validate a param with regex,this param should define in config file as an regex that encoded with base64SCRIPT: In this method you can write your own script to validate your param in your way
scriptBaseRepo
pathtrue
value
module.exports.validator = function (params) {
"use strict";
return true;
}
{
"URL": "/test/:param1/callcenter/:param2",
"METHOD": "GET",
"BODY": {
"param1": {
"type": "number"
},
"param2": {
"type": "number",
"min": 1,
"max": 4,
"required": false
},
}
}
[
{
"URL": "/test1",
"METHOD": "get",
"BODY": {
"date_from": {
"type": "date",
"required": false,
"regex64": "aBase64String",
"compareWithFiled": [
"<:date_to"
]
},
"date_to": {
"type": "date",
"required": false,
"regex64": "aBase64String",
"compareWithFiled": [
">:date_from"
]
}
}
},
{
"URL": "/test2",
"METHOD": "get",
"SCRIPT": "./script/testScript2.js"
}
]
{
"URL": "/test3",
"METHOD:"post",
"BODY":{
"details": {
"type": "array",
"required": false,
"rows": {
"type": "object",
"body": {
"detail_id": {
"type": "number"
},
"language": {
"type": "string",
"required": false,
"regex64": "aBase64String",
"length": 5
},
"title": {
"type": "string",
"required": false,
"maxLength": 1024
},
"channel": {
"type": "string",
"required": false
}
}
}
}
}
}
},
"URL": "/test5/:id",
"METHOD": "DELETE",
"SCRIPT": "./script/something.js"
}
"URL": "/test6",
"METHOD": "POST",
"SCRIPT": "./script/something2.js"
}
"URL": "/test7",
"METHOD": "PUT",
"SCRIPT": "./script/something3.js"
```
This project is licensed under the MIT License - see the LICENSE file for details