2.2.3 • Published 2 years ago

soke v2.2.3

Weekly downloads
68
License
-
Repository
github
Last release
2 years ago

soke

轻量级快速校验数据

  • 零依赖
  • 支持组合校验、多选一校验、多参数交集校验
  • 为每个校验自定定义错误
  • 正则可以组合使用
  • 支持 Typescript

Install

$ npm install soke

Use in Javascript / Typescript

import { soke } from "sock";

Use

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

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: "mail@qqq.com",
  code: "666666",
  password: "123123Qwe",
  passwordAgain: "123123Qwe",
  agree: true,
  cat: "123123Qwe",
};

let v2 = schema.dto(value);

expect(value).toEqual({
  name: "aaa",
  phone: "13333333333",
  email: "mail@qqq.com",
  code: "666666",
  password: "123123Qwe",
  passwordAgain: "123123Qwe",
  agree: true,
});

expect(v2).toEqual(value);

Test

Run test

$ npm run test-s
Test Suites
8 passed, 8 total

Tests
41 passed, 41 total
2.2.1

2 years ago

2.1.2

2 years ago

2.0.3

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.2.3

2 years ago

2.2.2

2 years ago

2.1.3

2 years ago

2.0.4

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.5

3 years ago

0.0.3

6 years ago

0.0.1

6 years ago