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 🙏

© 2025 – Pkg Stats / Ryan Hefner

validate-promisify

v0.0.7

Published

A asynchronous form validator by using promise

Readme

validate-promisify

一个Promise风格的表单异步验证工具.

Build Status

安装

    npm i -S validate-promisify
    //or
    yarn add validate-promisify

基础用法

    import Validator, { Schema } from 'validate-promisify';
    
    const validator = new Validator({
        name: Schema.string().required(),
        age: Schema.number().integer()
    });

    validator.validate({
        name: undefined,
        age: 9.9
    }).catch(errors => console.log(errors));

    //输出
    // {
    //   name: [ '必填', '请输出字符串' ], 
    //   age: ['请输入整数'] 
    // }

数据类型

validate-promisify提供5种基础类型的验证, 每种类型声明后可选择其相应的验证方法。

    import Validator, { Schema } from 'validate-promisify';

    //每种类型均可以定义required()规则, 并且其验证会第一个执行, 无论它定义的位置
    const validator = new Validator({
        idNumber: Schema.string().ChineseID().required(),
        weight: Schema.number().float().required(),
        isMale: Schema.boolean(),
        hobbies: Schema.array().unique(),
        introduction: Schema.object().shapeOf({
            yourName: Schema.string().maxLength(4).required(),
            yourAge: Schema.number().greaterThan(0),
            skills: Schema.array().unique()
        })
    });

异步校验

这里的异步特指耗时的验证, 如请求服务器校验或定时器校验等, 因为整个验证流程是基于Promise的异步数据流。

    import Validator, { Schema } from 'validate-promisify';

    const mockRequest = () => new Promise((resolve, reject) => {
        const timer = setTimeout(() => {
            reject();
            clearTimeout(timer);
        }, 2000);
    });

    new Validator({
        username: Schema.string().async(mockRequest, '用户名已被注册')
    }).validate({
        username: 'xxxxxxx'
    }).catch(errors => console.log(errors));

    //输出 { username: ['用户名已被注册'] }

同步验证

Validator.prototype.validate采用将返回Promise, 若验证用过将被resolve, 而验证失败则会被reject。 此外, 利用Validator.prototype.syncValidate可进行同步验证。

注意, 使用syncValidate是不能验证async()规则的。

    import Validator, { Schema } from 'validate-promisify';
    
    const validator = new Validator({
        name: Schema.string().required(),
        age: Schema.number().integer()
    });

    const result = validator.syncValidate({
        name: undefined,
        age: 9.9
    });

    console.log(result);
    //输出
    // {
    //   name: [ '必填', '请输出字符串' ], 
    //   age: ['请输入整数'] 
    // }

本地化

若根据每种规则定义message信息是不可配置的, 推荐使用本地化工具来定制提示信息。

    import Validator, { Schema, lang } from 'validate-promisify';
    
    Schema.config({
        //采用英文提示, 默认使用中文提示, 即lang['zh-CN']
        locale: lang['en']
    })    

默认消息都很丑陋, 你可以定制更加人性化的消息, 格式请参照locale

自定义规则

通过customizeRule可以添加自定义规则。

    import Validator, { Schema } from 'validate-promisify';
    
    const validator = new Validator({
        //一共接受4个配置参数
        character: Schema.string().customizeRule({

            //必填,且必须返回一个Boolean或者Promise
            validator: value => /\*\.\&\$\@/.test(value),

            //可选,若填写将覆盖本地化配置的默认提示
            message: '不能包含特殊字符',

            //可选,本地化文件中提示的key值
            name: 'specification',

            //可选, 本地化文件中{{  }}解析的参数
            extraParams: {
                list: ['*', '.', '&', '$', '@']
            }

        });
    });

API