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

x-validator

v0.1.2

Published

params validator

Downloads

19

Readme

x-validator

import {
  Validator,
} from 'x-validator'

const validator = new Validator()

const errors = validator.validate(
  // 表单数据
  {
    nickname: 'xx',
    age: 101
  },
  // 校验规则
  {
    nickname: {
      type: 'string',
      min: 5,
      max: 10
    },
    age: {
      type: 'int',
      min: 18,
      max: 100
    }
  },
  // 错误信息
  {
    nickname: {
      type: '昵称类型错误',
      min: '昵称请不要少于 5 个字',
      max: '昵称请不要超过 10 个字'
    },
    age: {
      type: '年龄类型错误',
      min: '年龄最小为 18 岁',
      max: '年龄最大为 100 岁'
    }
  }
)

// 如果没有错误,errors 为 undefined
// 如果有错误,格式如下:
{
  nickname: '昵称请不要少于 5 个字',
  age: '年龄最大为 100 岁'
}

内置验证规则

每种规则都支持 required 规则,即数据是否包含某个字段,默认为 true

string

  • type:必须是字符串类型
  • empty:可选,是否可以是 "",默认为 false
  • min: 可选,字符串长度的下限,类似至少输入 n 个字符
  • max: 可选,字符串长度的上限,类似最多输入 n 个字符
  • pattern: 可选,正则校验
  • custom: 可选,支持自定义验证函数
{
  type: 'string',
  empty: true,
  min: 5,
  max: 10000,
  pattern: /^\d+$/,
  custom: function (value) {
    if (value.length !== 6) {
      // 返回错误类型,只要能对应上错误信息中的 key 就行
      return 'custom'
    }
  }
}

integer(别名:int

  • type:必须是整数,浮点数会验证失败
  • min: 可选,整数的下限
  • max: 可选,整数的上限
{
  type: 'integer',
  min: 1,
  max: 100
}

number

  • type:可以是整数或浮点数,NaN 会验证失败
  • min: 可选,数字的下限
  • max: 可选,数字的上限
{
  type: 'number',
  min: 1,
  max: 100
}

boolean(别名:bool

  • type:必须是布尔类型
  • value: 可选,强制为 truefalse
{
  type: 'boolean',
  value: true
}

enum

  • type:必须是 values 中的某一个
  • values: 枚举值
{
  type: 'enum',
  values: [1, 2, 3]
}

array

  • type:必须是数组类型
  • min: 可选,数组长度的下限
  • max: 可选,数组长度的上限
  • itemType: 可选,数组项的类型,常见的类型包括 stringnumberboolean
{
  type: 'array',
  values: [1, 2, 3]
}

object

  • type:必须是对象类型
{
  type: 'object'
}

自定义校验规则

我们可以把一些业务常用的规则,注册到 Validator 实例中,这样可以避免同一个规则配置重复出现在多个地方,提高代码的可维护性。


import {
  Validator,
  checkString,
  checkInteger,
} from 'x-validator'

const validator = new Validator()

// 添加业务字段
validator.add(
  // 校验规则
  {
    name(rule, value) {

      return checkString(
        {
          required: rule.required,
          empty: rule.empty,
          type: 'string',
          min: 5,
          max: 10,
        },
        value
      )

    },
    age(rule, value) {

    	return checkInteger(
    	  {
    	    required: rule.required,
    	    type: 'integer',
    	    min: 18,
    	    max: 100
    	  },
    	  value
    	)

    }
  },
  // 报错信息
  {
    name: {
      required: '请输入用户名',
      empty: '请输入用户名',
      type: '用户名类型错误',
      min: '用户名请不要少于 5 个字',
      max: '用户名请不要超过 10 个字'
    },
    age: {
      required: '请输入年龄',
      type: '年龄类型错误',
      min: '年龄最小为 18 岁',
      max: '年龄最大为 100 岁'
    }
  }
)

// 使用场景
const errors = validator.validate(
  // 表单数据
  {
    nickname: 'this is a long name',
    age: 101
  },
  // 指定 nickname 对应的校验规则为 name
  {
    nickname: 'name',
    age: 'age'
  }
)