api-blueprint-json-schema

Validates payloads against a specified JSON Schema within your API Blueprint.

WARNING: This module has been renamed to bluth. Please install it instead. See https://github.com/ribot/bluth for more information.
Last updated 5 years ago by stefanpearson .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install api-blueprint-json-schema 
SYNC missed versions from official npm registry.

API Blueprint JSON Schema

Validates payloads against a specified JSON Schema within your API Blueprint.

  • Validate request bodies in your route handlers or middleware
  • Validate response bodies within tests

Installation

npm install api-blueprint-json-schema --save

Usage

// Dependencies
var BlueprintSchema = require( 'api-blueprint-json-schema' );

var blueprintSchema;

// Create instance of BlueprintSchema from Blueprint JSON
blueprintSchema = new BlueprintSchema( myBlueprintJson , {
  defaultErrorSchema: errorJsonSchema    // Response schema for 400+ status' (optional)
} );

// Create instance of BlueprintSchema from Blueprint markdown (parsing is async)
BlueprintSchema.create( '# My Blueprint', {
  defaultErrorSchema: errorJsonSchema
}, function ( error, blueprintSchemaInstance ) {
  blueprintSchema = blueprintSchemaInstance;
} );

// Access the blueprint JSON, for whatever reason
var blueprint = blueprintSchema.blueprint;

// Optionally set a response error schema later
blueprintSchema.defaultErrorSchema = errorJsonSchema;

router.get( '/my/resource/:resourceId', function( request, response, next ) {

  // Validate payload
  blueprintSchema.validate( request.body, {

    type: 'request',              // eg. 'request' or 'response'
    route: request.route.path,    // eg. '/my/resource/:resourceId'
    method: request.method,       // eg. 'GET'
    statusCode: '200'             // eg. '200' (only used if type = 'response')

  }, function( error, result ) {

    if ( error ) {
      console.log( error );
    }

    if ( result.errors ) {
      return response.status( 400 ).send( result );
    }

    return response.status( 200 ).send( {
      message: 'success!'
    } );

  } );

} );

// Get a schema, for whatever reason
var someSchema = blueprintSchema.get( {
  type: 'request',
  route: '/my/route',
  method: 'GET',
  statusCode: '200'
} );

Current Tags

  • 0.3.0                                ...           latest (5 years ago)

7 Versions

  • 0.3.0 [deprecated]           ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
  • 0.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (5)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |