# b-validate

> Javascript type validate

Latest version **1.5.3** (published 2023-08-02) · MIT license · 0 weekly downloads

## Install

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

## 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.5.3 |
| Published | 2023-08-02 |
| First published | 2019-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 94.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 57 |
| Author | PengJiyuan |
| Maintainers | pjy, yinkaihui |
| Keywords | validate, type |

## Links

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

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.5.3 (latest) — 2023-08-02
- 1.5.0-beta.0 (beta) — 2023-05-23
- 1.5.2 — 2023-05-26
- 1.5.1 — 2023-05-26
- 1.5.0 — 2023-05-26
- 1.4.6 — 2023-04-13
- 1.4.5 — 2023-04-13
- 1.4.4 — 2022-12-20
- 1.4.3 — 2022-10-14
- 1.4.2 — 2022-09-15
- 1.4.1 — 2022-06-22
- 1.4.0 — 2022-04-15
- 1.4.0-beta.2 — 2022-04-15
- 1.4.0-beta.1 — 2022-04-14
- 1.4.0-beta.0 — 2022-04-14
- … 21 more at https://npm.io/package/b-validate/versions

## README

[![Travis](https://img.shields.io/travis/PengJiyuan/b-validate.svg)](https://travis-ci.org/PengJiyuan/b-validate)
[![npm](https://img.shields.io/npm/v/b-validate.svg)](https://www.npmjs.com/package/b-validate)
[![npm](https://img.shields.io/npm/l/b-validate.svg)](https://www.npmjs.com/package/b-validate)
[![codecov.io](https://codecov.io/github/PengJiyuan/b-validate/coverage.svg?branch=master)](https://codecov.io/github/PengJiyuan/b-validate?branch=master)

# b-validate

Javascript type validate.

## Highlight

- chainable api
- support custom validator
- support schema validate
- support async validate

## Usage

```bash
npm i b-validate
```

```js
import bv from 'b-validate';

bv(123)
  .number.min(2)
  .max(10)
  .collect((error) => {
    console.log(error); // { value: 123, type: 'number', message: '123 is not less than 10' }
    // if no error, error equal to null
  });

// or get error message like this:

const error = bv('b-validate').string.isRequired.match(/Validator/).end;
// { value: 'b-validate', type: 'string', message: '`b-validate` is not match pattern /Validator/' }
```

### Schema

```js
import { Schema } from 'b-validate';

const schema = new Schema({
  name: [
    {
      type: 'string',
      required: true,
      message: '必填字段',
    },
    {
      type: 'string',
      maxLength: 10,
      message: '最大长度是10',
    },
  ],
  age: [
    {
      type: 'number',
      min: 2,
      max: 5,
      message: '在2和5之间',
    },
  ],
  email: [
    {
      type: 'email',
      message: '邮箱格式不对',
    },
  ],
  ip: [
    {
      type: 'ip',
      message: 'ip格式不对',
    },
  ],
  url: [
    {
      type: 'url',
      message: 'url格式不对',
    },
  ],
  custom: [
    {
      validator: (value, callback) => {
        if (value > 10) {
          callback('不能大于10！');
        }
      },
    },
  ],
  // Async validate
  async: [
    {
      validator: async (value, callback) => {
        if (value > 10) {
          callback('不能大于10！');
        }
      },
    },
  ],
});

schema.validate(
  {
    name: 'pengjiyuan is a nice boy',
    age: 24,
    email: 'pengjiyuan@bytedance.com',
    ip: '127.0.0.1',
    url: 'https://bytedancecom',
    custom: 20,
    async: 20,
  },
  (errors) => {
    console.log(errors);
    /*
     * {
     *  value: 'pengjiyuan is a nice boy', type: 'string', message: '最大长度是10' },
     *  age: { value: 24, type: 'number', message: '在2和5之间' },
     *  url: { value: 'https://bytedancecom', type: 'url', message: 'url格式不对' },
     *  custom: { value: 20, type: 'custom', message: '不能大于10！' }
     * }
     */
  }
);
```

### Validate Messages

Customize the validate messages by `options.validateMessages` or `(new schema({})).messages` / `bv.setGlobalConfig({ validateMessages: {} })`.

```
import bv from 'b-validate';

// set validateMessages for specific validator
const error = bv('', {validateMessages: {required: '必须有值'}}).string.isRequired.end.message;
// output: 必须有值

```

```
import bv from 'b-validate';

// set global validatemessages
bv.setGlobalConfig({ validateMessages: {required: '必须有值'} });

const error = bv('').string.isRequired.end.message;
// output: 必须有值
```

```
import bv from 'b-validate';

const error = bv('', {validateMessages: {required: '必须有值'}}).string.isRequired.end.message;
// output: 必须有值
```

```
import { Schema } from 'b-validate';
import zhCN from 'b-validate/es/locale/zh-CN';

const schema = new Schema({
  name: [{ type: 'string', length: 4 }],
});

schema.messages(zhCN);

schema.validate({name: 'aaa'}, errors => {
  console.log(errors);
  // {name: { message: '字符数必须是 4' }}
})
```

## Api

### String

#### .maxLength(length: number)

Validate string's max length.

#### .minLength(length: number)

Validate string's min length.

#### .length(length: number)

Validate string's length

#### .match(pattern: regexPattern)

Validate whether string is match regex pattern.

#### .uppercase

Validate whether string is uppercase.

#### .lowercase

Validate whether string is lowercase.

#### Demo

```js
bv('vvvv').string.isRequired.minLength(2).maxLength(10).lowercase.end; // pass
```

### Number

#### .min(num: number)

Minimum value.

#### .max(num: number)

Maximum value.

#### .equal(num: number)

Equal to number.

#### .range(min: number, max: number)

In range min ~ max.

#### .positive

Is it a positive number?

#### .negative

Is it a negative number?

#### Demo

```js
bv(123).number.min(2).max(250).positive.end; // pass
```

### Array

#### .length(length: number)

Validate array's length

#### .includes(arrays: any[])

Whether array includes some array.

#### .deepEqual(other: any[])

Deep equal to other array

#### Demo

```js
bv([1, 2, 3]).array.length(3).includes([1, 2]).deepEqual([1, 2, 3]).end; // pass
```

### Object

#### .hasKeys(keys: string[])

Object has some keys?

#### .deepEqual(obj: object)

Deep equal to other object.

#### .empty

Empty object.

#### Demo

```js
bv({ a: 1, b: 2 }).object.hasKeys(['a', 'b']).deepEqual({ a: 1, b: 2 }).not.empty.end; // pass
```

### Type

#### .email

```js
bv('abc@qq.com').type.email.end; // pass
```

#### .url

```js
bv('https://baidu.com').type.url.end; // pass
```

#### .ip

```js
bv('127.0.0.1').type.ip.end; // pass
```

### Custom validator

```js
bv('xxxxxx').custom.validate(
  (value, callback) => {
    if (value !== 'x') {
      callback(`Expect x but got ${value}`);
    }
  },
  (error) => {
    console.log(error); // { value: 'xxxxxx', type: 'string', message: 'Expect x but got xxxxxx' }
  }
);
```

### Async validate

```js
bv('xxxxxx').custom.validate(
  async (value, callback) => {
    const apiValue = api.getValue();
    if (value !== apiValue) {
      callback(`Expect x but got ${apiValue}`);
    }
  },
  (error) => {
    console.log(error);
  }
);
```

## LICENSE

[MIT](./LICENSE) © [PengJiyuan](https://github.com/PengJiyuan)

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