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

validate-pkg

v1.0.14

Published

A simple and easy-to-use data validation library

Downloads

30

Readme

validate-pkg

Install

npm install validate-pkg --save-dev;

Usage

import validate from 'validate-pkg';

const data = { name: '' };

/** rules:支持数组/对象写法 */
const rules = {
    name: 'name',
    rule: 'required' || ['required'] || () => 'required' || ['required'],
    message: '请输入您的姓名哦~' || ['请输入您的姓名哦~'] || () => '请输入您的姓名哦~' || ['请输入您的姓名哦~'],
    validator: [
        {
            message: 'validator:支持数组/对象写法',
            callBack: (value, method) => value || method.nullOrEmpty(value)
        }
    ]
};

try {
    await validate(data, rules);
    await validate(data, [rules]);

} catch({ message }) {
    console.log(message);
}

Uniapp

import validate from 'validate-pkg/uniapp';

const data = { name: '' };

/** rules:支持数组/对象写法 */
const rules = {
    name: 'name',
    rule: 'required' || ['required'] || () => 'required' || ['required'],
    message: '请输入您的姓名哦~' || ['请输入您的姓名哦~'] || () => '请输入您的姓名哦~' || ['请输入您的姓名哦~'],
    validator: [
        {
            message: 'validator:支持数组/对象写法',
            callBack: (value, method) => value || method.nullOrEmpty(value)
        }
    ]
};

try {
    await validate(data, rules);
    await validate(data, [rules]);
    await validate(data, [rules], showToastOptions);

} catch({ message }) {
    console.log(message);
}

Api

function validate(data, rules): Promise<void | Error>;

Type

import { Data, Value, Rules, Rule, Validator, Method } from 'validate-pkg';
import type { Data, Value, Rules, Rule, Validator, Method } from 'validate-pkg';
import { Data, Value, Rules, Rule, Validator, Method, ShowToastOptions } from 'validate-pkg/type';
import { Data, Value, Rules, Rule, Validator, Method, ShowToastOptions } from 'validate-pkg/uniapp';

validate.Data

| Data | Object | | :----: | :------ | | Data.key | string | | Data.value | Value |

validate.Value

| Value | string / number / (string / number)[] | | :----: | :------------------------------------- |

validate.Rules

| Rules | Object / Object[] | | :--------: | :---------------- | | Rules.name | string | | Rules.rule | [Rule === string] Rule / Rule[] / (() => Rule / Rule[]) | | Rules.message | string / string[] / (() => string / string[]) | | Rules.validator | Validator === Object / Object[] |

validate.Rule

| Rule | [Rule === string] Rule / Rule[] / (() => Rule / Rule[]) | | :--------: | :------------------------------------------------------ | | Rule | required 必填; / mobile 手机号; / email 邮箱; / carNo 车牌号; / idCard 身份证; / amount 金额; / number 数字; / chinese 中文; / notChinese 非中文; / english 英文; / enAndNo 英文和数字; / enOrNo 英文或数字; / special 特殊字符; / emoji 表情符; / date 日期; / url 地址; / same 与某个字段相同; / range 在某个范围内; / minLength 最小长度; / maxLength 最大长度; / keyword 关键字; / enAndNoANSpecial 是否两种或以上的数字/小写字母/大写字母/其他特殊符号的组合; |

validate.Validator

| Validator | Validator === Object / Object[] | | :---------: | :------------------------------------------------------ | | Validator.message | string / string[] / (() => string / string[]) | | Validator.callBack | (value, method) => boolean |

Validator.Method

| Method | Object | | :---------------------: | :-------------------------------- | | Method.url | (value: string) => boolean 是否url | | Method.same | (value1: unknown, value2: unknown) => boolean 是否与某个字段相同 | | Method.date | (value: string) => boolean 是否日期 | | Method.email | (value: unknown) => boolean 是否邮箱 | | Method.carNo | (value: string) => boolean 是否车牌号 | | Method.emoji | (value: string) => boolean 是否表情符 | | Method.range | (value: unknown, range1: unknown, range2: unknown) => boolean 是否在某个范围内 | | Method.enOrNo | (value: unknown, [boolean: boolean = true]是否包含特殊字符) => boolean 是否英文或数字 | | Method.amount | (value: string / number) => boolean 是否金额 | | Method.number | (value: string / number) => boolean 是否数字 | | Method.idCard | (value: string / number) => boolean 是否身份证 | | Method.mobile | (value: string / number) => boolean 是否手机号 | | Method.keyword | (value: string, keywords: string) => boolean 是否关键字 | | Method.enAndNo | (value: string) => boolean 是否 6~32 位数字和字母组合 | | Method.chinese | (value: string) => boolean 是否中文 | | Method.english | (value: string) => boolean 是否英文 | | Method.special | (value: string) => boolean 是否特殊字符 | | Method.minLength | (value: (string / number)[], min: string / number) => boolean 是否最小长度 | | Method.maxLength | (value: (string / number)[], max: string / number) => boolean 是否最大长度 | | Method.notChinese | (value: string) => boolean 是否不包含中文,可以有特殊字符 | | Method.nullOrEmpty | (value: unknown) => boolean 是否为空 | | Method.enAndNoANSpecial | (value: string) => boolean 是否两种或以上的数字/小写字母/大写字母/其他特殊符号的组合 |

Validator.ShowToastOptions

  • uniapp文档[https://uniapp.dcloud.net.cn/api/ui/prompt.html#showtoast]

example

const rules = {
    name: 'name',
    rule: () => 'required',
    rule: () => ['required'],
    message: () => '请输入您的姓名哦~',
    message: () => ['请输入您的姓名哦~'],
};

const rules1 = {
    name: 'name',
    rule: 'required',
    message: '请输入您的姓名哦~',
};

const rules2 = {
    name: 'name',
    rule: 'chinese',
    message: '姓名只支持中文哦~',
};

const rules1An2 = {
    name: 'name',
    rule: ['required', 'chinese'],
    message: ['请输入您的姓名哦~', '姓名只支持中文哦~'],
};

const rules3 = {
    name: 'name',
    rule: 'required',
    message: '请输入您的姓名哦~',
    validator: {
        message: '姓名只支持中文哦~',
        message: ['姓名只支持中文哦~'],
        message: () => '姓名只支持中文哦~',
        message: () => ['姓名只支持中文哦~'],
        callBack: function(value, method) {
            return value
            return !method.nullOrEmpty(value)
        }
    }
};

const rules3 = {
    name: 'name',
    rule: 'required',
    message: '请输入您的姓名哦~',
    validator: [
        {
            message: '姓名只支持中文哦~',
            callBack: function(value, method) {
                return value
                return !method.nullOrEmpty(value)
            }
        }, {
            message: '姓名应在 2~6 个字符之间哦~'
            callBack: value => value?.length >= 2 && value?.length <= 6
        }
    ]
};