npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

params-verifier

v1.0.23

Published

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

Downloads

9

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');

Demo

    const query = {
        page: 0,
        employment_type: '3',
        show_confirm_entry: 'true',
        realname: '测试',
        mobile: '13567441576',
        join_date_end: '2017-12-21T00:55:56+08:00',
        join_date: '2017-12-21T00:55:56+08:00',
        join_date_start: new Date()
    };


    try {
        const validator = new Validator(query, 'object', { stringNotEmpty: true });
        validator
            .field('page', 'number', { required: true })
            .field('employment_type', 'number', {
                type: 'enum',
                range: [0, 4]
            })
            .field('show_confirm_entry', 'boolean')
            .field('realname', 'string', {
                validator: value => /^[\u4E00-\u9FA5]*$/.test(value),
                validatorErrMsg: '姓名必须为中文'
            })
            .field('mobile', 'string', {
                type: 'mobile',
                typeErrMsg: '手机号格式错误'
            })
            .field('join_date_end', 'string', {
                type: 'date',
                typeErrMsg: '不是有效的时间字符串'
            })
            .field('join_date', 'date')
            .field('join_date_start', 'date');

        const filteredFields = validator.filter();
        console.log(filteredFields);
    } catch (e) {
        console.log('error: ', e);
    }
    try {
        const query = '张三';
        const validator = new Validator(query, 'string');
        validator.singleField('realname', {
            validator: value => /^[\u4E00-\u9FA5]*$/.test(value),
            validatorErrMsg: '姓名必须为中文'
        });
        const result = validator.filteredSingleField();
        console.log('result: ', result);
    } catch (e) {
        console.log(e);
    }
    const query = {
        page: 0,
        employment_type: '3',
        hukou_type: '6'
    };

    try {
        const validator = new Validator(query, 'object', { required: true });
        validator
            .singleField('query', {
                validator: value => value.page >= 0,
                validatorErrMsg: '页码大于等于0'
            });
        const result = validator.filteredSingleField();
        console.log('result: ', result);
    } catch (e) {
        console.log(e);
    }

introduction

About the data validated by the validator, four basic types are be supported which include string, number, boolean and date. Besides, one complex type is supported, namely, the object type. At the meantime, the object type can also be acted as a basic type.

create a validator

    const validator = new Validator(query, 'object', { required: true });
  • new Validator(data, dataType[, options])
  • params:
    • data: source data
    • dataType: 'string' | 'number' | 'boolean' | 'date' | 'object', if the field is omited, the default value is 'object'.
    • options: an object, two fields can be set which are as follows.
      • options.stringNotEmpty:boolean -- the field value can't be an empty string when the data type is string.
      • options.required:boolean -- all fields are needed when the options.required is true. When the options.required is false, the field that equals to undefined will be filtered by the filteredSingleField or filter method.

start to validate data

for complex type
    validator.field('mobile', 'string', {
        type: 'mobile',
        typeErrMsg: '手机号格式错误'
    })
  • we can use field method to load the field name, field type and options which is optional.
    • field(fieldName, fieldType[, options])
      • fieldName:string --
      • fieldType:string -- 'string' | 'number' | 'boolean' | 'date' | 'object'
      • options: an object
        • options.type:string -- the business type, only string and number is supported.

          | data type | business type | | --------- | ------------- | | string | 'mobile', 'id_card', 'email', 'date' | | number | 'enum' |

        • options.typeErrMsg:string -- set the customed error message about business type.

        • options.range:array[start, end] -- only for the field of which the field type is number and the options.type is enum. The start is the minimum and the end is maximum.

        • options.validator:function -- a lambda expression, specify the validation rule.

        • options.validatorErrMsg:string -- set the customed error message about validation rule.

        • options.require:boolean -- when the value is true, the field is needed. The options.require of field method can override the options.required of validator.

        • options.stringNotEmpty:boolean -- only for the field of which the field type is string. When the value is true, the field value can't be an empty string.

        • the supported fields in options are different for different field type.

          | data type | options.field supported | | ------------- | ------------- | | string | type, typeErrMsg, validator, validatorErrMsg, required, stringNotEmpty | | number | type, typeErrMsg, validator, validatorErrMsg, required | | boolean | required | | date | validator, validatorErrMsg, required | | object | validator, validatorErrMsg, required |

for basic type
    validator.singleField('query', {
        validator: value => value.page >= 0,
        validatorErrMsg: '页码大于等于0'
    });
  • we can use singleField method to load the field name and options which is optional.
    • singleField(fieldName[, options])
      • fieldName:string --
      • options: an object, the detail is the same as the complex type.

filter the fields

  • for the complex type, the filter method will filter the fields registered with the field method.
  • for the basic type, the filteredSingleField method will filter the field registered with the singleField method.
  • if there is no error during the validation process, the fields whose options.required is true will be filtered. For the fields whose options.required is false or is not defined , if the field value is not undefined, this field will be filtered. If the field value is undefined, this field will be omited.

the principle of realization

sequence of validation rule

  • there are five steps in sequence about the validation which is as follows. And only some of steps are required for one field type.

    | validation rules | string | number | boolean | date | object | | --------------------- | ------ | ------ | ------- | ---- | ------ | | tryCastType | | ✔ | ✔ | ✔ | | | verifyType | ✔ | ✔ | ✔ | ✔ | ✔ | | checkStringNotEmpty | ✔ | | | | | | verifyBusinessType | ✔ | ✔ | | | | | runValidator | ✔ | ✔ | | ✔ | ✔ |

  • According to the table, if the verifyType validation fails, the other validation after it won't be executed.

the validation of tryCastType
  • in this step, if the real data type is not the same as the specified data type, then the validator will try to cast the data to the specified type when the situation is as follows.

    | data type | cast type example | | :----------- | :----------------- | | number | '6' -> 6 | | boolean | 'true' -> true 'false' -> false | | date | '2017-04-06' -> (new Date('2017-04-06')) |

the validation of verifyType
  • It's necessary for all types to check whether to match the specified type.
the validation of checkStringNotEmpty
  • for the string type, if options.stringNOtEmpty is true, the validator will check whether the value is not an empty string.
the validation of verifyBusinessType
  • this step is only for string and number. The options.type specifies a predefined validation rule. The data will be check with this validation rule.
the validation of runValidator
  • in this step, data will be check with a validation rule specified by options.validator.

why there are only four basic types ?

  • these types(number, boolean, string, date) are related to the data types of relational database.

contributing

  • Please contribute using Github Flow. Create a branch, add commits, and open a pull request.
  • the source code is under the directory of src, the unit test is under the directory of test.
  • after the contributing, you need run the unit test. And all unit tests must be passed.
development
  • this node package is developed with es6 syntax, and it's compiled by babel before run npm publish. The compiled files is under the directory of lib. And the directory of src is omited when publishing which is added to the .npmignore.
example
  • start demo
    node ./example/demo.js
test
  • start unit test
    npm test

License

  • ISC