npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rsuite-schema

v0.1.0

Published

Schema for data modeling & validation

Downloads

76

Readme

rsuite-schema

rsuite-schema 是一个数据建模及数据验证工具

版本与状态

npm Travis

安装

npm i rsuite-schema --save
// 或者
yarn add rsuite-schema

示例

import { SchemaModel, StringType, DateType, NumberType } from 'rsuite-schema';

const userModel = SchemaModel({
  username: StringType().isRequired('用户名不能为空'),
  email: StringType().isEmail('请输入正确的邮箱'),
  age: NumberType('年龄应该是一个数字').range(18, 30, '年应该在 18 到 30 岁')
});

const checkResult = userModel.check({
  username: 'foobar',
  email: '[email protected]',
  age: 40
});

console.log(checkResult);

checkResult 返回结构是:

{
    username: { hasError: false },
    email: { hasError: false },
    age: { hasError: true, errorMessage: '年应该在 18 到 30 岁' }
}

多重验证

StringType()
  .minLength(6, '不能少于 6 个字符')
  .maxLength(30, '不能大于 30 个字符')
  .isRequired('该字段不能为空');

自定义验证

通过 addRule 函数自定义一个规则。

如果是对一个字符串类型的数据进行验证,可以通过 pattern 方法设置一个正则表达式进行自定义验证。

const myModel = SchemaModel({
  field1: StringType().addRule((value, data) => {
    return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
  }, '请输入合法字符'),
  field2: StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '请输入合法字符')
});

schema.check({ field1: '', field2: '' });

/**
{
  field1: {
    hasError: true,
    errorMessage: '请输入合法字符'
  },
  field2: {
    hasError: true,
    errorMessage: '请输入合法字符'
  }
};
**/

自定义验证

例如,验证两次输入密码是否一致

const schema = SchemaModel({
  password1: StringType().isRequired('该字段不能为空'),
  password2: StringType().addRule((value, data) => {
    if (value !== data.password1) {
      return false;
    }
    return true;
  }, '两次密码不一致')
});

schema.check({ password1: '123456', password2: 'root' });

/**
{
  password1: { hasError: false },
  password2: {
    hasError: true,
    errorMessage: '两次密码不一致'
  }
};
**/

API

StringType

  • isRequired()
StringType().isRequired('该字段不能为空');
  • isEmail(String: errorMessage)
StringType().isEmail('请输入正确的邮箱地址');
  • isURL(String: errorMessage)
StringType().isURL('请输入正确的URL地址');
  • isOneOf(Array: items, String: errorMessage)
StringType().isOneOf(['Javascript', 'CSS'], '只能输入 `Javascript`和 `CSS`');
  • containsLetter(String: errorMessage)
StringType().containsLetter('必须包含英文字符');
  • containsUppercaseLetter(String: errorMessage)
StringType().containsUppercaseLetter('必须包含大写的英文字符');
  • containsLowercaseLetter(String: errorMessage)
StringType().containsLowercaseLetter('必须包含小写的英文字符');
  • containsLetterOnly(String: errorMessage)
StringType().containsLetterOnly('只能包含的英文字符');
  • containsNumber(String: errorMessage)
StringType().containsNumber('必须包含数字');
  • pattern(Object: regexp, String: errorMessage)
StringType().pattern(/^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/, '请输入合法字符');
  • rangeLength(Number: minLength, Number: maxLength, String: errorMessage)
StringType().rangeLength(6, 30, '字符个数只能在 6 - 30 之间');
  • minLength(Number: minLength, String: errorMessage)
StringType().minLength(6, '最小需要6个字符');
  • maxLength(Number: maxLength, String: errorMessage)
StringType().minLength(30, '最大只能30个字符');
  • addRule(Function: onValid, String: errorMessage)
StringType().addRule((value, data) => {
  return /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/.test(value);
}, '请输入合法字符');

NumbserType

  • isRequired()
NumbserType().isRequired('该字段必填');
  • isInteger(String: errorMessage)
NumbserType().isInteger('只能是整型');
  • isOneOf(Array: items, String: errorMessage)
NumbserType().isOneOf([5, 10, 15], '只能是`5`,`10`,`15`');
  • pattern(Object: regexp, String: errorMessage)
NumbserType().pattern(/^[1-9][0-9]{3}$/, '请输入合法字符');
  • range(Number: minLength, Number: maxLength, String: errorMessage)
NumbserType().range(18, 40, '请输入 18 - 40 之间的数字');
  • min(Number: min, String: errorMessage)
NumbserType().min(18, '最小值 18');
  • max(Number: min, String: errorMessage)
NumbserType().max(40, '最大值 40');
  • addRule(Function: onValid, String: errorMessage)
NumbserType().addRule((value, data) => {
  return value % 5 === 0;
}, '请输入有效的数字');

ArrayType

  • isRequired()
ArrayType().isRequired('该字段必填');
  • rangeLength(Number: minLength, Number: maxLength, String: errorMessage)
ArrayType().rangeLength(1, 3, '至少选择1个,但不能超过3个');
  • minLength(Number: minLength, String: errorMessage)
ArrayType().minLength(1, '至少选择1个');
  • maxLength(Number: maxLength, String: errorMessage)
ArrayType().maxLength(3, '不能超过3个');
  • unrepeatable(String: errorMessage)
ArrayType().unrepeatable('不能出现重复选项');
  • of(Object: type, String: errorMessage)
ArrayType().of(StringType().isEmail(), '格式错误');
  • addRule(Function: onValid, String: errorMessage)
ArrayType().addRule((value, data) => {
  return value.length % 2 === 0;
}, '好事成双');

DateType

  • isRequired()
DateType().isRequired('日期不能为空');
  • range(Date: min, Date: max, String: errorMessage)
DateType().range(
  new Date('08/01/2017'),
  new Date('08/30/2017'),
  '时间应该在 08/01/2017 - 08/30/2017 之间'
);
  • min(Date: min, String: errorMessage)
DateType().min(new Date('08/01/2017'), '时间的最小值 08/01/2017');
  • max(Date: max, String: errorMessage)
DateType().max(new Date('08/30/2017'), '时间的最大值 08/30/2017');
  • addRule(Function: onValid, String: errorMessage)
DateType().addRule((value, data) => {
  return value.getDay() === 2;
}, '只能选择周二');

ObjectType

  • isRequired()
ObjectType().isRequired('该对象不能为空');
  • shape(Object: types)
ObjectType().shape({
  email: StringType().isEmail('应该是一个 email'),
  age: NumberType().min(18, '年龄应该大于18岁')
});
  • addRule(Function: onValid, String: errorMessage)
ObjectType().addRule((value, data) => {
  if (value.id || value.email) {
    return true;
  }
  return false;
}, 'id 与 email 必须有一个不能为空');

BooleanType

  • isRequired()
BooleanType().isRequired('该字段不能为空');
  • addRule(Function: onValid, String: errorMessage)
ObjectType().addRule((value, data) => {
  if (typeof value === 'undefined' && A === 10) {
    return false;
  }
  return true;
}, '当 A 等于 10 的时候,该值必须为空');