Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bedrock/validation

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bedrock/validation

Bedrock validation

  • 7.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
182
decreased by-22.88%
Maintainers
0
Weekly downloads
 
Created
Source

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 v6+

Quick Examples

npm install @bedrock/validation
import * as bedrock from '@bedrock/core';
import {postBarValidator, postBarQueryValidator} from '../schemas/my-schemas.js';
import {createValidateMiddleware as validate} from '@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({querySchema: postBarQueryValidator, bodySchema: 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

createValidateMiddleware({querySchema: schema, bodySchema: schema})

This method may be called with either query or body defined.

The method returns express middleware that will be used to validate a request using the schema associated with either query or body.

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> // only present when valid === false
}

Keywords

FAQs

Package last updated on 14 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc