# response-validator

> 基于koa2的响应内容格式检查中间件，统一接口返回的格式

Latest version **1.2.0** (published 2021-12-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install response-validator
pnpm add response-validator
yarn add response-validator
bun add response-validator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2021-12-06 |
| First published | 2021-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | littledust |
| Maintainers | littledust |
| Keywords | koa2, middleware, validator |

## Links

- npm: https://www.npmjs.com/package/response-validator
- Repository: https://github.com/tinysimple/response-validator
- Homepage: https://github.com/tinysimple/response-validator#readme
- Issues: https://github.com/tinysimple/response-validator/issues
- npm.io page: https://npm.io/package/response-validator

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.2.0 (latest) — 2021-12-06
- 1.1.0 — 2021-11-16
- 1.0.0 — 2021-11-15

## README

# response-validator
> 基于koa2的响应格式检查中间件，统一检查接口返回的格式，并将通过Class/Function实例化的对象转为json字符串。

## 安装

使用NPM
``` bash
  npm i -S response-validator
```
使用YARN
``` bash
  yarn add response-validator
```

## 使用方式：
CommonJS模块引入
``` bash
  const validator = require('response-validator');
  // 使用验证器
  const app = new Koa();
  app.use(validator(ReturnResult));
```
其中<code>ReturnResult</code>表示接口返回应该要遵循的格式类/函数；如果返回的内容不是该类/函数的实例对象（通过<code>instanceof</code>判断），那么就会验证失败，抛出错误并返回。

<code>ReturnResult</code>的定义可以是：
``` bash
  class ReturnResult {
    constructor(code, message, data) {
      this.code = code;
      this.message = message;
      this.data = data;
    }
  }
```
也可以是：
``` bash
  function ReturnResult(code, message, data) {
    this.code = code;
    this.message = message;
    this.data = data;
  }
```

### 校验
当返回的数据是通过 new ReturnResult(code, message, data)实例化出来的对象，那么就会将该对象通经过JSON.stringify()方法转换为JSON格式的字符串并返回给调用方。

#### 取消校验
<code>closeJSONValidate()</code>
当某个接口需要取消校验时（如文件下载等），需要跳过对返回数据格式的校验，可以使用如下方法：
（<code>closeJSONValidate</code>方法挂载在当前请求的上下文中）
``` bash
ctx.closeJSONValidate()
```

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