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 🙏

© 2026 – Pkg Stats / Ryan Hefner

validator-forms

v0.0.3

Published

A pack of validation rules and helpers for form validation, built on top of validator.js

Readme

validator-forms

A pack of validation rules and helpers built on top of validator.js, focused on making forms validation more flexible and easier. Validator Forms will be handy if you use form libraries like react-final-form, redux-form, formik and others.

Installation

You might need to use Validator itself, so it is a peer dependency and should be installed separately.

npm install --save validator-forms validator

or

yarn add validator-forms validator

Getting Started

All the validation rules provided by Validator Forms have signature (msg, ...opts) => val => [null | msg]. msg is a message to be displayed if validation fails, opts are the validation rule's properties. The returned function takes one parameter val which is a value to be validated, and returns null if the value passed the validation, msg otherwise.

A simple example of how it works with react-final-form:

import React from 'react';
import { Form, Field } from 'react-final-form';
import { required } from 'validator-forms';

const validatedFieldRequired = required('This field is required');

const MyValidatedForm = ({ onSubmit }) => (
  <Form
    onSubmit={onSubmit}
    render={({ form, handleSubmit }) => (
      <form onSubmit={handleSubmit}>
        <Field name="validatedField" validate={validatedFieldRequired}>
        {({ input, meta }) => (
          <div>
            <label>Required Field</label>
            <input type="text" {...input} />
            {meta.error && <span>{meta.error}</span>}
          </div>
        )}
        </Field>
        <button type="submit">Submit</button>
      </form>
    )}
  />
);

Now if the the field is empty, This field is required will appear.

Validation Rules

Validator Forms provides all the validation rules from Validator as well as a few more, covering common form validation cases. For example, if you want to use isAlpha(str [, locale]) rule from Validator, the corresponding rule will be

import { isAlpha } from 'validator-forms';
// under the hood it's pretty much
// (msg, locale) => val => validator.isAlpha(val, locale) ? null : msg;

The first parameter of a helper is always msg which will be returned if validation fails. The rest of the parameters are parameters of the original validation rule. You can check them in documentation. The returned value is a function taking a single argument val and returning null if val passes the validation, msg otherwise.

You don't have to sanitize input values for validation rules, nulls, undefineds and everything else will be processed correctly.

API

Validator-like rules

All the validation rules from Validator have equivalents in Validator Forms with signature (msg, ...opts) => val => [null | msg] (read more in Validation Rules section). You can check them in documentation for Validator.

Custom rules

| Rule | Description | | -------------------------------------------- | --------------------------------------------------------------------------------------- | | required(msg) | checks if applied string is not null or whitespace. | | maxLength(msg, maxLength) | checks if applied string's length is less than maxLength | | minLength(msg, minLength) | checks if applied string's length is greater than minLength | | lengthBetween(msg, minLength, maxLength) | checks if applied string's length falls in a range between minLength and maxLength. |

Helpers for Complex Validation Cases

In many cases you need to do multiple checks for a single value. For example, you want to show This field is required if the input is empty and This field must be shorter than 10 symbols if it's too long. For such situations Validator Forms provides helpers.

applyRules(rules) => val => [null | msg] where rules is a single Validator Forms rule or an array of rules. Returns a function taking a single argument val and applying it to all the provided validation rules. Returns null if all the checks passed or msg of the first failed check.

applyRulesReturnAll(rules) => val => [null | msgs]. Similar to applyRules but unlike it applyRulesReturnAll returns msgs array of all error messages of the failed checks, if there are any, null otherwise.

applyIfNotEmpty(rules) => val => [null | msg] where rules is a singleValidator Forms rule or an array of rules. Returns a function taking a single argument val and applying it to all the provided validation rules only if val is not empty. The returned function returns null if all the checks are passed or msg of the first failed check.

applyIfNotEmptyReturnAll(rules) => val => [null | msgs]. Similar to applyIfNotEmpty but unlike it applyIfNotEmptyReturnAll returns msgs array of all error messages of the failed checks, if there are any, null otherwise.

Note that val is considered empty if it contains only whitespaces.

License

MIT