# @dxp-dc/validator

> data validator

Latest version **0.0.1** (published 2021-03-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dxp-dc/validator
pnpm add @dxp-dc/validator
yarn add @dxp-dc/validator
bun add @dxp-dc/validator
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2021-03-15 |
| First published | 2021-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jackie19 |
| Maintainers | battercake, drpackstore, xiongyouhui, barretem, dmcomponents, jackie198512 |

## Links

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

## Recent versions

- 0.0.1 (latest) — 2021-03-15

## README

# 表单验证

> 使用装饰器语法创建规则及校验
> 支持部分数据校验， valid 方法

### Usage

```javascript
import Validator from '@dxp-dc/validator'

// 实例一个校验器
const validator = new Validator('test')

// 创建校验规则
class FeedbackData {
  @validator.rules([
    {
      required: true,
      type: 'string',
      message: '反馈内容不能为空',
    },
    {
      message: '反馈内容不能超过10个字符',
      validator(val) {
        return val.length <= 10
      },
    },
  ])
  feedback = ''
}

// 实例化数据
const myData = new FeedbackData()

// 创建一个类去更新数据并且进行切片验证
class Demo extends Component {
  @validator.valid(myData, msg =>
    Taro.showToast({
      title: msg,
      icon: 'none',
    }),
  )
  handleSubmit() {
    Taro.showToast({
      title: '已保存',
      icon: 'success',
    })
  }
}
```

### Validator(scope: string)
初始化校验器

### rules(rules: Rules)
给字段添加验证规则
#### required
字段必填

#### type
使用 typeof value !== type 进行校验

#### validator
自定义的校验函数

#### message
验证失败返回的提示信息


### Valid(data, callback)

#### data
需要校验的数据，支持对象和函数

#### callback

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