Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rsuite-schema

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rsuite-schema

Schema for data modeling & validation

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by120%
Maintainers
2
Weekly downloads
 
Created
Source

rsuite-schema

数据建模及数据验证

安装

    npm install rsuite-schema --save

示例

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:'foo@bar.com',
    age:40
})

console.log(checkResult);

checkResult 返回结构是:

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

自定义验证


const myModel = SchemaModel({
    field1: StringType().addRule((value) => {
        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}$/, '请输入合法字符')
});

API

StringType

  • isEmail(String errorMessage)
  • isURL(String errorMessage)
  • isOneOf(String errorMessage)
  • containsLetter(String errorMessage)
  • containsUppercaseLetter(String errorMessage)
  • containsLowercaseLetter(String errorMessage)
  • containsLetterOnly(String errorMessage)
  • containsNumber(String errorMessage)
  • pattern(Object regexp, String errorMessage)
  • rangeLength(Number minLength, Number maxLength, String errorMessage)
  • minLength(Number minLength, String errorMessage)
  • maxLength(Number maxLength, String errorMessage)

NumbserType

  • isInteger(String errorMessage)
  • isOneOf(String errorMessage)
  • pattern(Object regexp, String errorMessage)
  • range(Number minLength, Number maxLength, String errorMessage)
  • min(Number min, String errorMessage)
  • max(Number min, String errorMessage)

ArrayType

  • rangeLength(Number minLength, Number maxLength, String errorMessage)
  • minLength(Number minLength, String errorMessage)
  • maxLength(Number maxLength, String errorMessage)
  • unrepeatable(String errorMessage)
  • shape(Object type, String errorMessage)

DateType

  • range(Date min, Date max, String errorMessage)
  • min(Date min, String errorMessage)
  • max(Date max, String errorMessage)

Keywords

FAQs

Package last updated on 18 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc