JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 277
  • Score
    100M100P100Q106317F
  • License MIT

Validations without ties to any framework. Inspired by validator, express-validator and validictorian.

Package Exports

  • validr

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (validr) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Validr

Build Status

Framework agnostic Node.js validations. Inspired by validator, express-validator and validictorian.

Installation

npm install validr

Usage

If you have ever used express-validator you should feel right at home.

Here is an example using Express. Can be used similarly in any other framework.

var express = require('express'),
  Validr = require('validr'),
  trimBody = require('trim-body');

app = express();

app.use(express.bodyParser());
app.use(app.router);

app.post('/user', function (req, res){
  trimBody(req.body);

  // Expected 'req.body' object format
  // {
  //   name: {
  //     first: <first name>,
  //     last: <last name>
  //   },
  //   email: <email>,
  //   age: <age>,
  //   sex: <sex>,
  //   occupation: <occupation>
  // }


  // 1. Create an instance of Validr.

  var validr = new Validr(req.body);


  // 2. Validations

  validr
    // use string with dot-notation to validate nested fields
    .validate('name.first', 'First Name is required.')
    .isLength(1);

  validr
    // you can also use an array to validate nested fields
    .validate(['name', 'last'], 'Last Name is required.')
    .isLength(1);

  validr
    // an object can be used to set separate validation messages for validators.
    .validate('email', {
      isLength: 'Email is required.',
      isEmail: 'Email must be valid.'
    })
    // validators are chainable
    .isLength(1).isEmail(); 

  validr
    .validate('age', 'Age must be a number.')
    .isNumeric();

  validr
    .validate('sex', 'Sex must be M (male) or F (female).')
    .isIn(['M', 'F']).isLength(1);


  // 3. Check for errors.

  var errors = validr.validationErrors();

  if (errors) return res.json(errors);



  // ...
  // Process req.body however you want. Example: save to db.
});


app.listen(3000);

Validate

Validating fields is similar to express-validator's assert.

Differences between validate and assert.

  • No notEmpty and len methods. Use isLength.
  • Nested fields are targeted with a dot-notation string or array. Example: 'name.first' or ['name', 'first'].

Validation errors

You can get errors in two ways. Similar to express-validator.

var errors = validr.validationErrors();
var mappedErrors = validr.validationErrors(true);

errors:

[
  {param: "email", msg: "Email is required.", value: "<received input>"},
  {param: "email", msg: "Email must be valid.", value: "<received input>"},
  {param: "age", msg: "Age must be a number.", value: "<received input>"}
]

mappedErrors:

{
  email: {
    param: "email",
    msg: "Email must be valid.",
    value: "<received input>"
  },
  age: {
    param: "age",
    msg: "Age is required.",
    value: "<received input>"
  }
}

Tests

npm install -g mocha

Then,

npm test

License

MIT