bedrock-validation
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
Quick Examples
npm install @bedrock/validation
import * as bedrock from '@bedrock/core';
import {validate} from '@bedrock/validation';
bedrock.config.validation.schema.paths.push('/foo');
bedrock.events.on('bedrock-express.configure.routes', function(app) {
app.post('/bar',
validate({query: 'postBarQueryValidator', body: 'postBarValidator'}),
function(req, res) {
});
});
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])
This method may be called with either one or two 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.
getSchema(name)
Retrieves a validation schema given a name
for the schema. If no such
schema exists, null
is returned.
validateInstance(instance, schema)
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. This function returns
a promise that resolves to:
{
valid: <boolean>,
error: <error>
}