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

redux-form-validations

v0.1.5

Published

Validation utilities for redux-form v6

Downloads

123

Readme

redux-form-validations

A collection of utilities for validations in redux-form V6

Note: this project is under initial development, API may change (we will follow sem-ver).

Example

$ npm install --save redux-form-validations
import { React } from 'react';
import { reduxForm } from 'redux-form';
import { buildValidations, isPresent, isZipCode, isDateInPast, isDateAfter } from 'redux-form-validations';
import { uniq } from 'lodash';

const { warn, validate } = buildValidations({
  firstName: {
    validate: isPresent
  },
  lastName: {
    warn: {
      ...isPresent,
      errorMessage: 'Last names are helpful'
    }
  },
  zipCode: {
    validate: {
      ...isZipCode,
      required: true
    }
  },
  dateApplied: {
    validate: [
      isDateInPast,
      isDateAfter('04/19/1988'),
      {
        ...isPresent,
        validateIf: (allValues, value) => allValues.dependentField
      }
    ]
  },
  _fieldArrays: {
    hobbies: {
      _error: {
        validate: {
          validator: (fields) => {
            const names = field.map(field => field.name);
            return uniq(names).length === names.length;
          },
          errorMessage: 'Hobby names must be unique'
        }
      }
    }
  }
});

class MyForm extends React.Component { ... }

export default reduxForm({ warn, validate })(MyForm);

Custom validators

Every field defined in your schema object has top-level keys warn and validate which are used to build warn and validate functions for redux-form connected components. These keys take a validator object, which has the shape:

validator: Function (allFields, value) => Boolean If the return value is true, then the field is valid, if false, an error is added. errorMessage: String || Function (allField, value) => String The error message that is returned with the validate or warn function call when the validator returns false.

Alternatively, you can assign a function that returns a validator object to either warn or validate that will receive props from your ReduxForm wrapped component. This is helpful if you need to validate based on information outside of the form state.

Example:

const { warn, validate } = buildValidations({
  firstName: {
    validate: (props) => ({
      validator: (fields) => {
        return props.someBoolean && fields.numbers > 0,
      }
    }),
  },
});

Prior work

redux-form-schema is the main inspiration for this project. After redux-form moved to V6, this project was deprecated, since a large part of its functionality was obselete. However, we still liked being able to define our validations with a simple and declarative API. This small library aims to supply that abstraction with some built-in validators and configuration options.

Contributing/ Todos

This is a green project, and we are open to feature suggestions and other improvements. Additionally, you can contribute by helping with these outstanding todos:

  • Tighten up API for V1
  • Add documentation
  • Add more validators for common uses
  • More configuration options