1.0.20 • Published 5 years ago

object-checker v1.0.20

Weekly downloads
25
License
MIT
Repository
github
Last release
5 years ago

object-checker-js

NPM version Downloads

Notice: the package is updated to v1.0.0 and APIs are changed:

APIbefore v1.0.0after v1.0.0
isValid()Check the object and throw Exceptions when invalidRenamed to verify()
checkObject()Check the object and return check result.Renamed to check()
isValidObject()Check the object and return true/false for resultRenamed to isValid()
bodyCheckMiddlewareMiddleware for expressRenamed to expressBodyCheckMiddleware
errorHandlerHandler for middlewareRenamed to expressErrorHandler

A tool for checking object. And also provide a middleware for express.

  • No validation codes.
  • Easy to combine with other validation package.
  • Can make router.js in Express as an API document.

Quick Example:

var objectChecker = require('object-checker');

var checker = objectChecker.createObjectChecker()
var obj = {
  users: [
    {
      name:"a@a.com",
      additional:{
        age   : 20,
        height: 180,
        score : [80, 90, 100]
      }
    },
    {
      name:"123@b.com"
    },
    {
      name:"123@a.com",
      additional: {
        age   : 100,
        height:200,
        score : [60, 70, 80, 90]
      }
    }
  ]
};

var opt = {
  users: {
    $maxLength: 5,
    $: {
      name: {
        $isEmail  : true,
        $minLength: 6,
        $maxLength: 10
      },
      additional: {
        $isOptional: true,
        age: {
          $minValue: 20,
          $maxValue: 100
        },
        height: {
          $minValue: 100,
          $maxValue: 200
        },
        score: {
          $minLength: 3,
          $: {
            $minValue: 60,
            $maxValue: 100
          }
        }
      }
    }
  }
};

console.log(checker.check(obj, opt));

Use as an Express Middleware

// router.js

var express                    = require('express');
var expressBodyCheckMiddleware = require('object-checker').expressBodyCheckMiddleware;

var router = express.Router();

var opt = {
  username: {
    $isEmail: true
  },
  password: {
    $minLength: 6,
    $maxLength: 20
  }
};
router.post('/users', expressBodyCheckMiddleware(opt), handlerFunction);

module.exports = router;

Play with other modules

// router.js

var express                    = require('express');
var expressBodyCheckMiddleware = require('object-checker').expressBodyCheckMiddleware;
var validator                  = require('validator'); // 3rd-part validator module

var router = express.Router();

var opt = {
  username: {
    $assertTrue: validator.isEmail  // 3rd-part validator function(value){...}
  },
  password: {
    $minLength: 6,
    $maxLength: 20
  }
};
router.post('/users', expressBodyCheckMiddleware(opt), handlerFunction);

module.exports = router;

Custom customDirectives and messageTemplate

var objectChecker = require('../object-checker');
var checker = objectChecker.createObjectChecker({
  customDirectives: {
    $doNotCheck: null,
    $inRange: function(value, option) { return option.min < value && value < option.max;}
  },
  messageTemplate: {
    invalid   : "Value of Field `{{fieldName}}` is not valid. Got `{{fieldValue}}`, but require {{checkerName}} = {{checkerOption}}",
    missing   : "Missing {{fieldName}}",
    unexpected: "Not support {{fieldName}}"
  }
};

checker.verify(yourObject);

Custom error message and error handler in middleware (javascript)

var objectChecker = require('../object-checker');
objectChecker.messageTemplate = {
  invalid   : "Value of Field `{{fieldName}}` is not valid. Got `{{fieldValue}}`, but require {{checkerName}} = {{checkerOption}}",
  missing   : "Missing {{fieldName}}",
  unexpected: "Not support {{fieldName}}"
};
var objectChecker = require('../object-checker');
objectChecker.expressErrorHandler = function(err, req, res, next) {
  console.log(err);
  var template = {
    "invalid"   : "invalid request",
    "missing"   : "missing parameter",
    "unexpected": "found unexpected parameter"
  };
  res.send({
    err: 400,
    msg: objectChecker.createErrorMessage(err, template);
  });
};

Option list

  • $type:
    • Assert the type of value.
  • $skip:
    • Do not check this field.
  • $:
    • Iterate all elements in array.
  • $isOptional: true
    • Can be undefined. (When defaultRequired === true)
  • $optional: true
    • Alias to $isOptional
  • $isRequired: true
    • Can be undefined. (When defaultRequired === true)
  • $required: true
    • Alias to $isRequired
  • $allowNull: true
    • Can be null.
  • $assertTrue: assertFunction
    • assertFunction(value) should return true.
  • $assertFalse: assertFunction
    • assertFunction(value) should return false.
  • $notEmptyString: true
    • Should not be ''.
  • $isInteger: ture
    • Should be an integer.
  • $isPositiveZeroInteger: Renamed to $isPositiveIntegerOrZero
  • $isPositiveIntegerOrZero: true
    • Should be an positive integer or 0.
  • $isPositiveInteger: ture
    • Should be an positive integer.
  • $isNegativeZeroInteger: Renamed to $isNegativeIntegerOrZero
  • $isNegativeIntegerOrZero: ture
    • Should be an negative integer or 0.
  • $isNegativeInteger: true
    • Should be an negative integer.
  • $minValue: option
    • Min value should be option.
  • $maxValue: option
    • Max value should be option.
  • $isValue: option
    • Should be option
  • $notValue: option
    • Should not be option
  • $in: option1, option2, ...
    • Value should be in the array.
  • $notIn: option1, option2, ...
    • Value should not be in the array.
  • $minLength
    • Min length of value should be option.
  • $maxLength
    • Max length of value should be option.
  • $isLength: option
    • Length of value should be option.
  • $isEmail: true
    • Should be email.
  • $matchRegExp: RegExp.
    • Should match RegExp.
  • $notMatchRegExp: RegExp.
    • Should not match RegExp.

Install:

npm install object-checker

Test:

cd node_modules/object-checker
npm test

License

MIT

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.4.10

6 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.25

7 years ago

0.3.24

8 years ago

0.3.23

8 years ago

0.3.22

8 years ago

0.3.21

8 years ago

0.3.20

8 years ago

0.3.19

8 years ago

0.3.18

8 years ago

0.3.17

8 years ago

0.3.15

8 years ago

0.3.14

8 years ago

0.3.13

8 years ago

0.3.12

9 years ago

0.3.11

9 years ago

0.3.10

9 years ago

0.3.9

9 years ago

0.3.8

9 years ago

0.3.7

9 years ago

0.3.6

9 years ago

0.3.5

9 years ago

0.3.4

9 years ago

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago