Package Exports
- params-verifier
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (params-verifier) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
- params-verifier is a validator which can be used in controller of server or any other place.
install
npm install params-verifier --saveimport
- this package is developed by es6 syntax, so we recommend to load the package as follows:
import Validator from 'params-verifier';or you can also load like this:
const Validator = require('params-verifier').default;Demo
const query = {
page: 0,
show_confirm_entry: 'true',
realname: '测试',
mobile: '13567441576',
id_card: '130181199909091111',
mail: 'wang.cc@163.com',
employment_type: '3',
hukou_type: '6',
join_date: '2017-12-21T00:55:56+08:00',
join_date_start: new Date(),
join_date_end: '2017-12-21T00:55:56+08:00'
};
try {
const validator = new Validator(query, 'object', { stringNotEmpty: true });
validator
.field('page', 'number', { required: true })
.field('show_confirm_entry', 'boolean')
.field('realname', 'string', {
validator: value => /^[\u4E00-\u9FA5]*$/.test(value),
validatorErrMsg: '姓名必须为中文'
})
.field('mobile', 'string', {
type: 'mobile',
typeErrMsg: '手机号格式错误'
})
.field('id_card', 'string', {
type: 'id_card'
})
.field('mail', 'string', {
type: 'email'
})
.field('nickname', 'string')
.field('employment_type', 'number', {
type: 'enum',
range: [0, 4]
})
.field('hukou_type', 'number')
.field('join_date', 'date')
.field('join_date_start', 'date')
.field('join_date_end', 'string', {
type: 'date',
typeErrMsg: '不是有效的时间字符串'
});
const filteredFields = validator.filter();
console.log(filteredFields);
} catch (e) {
console.log('error: ', e);
}test
- you can test this node package in es6 development environment
