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

univ

v0.1.8

Published

Universal Validator.

Downloads

153

Readme

Universal Validator


NPM version spm package Build Status Coverage Status

Universal Validator.


Install

via [email protected]:

$ spm install univ

via npm:

$ npm install univ

Usage

var Validator = require('univ');

var rules = {
  "username": {
    type: "email",
    required: true,

    // @param {String} value.
    // @param {Function} certifiedCallback, optional.
    custom: function(value, certifiedCallback){
      $.ajax({
        url: "/check-username-available",
        data: "username="+value,
        success: function(data){
          if(data.state === "ok" && data.available = "yes"){
            callback(true);
          }else{
            certifiedCallback(false);
          }
        },
        error: function(){
          certifiedCallback(false);
        }
      });

      //!return undefined;

    }
  },
  "password": {
    type: "password",
    required: true,
    minlength: 6,
    maxlength: 30
  },
  "re-password": {
    type: "password",
    required: true,
    minlength: 6,
    maxlength: 30,
    custom: function(value, callback){
      return value === this.data("password");
    }
  }
};

var validator = new Validator(rules);
validator.validate({
  "username": "[email protected]",
  "password": "PassWord",
  "checkbox": ["check-0", "check-1"]
});

API

Validator(ruler)

constructor, new a validator by rulers.

{
  // rule name.
  "name": {
    type: {TypeEnum(
      text,password,
      radio,checkbox,
      select-one,select-multiple,
      search,textarea,
      number,range,
      date,week,month,time,datetime,datetime-local,
      email,url,tel,color,
      file,
      submit,button,image,
      hidden
    )},
    required: {Boolean},
    max: {Number},
    min: {Number},
    maxlength: {Number},
    minlength: {Number},
    pattern: {RegExp},
    multiple: {Boolean},
    step: {Number},
    accept: {Array<String>},
    custom: {Function}
  },
  "other-name": {
    // ...
  },
  // ...
}

Univ.rule(name, rule)

Set or get a custom rule.

validator.rule("isBankCard", function(values, callback){
  return true;
});

univ.validate(data)

data:

univ.validate(
  {
    "name": "value",
    "other-name": ["item-1", "item-2"],
    // ...
  }
);

Events

valid

单个数据项通过校验,数据合法有效。

validator.on("valid", function(name, value, validity){
  console.log("Field [name=" + name + "] Passed.");
});

invalid

单个数据项未通过校验,数据无效、不合法。

validator.on("valid", function(name, value, validity){
  console.log("Field [name=" + name + "] Failed.");
});

complete

所有数据校验完成。数据是否全部通过校验,则视事件处理函数的对应参数。

// @param {Boolean} certified.
validator.on("complete", function(certified){
  console.log("Form validation", certified ? "Passed" : "Failed");
});

error

校验过程中出现异常,则抛出 error 事件。

// @param {Error} error
validator.on("error", function(error){
});