项目作者: Aghabeiki

项目描述 :
A multipurpose param validator for SailsJS
高级语言: JavaScript
项目地址: git://github.com/Aghabeiki/paramValidator.git
创建时间: 2017-08-11T03:19:14Z
项目社区:https://github.com/Aghabeiki/paramValidator

开源协议:MIT License

下载


Param-Validator

A Param Validator in Middleware for Sailsjs/Express.js

note: \
this project is under heavy developments.

Installing

Install node module

  1. npm i param-validator

Add it as a middelware to your sails http.js .

  1. paramValidator: function (req, res, next) {
  2. let paramValidator = new ParamValidator(sails.config.ParamValidator
  3. , sails.log, sails.config.routes, sails.config.environment);
  4. paramValidator.validator(req, res, next);
  5. },

Config :

Create a config file under Config folder with any name and put this content on it.

  1. module.exports.ParamValidator = {
  2. projectBaseDIR: require('path').resolve(__dirname, '../'),
  3. validatorBaseRepo: 'api/ParamValidator/ValidatorBank',
  4. scriptBaseRepo: 'api/ParamValidator/ValidatorBank/script',
  5. excludePrefix: '^(\/upload\/center).*$',
  6. }
  • validatorBaseRepo :

    This config is required, and point to the path of your JSON defenition of param validation

  • scriptBaseRepo :

    This cinfig is required, and point to the path of your script defenition of param validation

  • excludePrefix :

    A regex that exclude any path from param validator

Param Validator
The JSON Object properties
  • URL \
    1. The URL that should validate ( should be like route config )
  • METHOD \
    1. The METHOD should be GET, PUT, DELETE or POST
  • BODY or SCRIPT \

    1. The way of validations :D
    Validation config
  • 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`
        • Multi data type could assign to a param with separator |
      • required: define the param is required or not,default value is true
        • Depend properties
        • type number
          • min : minimum acceptable value
          • max : maximum acceptable value
        • type string
          • minLength : minimum acceptable lenght of string
          • maxLength : maximum acceptable lenght of string
          • length : acceptable lenght of string
        • type array
          • rows: in the rows we can define the param and flow the validation config as well.
        • type object
          • body: the properties of the JSON Object and can use all the validation config as well.
    • Addition operation

      • compareWithFiled : compare one pram with another ones
        • this param defined as an array, each rows of this arrasy should defined with a single string like operator:param_name
        • acceptable operators: all the valid logical compare operand in JS!
          • regex64 : validate a param with regex,this param should define in config file as an regex that encoded with base64
  • SCRIPT: In this method you can write your own script to validate your param in your way

    • this param should contain the path of your validatior script file
    • the file should be under your scriptBaseRepo path
    • in valid param checking the script should return true value
    • on any invalidation just throw an Error
    • any Error message that you throwen will be send and formated in response
    • the script file should be like :
  1. module.exports.validator = function (params) {
  2. "use strict";
  3. return true;
  4. }

Some Validator Config

  • Simple JSON Config
    1. {
    2. "URL": "/test/:param1/callcenter/:param2",
    3. "METHOD": "GET",
    4. "BODY": {
    5. "param1": {
    6. "type": "number"
    7. },
    8. "param2": {
    9. "type": "number",
    10. "min": 1,
    11. "max": 4,
    12. "required": false
    13. },
    14. }
    15. }
  • Array Base JSON Config
    1. [
    2. {
    3. "URL": "/test1",
    4. "METHOD": "get",
    5. "BODY": {
    6. "date_from": {
    7. "type": "date",
    8. "required": false,
    9. "regex64": "aBase64String",
    10. "compareWithFiled": [
    11. "<:date_to"
    12. ]
    13. },
    14. "date_to": {
    15. "type": "date",
    16. "required": false,
    17. "regex64": "aBase64String",
    18. "compareWithFiled": [
    19. ">:date_from"
    20. ]
    21. }
    22. }
    23. },
    24. {
    25. "URL": "/test2",
    26. "METHOD": "get",
    27. "SCRIPT": "./script/testScript2.js"
    28. }
    29. ]
  • single JSON Config
    1. {
    2. "URL": "/test3",
    3. "METHOD:"post",
    4. "BODY":{
    5. "details": {
    6. "type": "array",
    7. "required": false,
    8. "rows": {
    9. "type": "object",
    10. "body": {
    11. "detail_id": {
    12. "type": "number"
    13. },
    14. "language": {
    15. "type": "string",
    16. "required": false,
    17. "regex64": "aBase64String",
    18. "length": 5
    19. },
    20. "title": {
    21. "type": "string",
    22. "required": false,
    23. "maxLength": 1024
    24. },
    25. "channel": {
    26. "type": "string",
    27. "required": false
    28. }
    29. }
    30. }
    31. }
    32. }
    33. }
  • A complext Config Desing
    ```
    {
    “group a”: [
    {
    1. "URL": "/test5/:id",
    2. "METHOD": "DELETE",
    3. "SCRIPT": "./script/something.js"
    },
    {
    1. "URL": "/test6",
    2. "METHOD": "POST",
    3. "SCRIPT": "./script/something2.js"
    }
    ],
    “group b”: [
    {
    1. "URL": "/test7",
    2. "METHOD": "PUT",
    3. "SCRIPT": "./script/something3.js"
    }
    ]
    }

```

Authors

  • Amin Aghabeiki

License

This project is licensed under the MIT License - see the LICENSE file for details