JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 54
  • Score
    100M100P100Q85463F
  • License MIT

params-verifier is a validator which can be used in controller of server or any other place.

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

NPM

NPM version

  • params-verifier is a validator which can be used in controller of server or any other place.

install

    npm install params-verifier --save

import

  • 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