# @incpad/validator

> ```$xslt const {Validator, initValidatorWithFile} = require("@incpad/validator") initValidatorWithFile("./validator.schema.js"); ``` 导入的js文件应该像这样的： ```$xslt module.exports = { schemaList: { testSchema: { code: {

Latest version **0.0.27** (published 2019-02-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @incpad/validator
pnpm add @incpad/validator
yarn add @incpad/validator
bun add @incpad/validator
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.27 |
| Published | 2019-02-18 |
| First published | 2019-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 40 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | yixuankeer |

## Links

- npm: https://www.npmjs.com/package/@incpad/validator
- npm.io page: https://npm.io/package/@incpad/validator

## Dependencies (2)

- [@incpad/base-tool](https://npm.io/package/@incpad/base-tool.md) ^1.0.26
- [fastest-validator](https://npm.io/package/fastest-validator.md) ^0.6.12

## Recent versions

- 0.0.27 (latest) — 2019-02-18
- 0.0.26 — 2019-02-13

## README

### Usage

```$xslt
    const {Validator, initValidatorWithFile} = require("@incpad/validator")
    initValidatorWithFile("./validator.schema.js");
```
导入的js文件应该像这样的：
```$xslt
module.exports = {
    schemaList: {
        testSchema: {
            code: {
                type: 'number',
                min: 1000,
            },
            msg: 'string',
            name: 'string'
        },
    },
    customCheckerList: {
        existPath,
    },
    customMessages: {
        existPathError: 'path not exist',
    },
};
```
其中的schemaList、customCheckerList、customMessages是必须的，  
schemaList是标准的验证对象格式定义、customCheckerList是自定义验证函数列表、
customMessages是自定义报错信息的列表。

或者也可以使用如下方式初始化:
```$xslt
const {Validator,initValidatorWithObject} = require("@incpad/validator")
initValidatorWithObject(object);
```
这个object对象等同于之前文件export出的对象

然后就可以使用如下方式进行验证：
```$xslt
Validator("testSchema", targetObject)
```

同时，该库还导出了便捷函数checkFuncParam   
```$xslt
const {initValidatorWithObject, Validator, checkFuncParam} = require("../index");
const schema = {
    schemaList: {
        getGeoParamCheck: {
            cbSucc: "function",
            cbErr: "function"
        },
    },
    customCheckerList: {},
    customMessages: {},
};
initValidatorWithObject(schema);
function getGeo(cbSucc,cbErr){
    checkFuncParam("getGeoParamCheck", {cbSucc,cbErr});
}
```
如果校验失败，则会直接抛出参数格式不正确的错误


由于该库只是fastest-validator的再次封装，所以schema、返回值等等都和原库一致

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