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-sync-validation

v0.6.0

Published

Synchronous validation for redux-form using object schemas.

Downloads

151

Readme

Synchronous Validation for redux-form using object schemas

The aim of this library is to provide schema validation that integrates easily with redux-form. The interface for this libarary is heavily based on JOI, the advantages that this library has over JOI are as follows:

  • The validate function automatically returns errors in a format that redux-form understands.
  • Everything is flow typed to ensure that errors are caught as soon as possible.
  • It is easily extensible in an immutable style, meaning that extensions can be scoped to certain areas of a project easily.
  • It is flexible and supports custom error messages at a property level as well as partial message helpers like withLabel.

Installation

yarn add redux-form-sync-validation or npm install redux-form-sync-validation --save

Use

To use the validation declare a schema as follows:

// model.js
// @flow
import Schema from 'redux-form-sync-validation'

export const schema = Schema.object({
  key1: Schema.string().min(5).required(),
  key2: Schema.boolean().required().valid(true)
})

And then use the schema with the validate function provided.

// Form.jsx
// @flow
import  * as React from 'react'
import { reduxForm, Field } from 'redux-form'
import type { FormProps } from 'redux-form'
import { validate } from 'redux-form-sync-validation'

import { schema } from './model'

class Form extends React.Component<FormProps> {
  render() {
    const { handleSubmit } = this.props
    return (
      <form onSubmit={handleSubmit}>
        <label>Key 1:</label><Field name='key1' component='input' />
        <label>Key 2:</label><Field name='key2' component='input' type='checkbox'/>
        <button>Submit</button>
      </form>
    )
  }

}

export default reduxForm({
  form: 'FormName',
  validate: validate(schema),
})(Form)

Extensions

It may become necessary for you to add extra validations such as for postcodes or phone numbers. You can extend an existing schema with a new class. You can also make a new schema from the base Schema class obtained by importing the named import import Schema, { Schema as BaseSchema } from 'redux-form-sync-validation', not to be confused with the default export. You can then use the helpful extend function which takes the default export and then an object with a set of key value pairs to override on that object. These keys can be existing keys or new keys.

You can add new validation rules using the addRule method which takes a function of the form type ValidationRule<T> = (T, Options) => ValidationResult where type ValidationResult = { isSuccess: boolean, error: ErrorObject }. The StringSchema also has the helpful match method for matching against regex patterns.

import Schema, { extend, StringSchema } from 'redux-form-sync-validation'
import type { ValidationRule } from 'redux-form-sync-validation'
import { UK_PHONE_NUMBER_REGEX, PASSWORD_REGEX, isValidPostcode } from '../../constants'

class ExtendedStringSchema extends StringSchema {

  UKPhone(): this {
    return this.match(UK_PHONE_NUMBER_REGEX, 'invalid UK phone number')
  }

  postcode(): this {
    return this.addRule(value => ({
      isSuccess: !value || isValidPostcode(value),
      error: 'invalid postcode'
    }))
  }

  password(): this {
    return this.match(PASSWORD_REGEX, PASSWORD_MESSAGE)
  }
}

const newSchemaObject = extend(Schema, { string: ExtendedStringSchema })

export default newSchemaObject

This would then allow you to import your new schema object and use it as Schema.string().postcode().required()

Api

TODO