<commit_before># houdin
Route-level file type validation for [hapi](https://github.com/hapijs/hapi) parsed in-memory `multipart/form-data` request payloads. Also works as a standalone module.

[![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
- [Installation](#installation)
- [Usage](#usage)
  - [`validate(payload, options, fn)`](#validatepayload-options-fn)
    - [Hapi](#hapi)
    - [Standalone](#standalone)
- [Supported File Types](#supported-file-types)

## Installation
Install via [NPM](https://www.npmjs.org).

```sh
$ npm install houdin
```

## Usage
### `validate(payload, options, fn)`
Validates all `Buffer` values in a `payload` given a `whitelist` of file types provided in the `options`. Results in a [joi](https://github.com/hapijs/joi)-like `ValidationError` if some file type is not allowed or unknown otherwise it returns the original parsed payload to account for additional custom validation.

#### Hapi

```js
const Hapi = require('hapi');
const Houdin = require('houdin');

const server = new Hapi.Server();

server.connection({
    routes: {
        validate: {
            options: {
                whitelist: ['image/png']
            }
        }
    }
});

server.route({
    config: {
        validate: {
            payload: Houdin.validate
        },
        payload: {
            output: 'data',
            parse: true
        }
    }
});
```

### Standalone

```js
const Houdin = require('houdin');

const options = { whitelist: ['image/png'] };
const png = Buffer.from('89504e47', 'hex');

Houdin.validate({ file: png }, options, (err, value) => {

    console.log(err); // null
    console.log(value); // { file: <Buffer 89 50 4e 47> }
});
```

```js
const Houdin = require('houdin');

const options = { whitelist: ['image/png'] };
const gif = Buffer.from('47494638', 'hex');

Houdin.validate({ file: gif }, options, (err, value) => {

    console.log(err); // [ValidationError: child "file" fails because ["file" type is not allowed]]
    console.log(value); // undefined
});
```

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

[coveralls-img]: https://img.shields.io/coveralls/ruiquelhas/houdin.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/ruiquelhas/houdin
[david-img]: https://img.shields.io/david/ruiquelhas/houdin.svg?style=flat-square
[david-url]: https://david-dm.org/ruiquelhas/houdin
[david-dev-img]: https://img.shields.io/david/dev/ruiquelhas/houdin.svg?style=flat-square
[david-dev-url]: https://david-dm.org/ruiquelhas/houdin?type=dev
[version-img]: https://img.shields.io/npm/v/houdin.svg?style=flat-square
[version-url]: https://www.npmjs.com/package/houdin
[travis-img]: https://img.shields.io/travis/ruiquelhas/houdin.svg?style=flat-square
[travis-url]: https://travis-ci.org/ruiquelhas/houdin
<commit_msg>Update documentation to match requirements<commit_after># houdin
Route-level file type validation for [hapi](https://github.com/hapijs/hapi) parsed in-memory `multipart/form-data` request payloads. Also works as a standalone module.

[![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
- [Installation](#installation)
- [Usage](#usage)
  - [`validate(payload, options, fn)`](#validatepayload-options-fn)
    - [Hapi](#hapi)
    - [Standalone](#standalone)
- [Supported File Types](#supported-file-types)

## Installation
Install via [NPM](https://www.npmjs.org).

```sh
$ npm install houdin
```

## Usage
### `validate(payload, options, fn)`
Validates all `Buffer` values in a `payload` given a `whitelist` of file types provided in the `options`. Results in a [joi](https://github.com/hapijs/joi)-like `ValidationError` if some file type is not allowed or unknown otherwise it returns the original parsed payload to account for additional custom validation.

#### Hapi

```js
const Hapi = require('hapi');
const Houdin = require('houdin');

const server = new Hapi.Server();

server.connection({
    routes: {
        validate: {
            options: {
                whitelist: ['image/png']
            }
        }
    }
});

server.route({
    config: {
        validate: {
            payload: Houdin.validate
        },
        payload: {
            output: 'data',
            parse: true
        }
    }
});
```

### Standalone

```js
const Houdin = require('houdin');

const options = { whitelist: ['image/png'] };
const png = Buffer.from('89504e470d0a1a0a', 'hex');

Houdin.validate({ file: png }, options, (err, value) => {

    console.log(err); // null
    console.log(value); // { file: <Buffer 89 50 4e 47 0d 0a 1a 0a> }
});
```

```js
const Houdin = require('houdin');

const options = { whitelist: ['image/png'] };
const gif = Buffer.from('474946383761', 'hex');

Houdin.validate({ file: gif }, options, (err, value) => {

    console.log(err); // [ValidationError: child "file" fails because ["file" type is not allowed]]
    console.log(value); // undefined
});
```

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

[coveralls-img]: https://img.shields.io/coveralls/ruiquelhas/houdin.svg?style=flat-square
[coveralls-url]: https://coveralls.io/github/ruiquelhas/houdin
[david-img]: https://img.shields.io/david/ruiquelhas/houdin.svg?style=flat-square
[david-url]: https://david-dm.org/ruiquelhas/houdin
[david-dev-img]: https://img.shields.io/david/dev/ruiquelhas/houdin.svg?style=flat-square
[david-dev-url]: https://david-dm.org/ruiquelhas/houdin?type=dev
[version-img]: https://img.shields.io/npm/v/houdin.svg?style=flat-square
[version-url]: https://www.npmjs.com/package/houdin
[travis-img]: https://img.shields.io/travis/ruiquelhas/houdin.svg?style=flat-square
[travis-url]: https://travis-ci.org/ruiquelhas/houdin

{'fraction_non_alphanumeric': 0.177662506078781, 'fraction_numerical': 0.009563948776138758, 'mean_word_length': 3.8929421094369547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 28, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '546537', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2134, 'n_words': 430}