Skip to content

Commit

Permalink
Merge 2018ffc into ebf84ee
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiquelhas committed Nov 7, 2017
2 parents ebf84ee + 2018ffc commit 105f27e
Show file tree
Hide file tree
Showing 6 changed files with 2,378 additions and 78 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -3,7 +3,8 @@ after_success: npm run coveralls
language: node_js

node_js:
- "4"
- "5"
- "8"
- "9"
- "node"

sudo: false
51 changes: 24 additions & 27 deletions README.md
@@ -1,7 +1,7 @@
# copperfield
Server-level [houdin](https://github.com/ruiquelhas/houdin) validation for [hapi](https://github.com/hapijs/hapi).

[![NPM Version][fury-img]][fury-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Dependencies][david-img]][david-url]
[![NPM Version][version-img]][version-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Dependencies][david-img]][david-url] [![Dev Dependencies][david-dev-img]][david-dev-url]

## Table of Contents

Expand All @@ -18,7 +18,6 @@ $ npm install copperfield
```

## Usage

Register the package as a server plugin to enable validation for each route that parses — `parse: true` — and reads the request payload into memory — `output: 'data'`. For every other route with a different configuration, the validation is skipped.

If the validation fails, a [joi](https://github.com/hapijs/joi)-like `400 Bad Request` error is returned alongside an additional `content-validation: failure` response header. If everything is ok, the response will ultimately contain a `content-validation: success` header.
Expand All @@ -29,23 +28,19 @@ If the validation fails, a [joi](https://github.com/hapijs/joi)-like `400 Bad Re
const Hapi = require('hapi');
const Copperfield = require('copperfield');

const server = new Hapi.Server();
server.connection({
// go nuts
});

const plugin = {
register: Copperfield,
options: {
// Allow png files only
whitelist: ['image/png']
}
};
try {
const server = new Hapi.Server();

server.register(plugin, (err) => {
await server.register({
plugin: Copperfield,
options: {
// Allow png files only
whitelist: ['image/png']
}
});

server.route({
config: {
options: {
payload: {
output: 'data',
parse: true
Expand All @@ -54,21 +49,23 @@ server.register(plugin, (err) => {
}
});

server.start(() => {
// go nuts
});
});
await server.start();
}
catch (err) {
throw err;
}
```

## Supported File Types
The same as [file-type](https://github.com/sindresorhus/file-type/tree/v7.0.0#supported-file-types).

The same as [file-type](https://github.com/sindresorhus/file-type#supported-file-types).

[coveralls-img]: https://coveralls.io/repos/ruiquelhas/copperfield/badge.svg
[coveralls-img]: https://img.shields.io/coveralls/ruiquelhas/copperfield.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/ruiquelhas/copperfield
[david-img]: https://david-dm.org/ruiquelhas/copperfield.svg
[david-img]: https://img.shields.io/david/ruiquelhas/copperfield.svg?style=flat-square
[david-url]: https://david-dm.org/ruiquelhas/copperfield
[fury-img]: https://badge.fury.io/js/copperfield.svg
[fury-url]: https://badge.fury.io/js/copperfield
[travis-img]: https://travis-ci.org/ruiquelhas/copperfield.svg
[david-dev-img]: https://img.shields.io/david/dev/ruiquelhas/copperfield.svg?style=flat-square
[david-dev-url]: https://david-dm.org/ruiquelhas/copperfield?type=dev
[version-img]: https://img.shields.io/npm/v/copperfield.svg?style=flat-square
[version-url]: https://www.npmjs.com/package/copperfield
[travis-img]: https://img.shields.io/travis/ruiquelhas/copperfield.svg?style=flat-square
[travis-url]: https://travis-ci.org/ruiquelhas/copperfield
17 changes: 9 additions & 8 deletions lib/index.js
Expand Up @@ -3,19 +3,20 @@
const Houdin = require('houdin');
const Supervizor = require('supervizor');

exports.register = function (server, options, next) {
const internals = {};

const plugin = {
register: Supervizor,
internals.register = async function (server, options) {

return await server.register({
plugin: Supervizor,
options: {
validator: Houdin.validate,
whitelist: options.whitelist
}
};

server.register(plugin, next);
});
};

exports.register.attributes = {
pkg: require('../package.json')
module.exports = {
pkg: require('../package.json'),
register: internals.register
};

0 comments on commit 105f27e

Please sign in to comment.