validate-prop
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

中文文档

validate-prop

Verify that the data is correct

Install

Install with npm

  npm install --save validate-prop

Usage

import validate from 'validate-prop';
// or
// const Validate = require("validate-prop");
const config = {
    userName: {
        type: 'notEmpty',
        msg: 'The user name cannot be empty',
    },
    password: {
        type: 'notEmpty',
        msg: 'The password cannot be empty',
    },
    phone: {
        msg: "Incorrect phone number format",
        test: (value, key, rule) => {
            if (!value) {
                return "The cell phone number cannot be empty";
            }
            const isPhone = /^1[3456789]\d{9}$/.test(value);
            if (isPhone) {
                return "";
            } else {
                return "Incorrect phone number format";
            }
        },
    },
    code: [{
        type: "notEmpty",
        msg: "The captcha cannot be empty",
    },{
        type: "length",
        value: [4, 6],
        msg: "The captcha length is between 4 and 6",
    }],
};

const model = {
    userName: 'lucas',
    password: 'password123456',
    phone: "13912345678",
    code: "1234",
}
 validate(config,model).then(({ msg, list }) => {
    if( msg ){
        // failed
        console.log(msg); // error msg
        return;
    }
});

Test

More examples to see the test

  npm test

Readme

Keywords

none

Package Sidebar

Install

npm i validate-prop

Weekly Downloads

0

Version

0.3.0

License

ISC

Unpacked Size

39.7 kB

Total Files

16

Last publish

Collaborators

  • huweicool