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

validatify

v1.0.2

Published

Extremely simple form and input validation for Node.js

Downloads

4

Readme

Extremely simple form and input validation for Node.js

Install

npm install validatify --save

Usage

var Form = require("validatify");

Example

// Initialize a new Form
var myForm = new Form([{
    name: "email",
    validators: {
        required: true,
        email: true
    }
}, {
    name: "password",
    validators: {
        required: true,
        minlength: 8
    }
}, {
    name: "exampleField",
    validators: {
        required: true,
        containsString: "string to test for"
    }
}]);

// Add a custom validator to myForm
myForm.validator("containsString", function (value, string) {
    return value.indexOf(string) > -1;
});

// Listen for "valid" event
myForm.on("valid", function (result) {
    console.log(result);
});

// Listen for "invalid" event
myForm.on("invalid", function (result) {
    // Should not run, as the values we are providing are valid.
    console.log(result);
});

// Run validation
myForm.validate({
    email: "[email protected]",
    password: "this is a string over 8 characters long",
    exampleField: "this field contains the string to test for"
});

Chaining

All functions support chaining, so you can write your code however you want!

new Form([{
    name: "email",
    validators: {
        required: true,
        email: true
    }
}])
.on("valid", function (result) {
    console.log(result);
})
.on("invalid", function (result) {
    // should not be called because the email is valid
    console.log(result);
})
.validate({email: "[email protected]"});

API

Form(fields)

  • fields - An array containing all the fields to iterate over and validate.

    Example

    [
      	{
            name: "email",
            validators: {
                required: true,
                email: true
            }
      	},
      	{
            name: "password",
            validators: {
                required: true,
                minlength: 8
            }
      	}
    ]

.validate(values, continueOnFail)

  • values - An object containing the values for all the fields passed to the constructor.

  • continueOnFail - Optional boolean that when true executes all validators, and does not stop at first failed validator

    Example

    {
        email: "[email protected]",
        password: "an example password",
    }

.validateField(field, value, continueOnFail)

  • field - Name of the field we want to validate.

    Example

    "email"
  • value - Value of the field specified.

    Example

    "[email protected]"
  • continueOnFail - Optional boolean that when true executes all validators, and does not stop at first failed validator

.validator(name, validator)

  • name - Name of the validator to add (this can also be used to overwrite default validators)

  • validator - Function that should return either true or false based on the value and argument

    Example

    new Form([
        {
            name: "someField",
            validators: {
                newValidator: "this is the expectedValue argument"
            }
        }
    ])
    .validator("newValidator", function (value, expectedValue) {
        return value == expectedValue;
    })
    .on("invalid", function (result) {
        // This will run, because the input will fail our new validator's check.
        console.log(result);
    })
    .validateField("someField", "this is not the expected string")

.on(event, callback)

  • event - Name of the event to listen for, can be any of the below:

    • valid
    • valid field
    • invalid
    • invalid field
  • callback - Function that only gets called on any event

    Example

    function (result) {
        console.log(result);
    }

Validators

These are the default validators, however you can add your own easily. (Here's how).

Usage

This is an example of what you would pass to the Form constructor.

[
    {
        name: "someField",
        validators: {
            validatorName: validatorArgument
        }
    }
]

Validator names and usages

  • number - Checks if input is a number Source
  • boolean - Checks if input is either "true" or "false" as a string Source
  • string - Checks if input is a string Source
  • integer - Checks if input is a number without decimals Source
  • float - Checks if input is a number with decimals Source
  • alphanumeric - Checks if input is a string and contains only alphanumeric characters Source
  • email - Checks if input is an email address Source
  • defined - Checks if input is defined Source
  • required - Checks if value is defined and not empty Source
  • range - Checks if value is a number within a range defined as an array Source
  • min - Checks if value is a number more than or equal to the min setpoint Source
  • max- Checks if value is a number less than or equal to the max setpoint Source
  • minlength - Checks if value as a string has a length more than or equal to the min setpoint Source
  • maxlength - Checks if value as a string has a length less than or equal to the max setpoint Source
  • length - Checks if value as a string has a length equal to the setpoint Source
  • regex - Checks if input matches a regex Source