Comparing version 2.0.0 to 2.1.0
# Changelog | ||
## [v2.1.0](https://github.com/ruiquelhas/supervizor/tree/v2.1.0) (2017-11-11) | ||
[Full Changelog](https://github.com/ruiquelhas/supervizor/compare/v2.0.0...v2.1.0) | ||
**Implemented enhancements:** | ||
- Support async validators [\#11](https://github.com/ruiquelhas/supervizor/issues/11) | ||
- Add support for asynchronous validators [\#12](https://github.com/ruiquelhas/supervizor/pull/12) ([ruiquelhas](https://github.com/ruiquelhas)) | ||
## [v2.0.0](https://github.com/ruiquelhas/supervizor/tree/v2.0.0) (2017-11-07) | ||
@@ -4,0 +12,0 @@ [Full Changelog](https://github.com/ruiquelhas/supervizor/compare/v1.0.6...v2.0.0) |
@@ -9,3 +9,3 @@ 'use strict'; | ||
return function (request, h) { | ||
return async function (request, h) { | ||
@@ -20,3 +20,3 @@ if (request.method !== 'post') { | ||
try { | ||
const payload = options.validator(request.payload, opts); | ||
const payload = await options.validator(request.payload, opts); | ||
@@ -23,0 +23,0 @@ if (payload) { |
{ | ||
"name": "supervizor", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Server-level request payload validation for hapi", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -10,3 +10,4 @@ # supervizor | ||
- [Usage](#usage) | ||
- [Example](#example) | ||
- [Synchronous validation](#synchronous-validation) | ||
- [Asynchronous validation](#asynchronous-validation) | ||
@@ -26,3 +27,3 @@ ## Installation | ||
### Example | ||
### Synchronous validation | ||
@@ -64,2 +65,42 @@ ```js | ||
### Asynchronous validation | ||
```js | ||
const Hapi = require('hapi'); | ||
const Supervizor = require('supervizor'); | ||
const plugin = { | ||
plugin: Supervizor, | ||
options: { | ||
validator: async (payload, options) => { | ||
// In this example, an asychronous validation function is called. | ||
try { | ||
await validate(payload, options); | ||
// Be nice to yourself and allow further validation. | ||
return payload; | ||
} | ||
catch (err) { | ||
// Be nice to everyone and provide details about the issue. | ||
// protip: https://github.com/hapijs/joi/blob/v13.0.1/API.md#errors | ||
const error = new Error('invalid payload'); | ||
error.details = [{ path: ['valid'] }]; | ||
throw error; | ||
} | ||
} | ||
} | ||
}; | ||
try { | ||
const server = new Hapi.Server(); | ||
await server.register(plugin); | ||
await server.start(); | ||
} | ||
catch (err) { | ||
throw err; | ||
} | ||
``` | ||
[coveralls-img]: https://img.shields.io/coveralls/ruiquelhas/supervizor.svg?style=flat-square | ||
@@ -66,0 +107,0 @@ [coveralls-url]: https://coveralls.io/github/ruiquelhas/supervizor |
10656
113