0.3.0 • Published 3 years ago

validate-prop v0.3.0

Weekly downloads
2
License
ISC
Repository
github
Last release
3 years ago

中文文档

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
0.3.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.6

3 years ago

0.1.4

3 years ago

0.1.5

3 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago