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

wildgeese

v0.0.6

Published

Async supported validation library

Downloads

4

Readme

Wildgeese

Validator container for Async Validators.

Currently not implement any validation rules.

Install

npm i --save wildgeese

Usage

const Wildgeese = require("Wildgeese");
const wildgeese = new Wildgeese();

// Register validation rule
wildgeese.addRule([
    {
        name : "required",
        validate : function (value, ctx) {
            return (value == null || value.length === 0) ? `Field ${ctx.label} must be required.` : null;
        }
    }, {
        name : "match-with",
        validate : function (value, ctx) {
            const target = ctx.args.with;
            return (value !== ctx.values[target]) ? `${ctx.label} and ${ctx.labels[target]} not matched.` : null;
        }
    }, {
        name : "uniqueUserId",
        validate : function* (value) {
            // `database.find` expect returns `Promise`.
            const matches = yield database.find({userId: value});
            return matches.length > 0 ? `ID "${value}" already used.` : null;
        }
    }
]);

// Define validation target fields
const fields = wildgeese.makeFieldSet();

//-- fields.add(name, label, rules);
fields.add("username", "Username", ["required", "uniqueUserId"]);
fields.add("password", "Password", ["required"]);
fields.add("password_confirm", "Password confirm", [
    "required",
    ["match-with", {with: "password"}] // with options
]);

// validate
fields.validate({
    username: "wild geese",
    password: "passw0rd",
    password_confirm: "passw0rd",
})
.then(errors => {
    if (errors) {
        // errors : Object
        errors.username && console.error("Username errors", errors.username.join(","));
        errors.password && console.error("Password errors", errors.password.join(","));
        errors.password_confirm && console.error("Password confirm errors", errors.password_confirm.join(","));
        return;
    }

    console.log("All values correctly.");
})
.catch(e => {
    console.error(e);
})

Why Wildgeese returns Promise when validate?

Validator functions are wrapping by co.wrap. it's assitance for async validator implementation.

API

validateFunction

Wildgeese accepts normal Function and also Generator function as validator function

validator function given the below two arguments and must be return errorMessage: String when value is invalid. (if value correctly, validator function my be not return anything.)

  • value : any
    validation target value

  • context : Object
    validation target informations.

    • args : Array
      validation options.
    • label : String
      Human readable field name.
    • labels : Object Human readable other field labels.
    • values : Object
      other field values.
    • options : Object
      User defined options (see Wildgeese#get)

class Wildgeese

static

  • is(value: any, rules:Array<String|Function>)
    validate value with built-in rules.

instance

  • is(value: any, rules: Array<String|Function>) : Promise
    validate an value with rules.

  • get(key: String)
    get user defined options. it's useful validation messages i18n support.

  • set(key: String, value: any)
    set user defined options.

  • addRule(rule: Object, strict = false)

  • addRule(rules: Array<Object>, strict = false)
    Register validation rule.
    The rule is Object of below structure.

    • name: String
    • validate: Function(value: any, values: Object, options: Object) : String|Promise
      function accepts Function or Generator Function.
      it's wrapping by co#wrap in Wildgeese#addRule
    • override: Boolean
      specify overriding existing validation rule.
  • getRule(ruleName: String) : Function
    get ruleNameed validator Function.

  • makeFieldSet() : FieldSet
    make empty FieldSet

class FieldSet

  • clone() : FieldSet create FieldSet clone.

  • add(fieldName: String, label: String, rules: Array<String|Function>)
    add field.

  • remove(fieldName: String)
    remove field from FieldSet.

    it's side effecting on that FieldSet instance.
    if you do not want to give the side effects, use clone() method and remove()ing to cloned instance.

  • fields() : Object
    get defined fields.

  • field(fieldName) : Object
    get fieldName field definition.

  • validate(values: Object) : Promise
    validate fields of values.