0.2.0 • Published 7 years ago

ezvalidate v0.2.0

Weekly downloads
-
License
Unlicense
Repository
-
Last release
7 years ago

Ez Validate

Validate data against a set of rules, defined through ez to use chainable functions.


Example:

const Ezvalidate = require("ezvalidate");
const validate = new Ezvalidate();

const data = {
    "username": "hello",
    "password": "world",
    "comparePassword": "world"
};

const rules = {
    "username": validate.string().max(20),
    "password": validate.string().min(4).compare(data.comparePassword),
    "age": validate.int().required(false).strict(false)
}

validate.check(rules, data); // returns undefined if validation passes, else array of errors

API

require("ezvalidate")(strictCheck=true)

When instatiating ezvalidate, you can pass a global boolean option. When this option is false, before the validation it will try to cast the inputs into the relative types, when possible. Example: parseInt("5"). You can override this option in the rules

.check(rules: Object, data: Object)

Description: validate the data against the set of rules

Params:
    rules {Object} = Rules are defined through chainable functions (see below)
    data {Object} = object to validate

Returns:
    undefined if the validation passes
    or an array of errors in the following format: [{msg, rule, field, value}]

Available validators

  • string() // alias: str()
  • integer() // alias: int()
  • number() // alias: num()
  • url()
  • email()
  • boolean() // alias: bool()
  • function()
  • date()
  • custom(function) // to define your custom type validator

Available chainable methods

  • optional() // when the field is not required (but validation will run if the value is provided)
  • strict(boolean=true) // when false, will cast the inputs to the relative type, before validating
  • min(value) // minlength for strings or min value for numbers
  • max(value) // maxlength for strings or max value for numbers
  • format(regex, errMsg) // for strings: test using the regex. The custom error msg is used when the test fails
  • trim() // for strings: trim a string before validating
  • lowerCase() // for strings: lowerize a string before validating
  • upperCase() // for strings: upperize a string before validating
  • compare(value) // strictly matches the provided value with the input
  • validator(function) // to use your custom validator in specific rules

Note: order which you call the chainable methods is not important.

Note 2: the `strict(false) rule does not apply to function() and custom() types


Usage with Express, Koa and similar

const Ezvalidate = require("ezvalidate");
const validate = new Ezvalidate(false);

app.use(validate.express());
// or
app.use(validate.koa());

Then, you will be able to ez call the function in your routes:

// Example route
app.use((req, res) => {
    ctx.checkQuery(is => {
        return {
            "username": is.string().optional()
        };
    });

    // Also available:
    // ctx.checkBody();
    // ctx.checkParams();
});

Tests

npm test

License

The Unlicense