validate-prop

Verify that the data is correct

Usage no npm install needed!

<script type="module">
  import validateProp from 'https://cdn.skypack.dev/validate-prop';
</script>

README

中文文档

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