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

mootools-validations

v0.0.2

Published

Make your Mootools classes validatable.

Downloads

5

Readme

Validations

Validations is an addition to Mootools Class.Extras, providing the ability to run any number of validations on a class.

Validations is inspired by Mootools own Events and by caolan's async.js.

Usage

The most basic usage is to create a group of validations to be run when asked. The validate method takes a callback, with err as the first argument, in the async.js style.

var ValidatableClass = new Class({
    Implements: Validations
});

var validateMe = new ValidatableClass();

var myGreatValidation = function (callback) {
    if (1===2) {
        callback("Something is wrong here.");
    } else {
        callback();
    }
};

validateMe.addValidation(myGreatValidation);

validateMe.validate(function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log("Everything is so great!");
    }
});

Documentation

addValidation(fn)

Add a new validation function, to be executed when validate is called.

Validation functions should take a callback argument, to be called upon deciding whether the validation has passed or failed.

The callback will accept an err as the first argument when the validation fails. When the validation passes, ensure that the first argument passed to the callback is falsey (null, false, etc).

Syntax

myClass.addValidation(fn);

Arguments

  1. fn - (function) The validation function to execute.

Returns

  • (object) This Class instance.

Example

var myValidation = function (callback) {
    if (validateMe.hasNeededProperty()) {
        callback(null);
    } else {
        callback("Does not have Needed Property.");
    }
};

validateMe.addValidation(myValidation);

addValidations(arr)

Add an array of validation functions to be executed when validate is called.

Syntax

myClass.addValidations(arr);

Arguments

  1. arr - (array) An array of validation functions to execute.

Returns

  • (object) This Class instance.

Example

var myValidation = function (callback) {
    if (validateMe.hasNeededProperty()) {
        callback(null);
    } else {
        callback("Does not have Needed Property.");
    }
};

var myOtherValidation = function (callback) {
    if (validateMe.hasOtherNeededProperty()) {
        callback(null);
    } else {
        callback("Does not have Needed Property.");
    }
};

validateMe.addValidations([myValidation, myOtherValidation]);

removeValidation(fn)

Remove a validation function from being executed when validate is called.

Syntax

myClass.removeValidation(fn);

Arguments

  1. fn - (function) The validation function to remove.

Returns

  • (object) This Class instance.

Example

validateMe.removeValidation(myOtherValidation);

removeValidations()

Remove all validation functions from being executed when validate is called.

Syntax

myClass.removeValidations();

Returns

  • (object) This Class instance.

Example

validateMe.removeValidations();

validate(callback)

Each validation function is called with a callback for when they have all finished. If any validation function passes an error to this callback, the main callback is immediately called with the error.

Syntax

myClass.validate(callback, method);

Arguments

  1. callback - (function) Callback function to be executed when all of the validation functions complete, or when any one of them passes an error.
  2. method - (string, optional) async method to use to run validations. Currently limited to parallel, series, and waterfall. Defaults to parallel, if not passed.

Returns

  • (object) This Class instance.

Example

validateMe.validate(function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log("Everything is so great!");
    }
}, 'series');