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

@ntbl/validator

v0.0.1

Published

一个内置验证规则的数据验证库

Downloads

4

Readme

validator

GitHub npm MIT

validator 是一个内置验证规则的数据验证库。

Installation

npm i @ntbl/validator --save

Usage

import Validator from '@ntbl/validator'

// 表单数据
const formData = {
  username: 'sunny',
  password: '1234567',
  mali: '[email protected]',
  point: [4, 4, 5]
};

// 验证规则
const rules = {
  username: [
    // 使用内置的自定义规则
    {rule: 'required', msg: '必须填写用户名'},
  ],

  password: [
    {rule: 'required', msg: '必须填写密码'},
    // 使用数组的方式带参数
    {rule: ['min', 6], msg: '不少于 6 个字符'},
    {rule: ['max', 16], msg: '不超过 16 个字符'},
  ],

  mali: [
    {rule: 'required', msg: '必须填写邮箱'},
    // 使用 validator.js 库支持的验证规则
    {rule: 'isEmail', msg: '邮箱格式不正确'}
  ],

  point: [
    // 或者自定义函数
    {rule: d => d.every(e => e > 3) , msg: '骰子的点数必须都大于 3 点'}
  ]
};

const validator = Validator(formData, rules);

// 验证所有的字段
// 返回 undefined,即无错误信息,表示验证通过
console.log(validator.validate())   // undefined

// 仅验证 emil 字段
console.log(validator.validateOf('emil'))   // undefined
const formData1 = {
  username: 'sunny',
  password: '123',
  mali: 'hsy.ntbl',
  point: [4, 4, 5]
};

const validator2 = Validator(formData1, rules);

// 验证未通过时,将返回一个错误数组

console.log(validator2.validate())
/*
    [
        { name: 'password', msg: '不少于 6 个字符' },
        { name: 'mali', msg: '邮箱格式不正确' }
    ]
*/

// 请注意 validateOf 返回的是一个错误对象
console.log(validator2.validateOf('mali'))
// { name: 'mali', msg: '邮箱格式不正确' }

验证规则

validator 内置了所有 validator.js 支持的验证规则。

除此之外,validator 还内置以下方便使用的自定义验证规则:

  • required(): 不能为空
  • min(n): 字符串/数值的长度或数值或不小于指定大小
  • max(n): 字符串/数值的长度或数值或不小于指定大小
  • is(pattern): 自定义验证规则