bedrock-validation
Bedrock validation
Last updated 2 months ago by dlongley .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install bedrock-validation 
SYNC missed versions from official npm registry.

bedrock-validation

Build Status

A bedrock module that provides a express middleware and an API for validating data structures and their contents. bedrock-validation uses schemas based on JSON schema.

Requirements

  • npm v3+

Quick Examples

npm install bedrock-validation
var bedrock = require('bedrock');
var {validate} = require('bedrock-validation');

// load schemas from '/foo'
bedrock.config.validation.schema.paths.push('/foo');

// add an express route with validation middleware
bedrock.events.on('bedrock-express.configure.routes', function(app) {
  app.post('/bar',
    // validate the query using the 'postBarQueryValidator'
    // validate the response body using the 'postBarValidator'
    validate({query: 'postBarQueryValidator', body: 'postBarValidator'}),
    function(req, res) {
      // do something
    });
});

bedrock.start();

Configuration

bedrock-validation will, on initialization (via the bedrock.init event), read any schemas found in the list of paths specified in bedrock.config.validation.schema.paths. Individual schemas can be skipped via the bedrock.config.validation.schema.skip configuration option. If any schema name matches a previously loaded schema, it will override that schema.

For more documentation on configuration, see config.js.

API

validate(name, [data], [callback])

This method may be called with either one, two, or three parameters.

If only one parameter is given:

  • The method returns express middleware that will be used to validate a request using the schema associated with the given name.
  • If a string is provided for the first parameter, then it will be used as the schema name for validating the request body.
  • If an object is provided for the first parameter, then the object can contain body and query schema names as properties of the object.

If two parameters are given:

  • The first parameter must be a string and the second parameter must be the data to validate. The return value will contain the result of the validation.

If three parameters are given:

  • The first parameter must be a string, the second parameter must be the data to validate and the third must be a callback function to be called once the validation operation completes. If an error occurs (including a validation error), it will be passed as the first parameter of the callback, otherwise validation has passed. The return value will contain the result of the validation.

getSchema(name)

Retrieves a validation schema given a name for the schema. If no such schema exists, null is returned.

validateInstance(instance, schema, [callback])

Validates an instance (data) against a schema. This method may be used to validate data using a schema that wasn't necessarily registered via the configuration system. The schema must be a JSON schema instance. The return value will contain the result of the validation. If a callback is given, it will be called once the validation operation completes. If an error occurred it will be passed in the second parameter of the callback. The synchronous and callback return value are the same:

{
  valid: <boolean>,
  error: <error> // only present when valid === false
}

Current Tags

  • 5.2.0                                ...           latest (2 months ago)

31 Versions

  • 5.2.0                                ...           2 months ago
  • 5.1.0                                ...           3 months ago
  • 5.0.0                                ...           6 months ago
  • 4.5.0                                ...           10 months ago
  • 4.4.0                                ...           a year ago
  • 4.3.0                                ...           a year ago
  • 4.2.0                                ...           2 years ago
  • 4.1.0                                ...           3 years ago
  • 4.0.1                                ...           3 years ago
  • 4.0.0                                ...           3 years ago
  • 3.1.2                                ...           3 years ago
  • 3.1.1                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 2.4.1                                ...           3 years ago
  • 2.4.0                                ...           3 years ago
  • 3.0.0                                ...           4 years ago
  • 2.3.0                                ...           4 years ago
  • 2.2.1                                ...           5 years ago
  • 2.2.0                                ...           5 years ago
  • 2.1.1                                ...           5 years ago
  • 2.1.0                                ...           5 years ago
  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.0.3                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 0.1.0                                ...           7 years ago
  • 0.0.1                                ...           7 years ago

Copyright 2014 - 2021 © npmmirror.com |