# graphql-validation

> An GraphQL middleware for validator.js.

Latest version **2.2.2** (published 2019-08-27) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install graphql-validation
pnpm add graphql-validation
yarn add graphql-validation
bun add graphql-validation
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.2.2 |
| Published | 2019-08-27 |
| First published | 2019-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12.1 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 22 |
| Author | TuiDev |
| Maintainers | thaihv |
| Keywords | graphql, validation, validator, validate, check |

## Links

- npm: https://www.npmjs.com/package/graphql-validation
- Repository: https://github.com/havinhthai/graphql-validation
- Homepage: https://github.com/havinhthai/graphql-validation#readme
- Issues: https://github.com/havinhthai/graphql-validation/issues
- npm.io page: https://npm.io/package/graphql-validation

## Dependencies (1)

- [validator](https://npm.io/package/validator.md) ^11.0.0

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 2.2.2 (latest) — 2019-08-27
- 2.2.1 — 2019-05-27
- 2.1.1 — 2019-04-24
- 2.1.0 — 2019-04-24
- 2.0.3 — 2019-04-08
- 2.0.2 — 2019-04-07
- 2.0.1 — 2019-04-07
- 1.1.2 — 2019-04-06
- 1.1.1 — 2019-04-06
- 1.1.0 — 2019-04-05
- 1.0.1 — 2019-04-05
- 1.0.0 — 2019-04-02

## README

<p align="center"><img src="https://s3-ap-southeast-1.amazonaws.com/cdn.tuidev.io/graphql-validation.png" width="150" /></p>

# graphql-validation
[![NPM version](https://img.shields.io/npm/v/graphql-validation.svg)](https://www.npmjs.com/package/graphql-validation)
[![Minified size](https://img.shields.io/bundlephobia/min/graphql-validation.svg)](https://img.shields.io/bundlephobia/min/graphql-validation.svg)
[![License: MIT](https://img.shields.io/npm/l/graphql-validation.svg)](https://opensource.org/licenses/MIT)
[![Dependency Status](https://david-dm.org/havinhthai/graphql-validation.svg)](https://david-dm.org/havinhthai/graphql-validation.svg)
[![TravisCI](https://travis-ci.org/havinhthai/graphql-validation.svg?branch=master)](https://travis-ci.org/havinhthai/graphql-validation.svg?branch=master)

`graphql-validation` is a GraphQL middleware that wraps [validator.js](https://github.com/chriso/validator.js) validator functions.

## Table of Contents
- [Features](#features)
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Contribution](#contribution)
- [License](#license)

## Features
- Based on validator.js
- Validate both args & input types
- Easy to use
- Easy to modularizing
- Pure javascript

## Install
```sh
yarn add graphql-validation
```
or
```sh
npm i --save graphql-validation
```
## Usage
### Basic 
```javascript
const { validator, validate } = require('graphql-validation'); // Import module

const resolver = {
  Mutation: {
    createPost: validator([ // <--- Validate start here
      validate('id').isMongoId(),
      validate('title') // <--- Validate title 
        .isLength({ msg: 'Title is invalid' options: { min: 3, max: 20 } })
        .contains({ msg: 'Title must contains "hi"', options: 'hi' })
        .not().isEmpty({ msg: 'Title is required' }),
      validate('content') // <--- Validate content
        .isLength({ options: { min: 10, max: 20 } }),
    ], (parent, args, context, info) => {
      if (context.validationErrors) {
        // Validate failed
        console.log(context.validationErrors); // Do anything with this errors
        
        return;
      }
    
      // Validate successfully, time to create new post
    }),
  },
};
```
```javascript
Input: { id: 'hellomongo', title: '', content: 'Hi!' };

// console.log(context.validationErrors);
Output: [
  {
    param: 'id',
    msg: 'MongoId is invalid',
  },
  {
    param: 'title',
    msg: 'Title is invalid',
  },
  {
    param: 'title',
    msg: 'Title must contains \"hi\"',
  },
  {
    param: 'title',
    msg: 'Title is required',
  },
  {
    param: 'content',
    msg: 'Invalid value',
  }
];
```

### Validate Input types
```javascript
const { validator, validate } = require('graphql-validation'); // Import module

const resolver = {
  Mutation: {
    createPost: validator([
      validate('title', 'data') // <--- Validate input types
        .not().isEmpty({ msg: 'Title is required' }), 
      validate('content') // <--- Just validate args
        .isLength({ options: { min: 10, max: 20 } }),
    ], (parent, args, context, info) => {
      if (context.validationErrors) {
        // Validate failed
        console.log(context.validationErrors); // Do anything with this errors
        
        return;
      }
    
      // Validate successfully, time to create new post
    }),
  },
};
```
```javascript
Input: { data: { title: '' }, content: 'Hi!' };

// console.log(context.validationErrors);
Output: [
  { param: 'title', msg: 'Title is required' },
  { param: 'content', msg: 'Invalid value' },
];
```

> To get started with `graphql-validation`, you can refer to this [example](example).


## API
#### `validator(rules: array, controller: function)`
| Args                         | Type                                                            | Default | Description                                                                                                                                                                                                                                              |
| --------------------------- | --------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `rules`                  | `Array` | `undefined`  |  List of validation's rules. **Required**.                                            |
| `controller`             | `Function`              | `undefined`       | Controller of mutation's field. **Required**. |
     
#### `validate(param: string, input: string)`
| Args                         | Type                                                            | Default | Description                                                                                                                                                                                                                                              |
| --------------------------- | --------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `param`                  | `String` | `undefined`  |  Name of param. **Required**.                                            |
| `input`                  | `String` | `undefined`  |  Name of input. Options.                                            |

#### Validator functions 
| Args                         | Type                                                            | Default | Description                                                                                                                                                                                                                                              |
| --------------------------- | --------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `config`                  | `Object { msg: string, options: any }` | `{ msg: 'Invalid value' }`  | `msg`: Custom error message, `options`: options of [validator functions](https://github.com/chriso/validator.js#validators).  

## Contribution
Contribution are always **welcome and recommended**! Here is how:

- Fork the repository ([here is the guide](https://help.github.com/articles/fork-a-repo/)).
- Clone to your machine `git clone https://github.com/YOUR_USERNAME/graphql-validation.git`
- Make your changes
- Create a pull request

## License
`graphql-validation` is released under the MIT license. See [LICENSE](./LICENSE) for details.  
  
Any question or support will welcome.

---
_Source: https://npm.io/package/graphql-validation · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
