New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

koa-request-schema

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-request-schema

Data validation using jsonschema for koa

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

koa-request-schema

NPM version build status David deps

node version io version

koa-request-schema implements request data validation using jsonschema. If data does not pass validation, the server returns a 400 Bad Request error. In non production environments, the response body is populated with the validation errors.

Usage

const schema = require('koa-request-schema');

router.post('/secret/:object',
	schema({
		params: {
			properties: {
				object: { type: 'string', required: true }
			}
		},
		query: {
			properties: {
				something: { type: 'string', required: false } }
			}
		},
		body: {
			properties: {
				password: { type: 'string', required: true, minLength: 10 }
			}
		}
	}),
	function *() {
		let body = this.request.body;

		if (body.password === 'the best password ever') {
			this.body = 'You got it boss';
		} else {
			this.throw(403, 'Pffttt...');
		}
	});

The error includes the following properties on schema validation error. The validationErrors property is the errors property returned by jsonschema on validation.

{
	"message": "Invalid request parameters",
	"details": {
		"validationErrors": [{
			"property": "request.body",
			"message": "Property password is required",
			"schema": { ... },
			"instance": ...
		}]
	}
}

Options

Options may be passed as the second argument to koa-request-schema; additionally require('koa-request-schema').create({ ... }) will return a function with options you pass it as defaults.

  • displayErrors [default=true in non-production environments]: Include validationErrors in the error.
  • coerceTypes [default=true]: Convert string values for date, integer, number, boolean, and object types to their respective type.
  • validator: Override the jsonschema Validator instance used.
  • strict [default=true]: Do not permit unknown properties in params, query, or body unless the schema defines its own additionalProperties value. (Default cannot be changed)

Koa 2 Support

To use koa-request-schema with koa@2, please use:

npm install --save koa-request-schema@next

Changelog

Keywords

koa

FAQs

Package last updated on 17 May 2016

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