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

async-validate.js

v1.1.3

Published

transform all validator to async or promise

Downloads

12

Readme

##Async-Avalidate 是什么 Async-Avalidte是一个支持同步与异步模式的验证框架, 它可以十分方便的把验证条件转换为同一步模式进行处理, 并返回给开发者一个经过组织的结果

##构建状态 [build-image]: https://travis-ci.org/darkty2009/async-validate.svg [build-url]: https://travis-ci.org/darkty2009/async-validate [license-image]: http://img.shields.io/badge/license-MIT-blue.svg [license-url]: https://github.com/darkty2009/async-validate/blob/master/LICENSE [![MIT License][license-image]][license-url]

安装

Node使用:

npm install async-validate.js

浏览器使用:

<script type="text/javascript" src="validate.async.js"></script>

所有的代码都在validate.async.js文件里, 当然,你也可以从这个链接下载

##快速起步

AValidate({
    val:/^\w+$/
}, {val:"abcdefg1"});
// console: {val:"error"}
AValidate({
    val:{
        required:true,
        custom:/^\w+$/
    }
}, {val:"abcdefg1"});
// console: {val:{custom:"error"}}
AValidate({
    val:/^\w+$/
}, {val:"abcdefg"});
// console: null

当验证成功时,方法会返回null。 当验证失败时,方法会返回具体的错误信息。

##异步模式

AValidate.async({
    val:{
        required:true,
        exist:function(opt, resolve, reject) {
            $.ajax({
                url:"validate-user-id-exist",
                data:{id:opt.value},
                success:function() {
                    resolve();
                },
                error:function() {
                    reject("is-not-exist");
                }
            })
        }
    }
}, {val:12001}).then(function(message) {
    // console: message is null or undefined
}).catch(function(message) {
    // console: {val:{exist:'is-not-exist'}}
});

##默认的验证规则

required

参数:可选 true | false

length

参数: 可选 [number] | [object]

{
    is:[number],
    max:[number],
    min:[number]
}

若参数为数字,则默认将其赋值给 [object].is

number

参数: 可选 [number] | [object]

{
    '>':[number],
    '>=':[number],
    '==':[number],
    '<=':[number],
    '<':[number],
    'or':false | true
}

若参数为数字,则默认将其赋值给 [object]['==']。 参数中的 or 默认为 false, 例如设置了 {'>=':1, '<':10} 转换后的条件为 val >=1 && val < 10 设置了 {'>=1':1, '<':10, or:true} 转换后的条件为 val >=1 || val < 10

include

参数: [array] 该规则的作用是验证给定的值是否在数组中

exclude

参数: [array] 该规则的作用是验证给定的仠不在数组中

email

参数: true | false 验证当前值是否为email格式

char

参数: [object]

{
    'number':true | false,
    'symbol':true | false,
    'chinese':true | false,
    'english':true | false
}

验证当前值是否满足字符类型要求 参数包含四个属性(默认值均为false):

  • number 数字
  • symbol 特殊符号 注:`~!@#¥%……&*()_-=+[]|;:,.
  • chinese 中文
  • english 英文

自定义同步验证

AValidate.add('same_password', function(opt) {
    if(opt.value != opt.data.password) {
        return false;
    }
    return true;
});

参数: opt 包含三个属性

  • value 当前字段的输入值
  • data 当前所有的输入值
  • param 当前验证条件的参数

自定义异步验证

AValidate.add('user_exist', function(opt, resolve, reject) {
    $.ajax({
        url:"user_exist_check",
        data:{
            user:opt.value
        },
        success:function() {
            resolve();
        },
        error:function() {
            reject();
        }
    });
});

参数: opt 包含三个属性

  • value 当前字段的输入值
  • data 当前所有的输入值
  • param 当前验证条件的参数

参数: resolve 验证成功时调用 参数: reject 验证失败时调用

BUG反馈

先谢谢所有提供反馈的开发者 EMAIL: darkty2009#gmail.com 或者 issues