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

schema-typed

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

schema-typed

Schema for data modeling & validation

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
66K
decreased by-8.14%
Maintainers
1
Weekly downloads
 
Created
Source

schema-typed

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

版本与状态

npm Travis

安装

npm install schema-typed --save

示例

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

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 岁' }
}

多重验证

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 的时候,该值必须为空');

Keywords

FAQs

Package last updated on 30 Aug 2018

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