# validate-provider

> 验证提供方

Latest version **1.0.33** (published 2019-03-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install validate-provider
pnpm add validate-provider
yarn add validate-provider
bun add validate-provider
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.33 |
| Published | 2019-03-19 |
| First published | 2019-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 52.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | XueYou |
| Maintainers | xueyou2000 |
| Keywords | validate |

## Links

- npm: https://www.npmjs.com/package/validate-provider
- Repository: https://github.com/xueyou2000/validate-provider
- Homepage: https://github.com/xueyou2000/validate-provider#readme
- Issues: https://github.com/xueyou2000/validate-provider/issues
- npm.io page: https://npm.io/package/validate-provider

## 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.0.33 (latest) — 2019-03-19
- 1.0.32 — 2019-03-19
- 1.0.31 — 2019-03-12

## README

# validate-provider

> 验证提供方, 作为管理验证函数的容器. 基于`Promise`

支持:

-   验证函数中返回布尔值, 则代表验证状态
-   验证函数中抛出异常, 则验证失败, 并将异常`error.message`作为验证失败消息
-   验证函数可以是`Promise`方法

## Install And Usega

Using Browser:

```html
<script src="./dist/index.js"></script>
<script>
    var provider = new ValidateProvider();

    provider.add(
        "IsPhone",
        (val: string) => {
            return /^(13[0-9]|15[0123456789]|17[03678]|18[0-9]|14[57])[0-9]{8}$/.test(val);
        },
        "手机号不正确"
    );

    provider
        .get("IsPhone")
        .method("xueyoucd@gmail.com")
        .then(() => {
            console.log("验证成功");
        })
        .catch((error) => {
            console.log("验证失败, 原因:", error.message);
        });
</script>
```

Or Using npm:

```sh
npm install --save validate-provider
```

```js
import { ValidateProvider } from "validate-provider";

// 实例化验证提供方
var provider = new ValidateProvider();
// 添加验证函数到容器
provider.add(
    "IsPhone",
    (val: string) => {
        return /^(13[0-9]|15[0123456789]|17[03678]|18[0-9]|14[57])[0-9]{8}$/.test(val);
    },
    "手机号不正确"
);
// 执行验证函数
provider
    .get("IsPhone")
    .method("xueyoucd@gmail.com")
    .then(() => {
        console.log("验证成功");
    })
    .catch((error) => {
        console.log("验证失败, 原因:", error.message);
    });
```

---

## 验证提供方使用

-   `provider.add(name: string, method: Function, errMsg?: string)` 添加验证函数
-   `provider.addByMethodRegister(methodRegister: IMethodRegister)` 添加验证函数

### 异步验证函数

```js
provider.add("async-valid", async (val: string) => {
    if (val === "123456") {
        return true;
    } else {
        throw new Error("异步验证失败");
    }
});
```

---

## 接口与类

### IMethodRegister

```typescript
/**
 * 验证函数注册信息
 */
export interface IMethodRegister {
    /**
     * 验证函数名称
     */
    name: string;
    /**
     * 验证函数
     * @description 返回promise则根据状态确认验证结果, 返回boolean则根据布尔确认结果, 返回字符串则总是失败结果, 并将字符串作为失败消息
     */
    method?: Function;
    /**
     * 是否动态验证
     */
    dynamic?: boolean;
    /**
     * 默认验证错误消息
     */
    errMsg?: string;
    /**
     * 验证错误消息格式化函数
     */
    format?: (value: any, params: any[], message: string) => string;
}
```

### ValidateMethodError

```typescript
const error: ValidateMethodError;
// 验证值
console.log(error.value);
// 验证参数
console.log(error.params);
// 验证错误消息
console.log(error.message);
// 验证函数名称
console.log(error.validName);
```

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