js-validator-lite
TypeScript icon, indicating that this package has built-in type declarations

1.0.15 • Public • Published

js-validator-lite

Field validator, form validation

install

pnpm add js-validator-lite

yarn add js-validator-lite

npm i js-validator-lite
import FieldsValidator from 'js-validator-lite'
const rules = {
    name: [
        { 
            type: 'string', 
            message: 'Name must be a string'
        },
    ],
    age: [{ 
        type: 'number', 
        message: 'Age must be a number',
        validator (fieldName, value, callback){
            if (value > 100) {
                callback('The maximum cannot exceed 100')
                return
            }
            callback() // Verification passed
        }
    }],
    email: [{ type: 'email' }]
};
const validator = new FieldsValidator(rules);
// const rs = await validator.validate(data)
validator.validate(data).then(({ valid, fields })=>{ 
    // valid=boolean false Verification failed | true Verification passed, fields=
    console.log(rs, 'rs')
}).catch((err)=>{
    console.log(err, 'errerr2')
})
// Return results
{
    valid: true | false,
    fields:{
        // Fields that failed validation, For example:
        name: 'name is required'
    }
}

Package Sidebar

Install

npm i js-validator-lite

Weekly Downloads

4

Version

1.0.15

License

ISC

Unpacked Size

7.87 kB

Total Files

6

Last publish

Collaborators

  • devxtools