https://github.com/ymzuiku/soke
Tiny verify body schema
https://github.com/ymzuiku/soke
Last synced: about 2 months ago
JSON representation
Tiny verify body schema
- Host: GitHub
- URL: https://github.com/ymzuiku/soke
- Owner: ymzuiku
- License: mit
- Created: 2020-12-14T06:31:59.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-18T22:31:04.000Z (almost 3 years ago)
- Last Synced: 2025-02-20T17:48:58.168Z (2 months ago)
- Language: TypeScript
- Size: 430 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# soke
> 轻量级快速校验数据
- 零依赖
- 支持组合校验、多选一校验、多参数交集校验
- 为每个校验自定定义错误
- 正则可以组合使用
- 支持 Typescript## Install
```sh
$ npm install soke
```Use in Javascript / Typescript
```js
import { soke } from "sock";
```## Use
```js
const schema = soke.object({
name: soke.string().min(2, "to min").max(10, "to max"),
email: soke.string().email("need a email"),
phone: soke
.string()
.required("need input phone")
.chinaPhone("phone formart error"),
code: soke.string("need input code").required().matches(/^\d+$/).len(6),
agree: soke.bool().required("need click agree"),
password: soke
.string()
.required("need input password")
.passwordStrong("password is too weak"),
passwordAgain: soke
.string("password again need equal to password")
.required()
.equalByKey("password"),
});
const errors = schema.validate({
name: "a",
phone: "133",
code: "11",
password: "123",
passwordAgain: "456",
});expect(errors.errors).toEqual({
name: "to min",
email: "need a email",
phone: "phone formart error",
code: "need input code",
agree: "need click agree",
password: "password is too weak",
passwordAgain: "password again need equal to password",
});
expect(errors.error).toEqual("to min");
```## Validate and dto
```ts
const schema = soke.object({
name: soke.string().min(2, "to min").max(10, "to max"),
email: soke.string().email("need a email"),
phone: soke
.string()
.required("need input phone")
.chinaPhone("phone formart error"),
code: soke.string("need input code").required().matches(/^\d+$/).len(6),
agree: soke.bool().required("need click agree"),
password: soke
.string()
.required("need input password")
.passwordStrong("password is too weak"),
passwordAgain: soke
.string("password again need equal to password")
.required()
.equalByKey("password"),
});try {
const errors = schema.dto({
name: "a",
phone: "133",
code: "11",
password: "123",
passwordAgain: "456",
});
} catch (err) {
expect(err).toEqual(new Error("to min"));
}const value = {
name: "aaa",
phone: "13333333333",
email: "[email protected]",
code: "666666",
password: "123123Qwe",
passwordAgain: "123123Qwe",
agree: true,
cat: "123123Qwe",
};let v2 = schema.dto(value);
expect(value).toEqual({
name: "aaa",
phone: "13333333333",
email: "[email protected]",
code: "666666",
password: "123123Qwe",
passwordAgain: "123123Qwe",
agree: true,
});expect(v2).toEqual(value);
```
## Test
Run test
```sh
$ npm run test-s
``````sh
Test Suites
8 passed, 8 totalTests
41 passed, 41 total
```