koalesce-mw-input-validator

0.1.1 • Public • Published

Input Validation Middleware for Koalesce

This module is Koalesce middleware that handles schema validation for url parameters, query strings, and request bodies.

If that sentence doesn't mean anything to you, you can always start here: koalesce-starter. (A working starter app built using angular, koa, node, and mongo.)

Route Fields

The input validation middleware checks for the schema field on a route. The schema object itself is a Joi schema. Refer to the Joi documentation for type specification. A 400 Bad Request is returned if the validation fails.

URL Parameter Example:

routes: {
    example: {
        url: '/:firstParameter/:secondParameter',
        method: 'GET',
        responseContentType: 'json',		// koalesce-mw-response-types
        schema: {
            params: {
                firstParameter: Joi.string(),
                secondParameter: Joi.number()
            }
        },
        handler: function* () {
            console.log('firstParameter', this.params.firstParameter);
            this.status = 200;
            this.body = 'success';
        }
    }
}

Query String Example:

(Request url would contain parameters: /route?parameter1=123&parameter2=456)

routes: {
    example: {
        url: '/route',
        method: 'GET',
        schema: {
            body: {
                parameter1: Joi.number().required(),
                parameter2: Joi.number().required()
            }
        },
        handler: function* () {
            console.log('parameter1', this.request.body.parameter1);
            this.status = 200;
            this.body = 'success';
        }
    }
}

HTTP Request Body Example:

routes: {
    example: {
        url: '/route',
        method: 'POST',
        requestContentType: 'json', 		// koalesce-mw-request-types
        responseContentType: 'json',		// koalesce-mw-response-types
        schema: {
            body: {
                parameter1: Joi.string().max(12).required(),
                parameter2: Joi.number().required()
            }
        },
        handler: function* () {
            console.log('parameter1', this.request.body.parameter1);
            this.status = 200;
            this.body = 'success';
        }
    }
}

Joi options are supported for each type in paramsOptions, queryOptions, and bodyOptions. The Joi

Koalesce Middleware Metadata

There is no metadata for this module.

Configuration Options

There are no configuration options.

Readme

Keywords

none

Package Sidebar

Install

npm i koalesce-mw-input-validator

Weekly Downloads

1

Version

0.1.1

License

ISC

Last publish

Collaborators

  • madams5