object-checker-js
Notice: the package is updated to
v1.0.0
and APIs are changed:
API | before v1.0.0 |
after v1.0.0 |
---|---|---|
isValid() |
Check the object and throw Exceptions when invalid | Renamed to verify() |
checkObject() |
Check the object and return check result. | Renamed to check() |
isValidObject() |
Check the object and return true /false for result |
Renamed to isValid() |
bodyCheckMiddleware |
Middleware for express | Renamed to expressBodyCheckMiddleware |
errorHandler |
Handler for middleware | Renamed 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 = ; var checker = objectCheckervar 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;
Use as an Express Middleware
// router.js var express = ;var expressBodyCheckMiddleware = expressBodyCheckMiddleware; var router = express; var opt = username: $isEmail: true password: $minLength: 6 $maxLength: 20 ;router; moduleexports = router;
Play with other modules
// router.js var express = ;var expressBodyCheckMiddleware = expressBodyCheckMiddleware;var validator = ; // 3rd-part validator module var router = express; var opt = username: $assertTrue: validatorisEmail // 3rd-part validator function(value){...} password: $minLength: 6 $maxLength: 20 ;router; moduleexports = router;
Custom customDirectives and messageTemplate
var objectChecker = ;var checker = objectChecker
Custom error message and error handler in middleware (javascript)
var objectChecker = ;objectCheckermessageTemplate = invalid : "Value of Field `{{fieldName}}` is not valid. Got `{{fieldValue}}`, but require {{checkerName}} = {{checkerOption}}" missing : "Missing {{fieldName}}" unexpected: "Not support {{fieldName}}";
var objectChecker = ;objectChecker { console; var template = "invalid" : "invalid request" "missing" : "missing parameter" "unexpected": "found unexpected parameter" ; res;};
Option list
- $type:
- Assert the type of value.
- $skip:
- Do not check this field.
- $:
- Iterate all elements in array.
- $isOptional: true
- Can be
undefined
. (WhendefaultRequired
=== true)
- Can be
- $optional: true
- Alias to
$isOptional
- Alias to
- $isRequired: true
- Can be
undefined
. (WhendefaultRequired
=== true)
- Can be
- $required: true
- Alias to
$isRequired
- Alias to
- $allowNull: true
- Can be
null
.
- Can be
- $assertTrue:
assertFunction
assertFunction(value)
should returntrue
.
- $assertFalse:
assertFunction
assertFunction(value)
should returnfalse
.
- $notEmptyString: true
- Should not be
''
.
- Should not be
- $isInteger: ture
- Should be an integer.
- $isPositiveZeroInteger: Renamed to
$isPositiveIntegerOrZero
- $isPositiveIntegerOrZero: true
- Should be an positive integer or
0
.
- Should be an positive integer or
- $isPositiveInteger: ture
- Should be an positive integer.
- $isNegativeZeroInteger: Renamed to
$isNegativeIntegerOrZero
- $isNegativeIntegerOrZero: ture
- Should be an negative integer or
0
.
- Should be an negative integer or
- $isNegativeInteger: true
- Should be an negative integer.
- $minValue:
option
- Min value should be
option
.
- Min value should be
- $maxValue:
option
- Max value should be
option
.
- Max value should be
- $isValue:
option
- Should be
option
- Should be
- $notValue:
option
- Should not be
option
- Should not be
- $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
.
- Min length of value should be
- $maxLength
- Max length of value should be
option
.
- Max length of value should be
- $isLength:
option
- Length of value should be
option
.
- Length of value should be
- $isEmail: true
- Should be email.
- $matchRegExp:
RegExp
.- Should match
RegExp
.
- Should match
- $notMatchRegExp:
RegExp
.- Should not match
RegExp
.
- Should not match
Install:
npm install object-checker
Test:
cd node_modules/object-checkernpm test