UNPKG

3.52 kBMarkdownView Raw
1# bedrock-validation
2
3[![Build Status](http://ci.digitalbazaar.com/buildStatus/icon?job=bedrock-validation)](http://ci.digitalbazaar.com/job/bedrock-validation)
4
5A [bedrock][] module that provides a express middleware and an API for
6validating data structures and their contents. **bedrock-validation** uses
7schemas based on [JSON schema][].
8
9## Requirements
10
11- npm v3+
12
13## Quick Examples
14
15```
16npm install bedrock-validation
17```
18
19```js
20var bedrock = require('bedrock');
21var {validate} = require('bedrock-validation');
22
23// load schemas from '/foo'
24bedrock.config.validation.schema.paths.push('/foo');
25
26// add an express route with validation middleware
27bedrock.events.on('bedrock-express.configure.routes', function(app) {
28 app.post('/bar',
29 // validate the query using the 'postBarQueryValidator'
30 // validate the response body using the 'postBarValidator'
31 validate({query: 'postBarQueryValidator', body: 'postBarValidator'}),
32 function(req, res) {
33 // do something
34 });
35});
36
37bedrock.start();
38```
39
40## Configuration
41
42**bedrock-validation** will, on initialization (via the `bedrock.init` event),
43read any schemas found in the list of paths specified in
44`bedrock.config.validation.schema.paths`. Individual schemas can be skipped
45via the `bedrock.config.validation.schema.skip` configuration option. If any
46schema name matches a previously loaded schema, it will override that
47schema.
48
49For more documentation on configuration, see [config.js](./lib/config.js).
50
51## API
52
53### validate(name, [data], [callback])
54
55This method may be called with either one, two, or three parameters.
56
57If only one parameter is given:
58
59* The method returns express middleware that will be used to validate a request
60 using the schema associated with the given name.
61* If a string is provided for the first parameter, then it will be used as the
62 schema name for validating the request body.
63* If an object is provided for the first parameter, then the object can contain
64 `body` and `query` schema names as properties of the object.
65
66If two parameters are given:
67
68* The first parameter must be a string and the second parameter must be the
69 data to validate. The return value will contain the result of the validation.
70
71If three parameters are given:
72
73* The first parameter must be a string, the second parameter must be the data
74 to validate and the third must be a callback function to be called once the
75 validation operation completes. If an error occurs (including a validation
76 error), it will be passed as the first parameter of the callback, otherwise
77 validation has passed. The return value will contain the result of the
78 validation.
79
80### getSchema(name)
81
82Retrieves a validation schema given a `name` for the schema. If no such
83schema exists, `null` is returned.
84
85### validateInstance(instance, schema, [callback])
86
87Validates an `instance` (data) against a `schema`. This method may be used
88to validate data using a schema that wasn't necessarily registered via
89the configuration system. The `schema` must be a [JSON schema][] instance. The
90return value will contain the result of the validation. If a `callback` is
91given, it will be called once the validation operation completes. If an
92error occurred it will be passed in the second parameter of the `callback`.
93The synchronous and callback return value are the same:
94```js
95{
96 valid: <boolean>,
97 error: <error> // only present when valid === false
98}
99```
100
101
102[bedrock]: https://github.com/digitalbazaar/bedrock
103[JSON schema]: http://json-schema.org/