Socket
Socket
Sign inDemoInstall

base-validator

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    base-validator

基础的表单验证器


Version published
Weekly downloads
8
increased by700%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

base-validator

基础的表单验证器。用于 Browser 和 Node.js 环境下校验用户提交的数据。

在确保校验准确度的前提下,尽可能放宽规则。例:

  • 符合数字格式的字符串或Number类型均可以通过 number 校验。

  • 允许数据前后包含空白字符,用户可能不会注意到输入了空白字符。

  • 0false 出现在表单内,常作为 <input type="number" /><input type="checkbox" /> 的结果值。应当作有效值,可以通过 required 校验。

  • 包含一个或多个空白字符的字符串,不包含任何可见字符,可能是用户无意中输入,但发现不到,这应当被 required 校验阻止。

Usage

npm i -S base-validator
const Validator = require('base-validator')

const assert = [
    {
        prop: 'phone',
        msg: '手机号码不能为空',
        assert: 'required'
    },
    {
        prop: 'phone',
        msg: '手机号码长度应为11位',
        assert: 'betweenLength',
        expected: [11, 11]
    },
    {
        prop: 'phone',
        msg: '手机号码不正确',
        assert: 'tel'
    },
    {
        prop: 'code',
        msg: '验证码不能为空',
        assert: 'required'
    },
    {
        prop: 'code',
        msg: '验证码应为6位',
        assert: 'betweenLength',
        expected: [6, 6]
    },
    {
        prop: 'code',
        msg: '验证码不正确',
        assert: 'regular',
        expected: '/^\\d{6}$/'
    }
]

const form = {
    phone: ' ',
    code: '23342'
}

const msg = Validator(assert, form)
console(msg)

Assert

assert描述
tel手机号码
email电子邮箱
idcard居民身份证
urlurl
min最小值
max最大值
minlength最小长度值
maxlength最大长度值
number允许 Number 类型或符合数字格式的字符串(前后可包含一个或多个空白字符,可以是小数和负数)
digits整数
between在最小值和最大值之间
betweenLength在最小长度值和最大长度值之间
equalTo值相等
contains包含给定值
in在给定值内存在
regular正则校验。期望值是 RegExp 类型或 string 类型(正则字面量格式)
required必填。0false 属于有效值,仅包含一个或多个空白字符的字符串属于无效值

Keywords

FAQs

Last updated on 01 Feb 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc