node package manager

composable-validator

Composable Validator

  npm install composable-validator@latest

This is a convenience package around chriso's node-validator.

This package lets you create validation objects that you can nest, combine and re-use.

Simple "user" validation object:

 
    var userValidator = new schema({
      email: check().len(4, 64).isEmail(),
      password: check().len(4, 80).notIn(["password", "love", "12345"])
    });
 
    var results = userValidator.check({
      email:"ab@c",
      password: "love"
    });
 
    //true
    results.deepEqual({ email: [ 'Invalid email' ], password: [ 'Unexpected value' ] });
    

Nesting!

 
    registrationValidator = new schema({
      subdomain: check().is(/[a-zA-Z][a-zA-Z0-9]*/),
      user: userValidator /* note the re-use! */
    })
 
    var regResult = registrationValidator.check({
      subdomain: "w3",
      user: {
        email: "@gmail.com",
        password: "w00t!!!"
      }
    })
 
    //true
    regResult.deepEqual({ user: { email: [ 'Invalid email' ] } });