Redux Freeform

Redux Freeform

  • GitHub

›API Reference

Getting Started

  • Intro
  • Getting Started
  • createFormState
  • Validators
  • Constraints

API Reference

  • Validators

Validators

required

required validates on anything other than an empty string

import { required } from "redux-freeform";

const formConfig = {
  email: {
    validators: [required()]
  }
};
ValueValidates
""False
"foo"True

onlyIntegers

onlyIntegers will validate only on fields containing integers

import { onlyIntegers } from "redux-freeform";

const formConfig = {
  age: {
    validators: [onlyIntegers()]
  }
};
ValueValidates
""True
"123"True
"-123"True
"asdf"False
"as123"False

onlyNaturals

onlyNaturals will validate only on fields containing positive and zero integers

import { onlyNaturals } from "redux-freeform";

const formConfig = {
  age: {
    validators: [onlyNaturals()]
  }
};
ValueValidates
""True
"123"True
"-123"False
"asdf"False
"as123"False

numberLessThan

numberLessThan will validate any number less than the one provided

import { numberLessThan } from "redux-freeform";

const formConfig = {
  age: {
    validators: [numberLessThan(11)]
  }
};

Arguments: numberLessThan(n)

  • n value must be numerically < than n
ValuenValidates
""anyTrue
"10"11True
"11"11False
"15"11False

numberLessThanOrEqualTo

numberLessThanOrEqualTo will validate any number less than or equal to the one provided

import { numberLessThanOrEqualTo } from "redux-freeform";

const formConfig = {
  age: {
    validators: [numberLessThanOrEqualTo(11)]
  }
};

Arguments: numberLessThanOrEqualTo(n)

  • n value must be numerically <= than n
ValuenValidates
""anyTrue
"10"11True
"11"11True
"15"11False

numberGreaterThan

numberGreaterThan will validate any number greater than the one provided

import { numberGreaterThan } from "redux-freeform";

const formConfig = {
  age: {
    validators: [numberGreaterThan(11)]
  }
};

Arguments: numberGreaterThan(n)

  • n value must be numerically < than n
ValuenValidates
""anyTrue
"10"11False
"11"11False
"15"11True

numberGreaterThanOrEqualTo

numberGreaterThanOrEqualTo will validate any number greater than the one provided

import { numberGreaterThanOrEqualTo } from "redux-freeform";

const formConfig = {
  age: {
    validators: [numberGreaterThanOrEqualTo(11)]
  }
};

Arguments: numberGreaterThanOrEqualTo(n)

  • n value must be numerically <= than n
ValuenValidates
""anyTrue
"10"11False
"11"11True
"15"11True

hasLength

hasLength will validate for any string of the given length

import { hasLength } from "redux-freeform";

const formConfig = {
  code: {
    validators: [hasLength(4, 6)]
  }
};

Arguments: hasLength(min, max)

  • min value must be >= min
  • max value must be <= max

you can specify exactly one length like so:

hasLength(4, 4)

ValueminmaxValidates
""04True
""14False
"abc"44False
"abcd"44True
"abc"24True
"abcdef"13False

matchesRegex

matchesRegex validates if this fields value is equivalent to the regex argument passed in the matchesRegex function

import { matchesRegex } from "redux-freeform";

const formConfig = {
  email: {
    validators: [matchesRegex("^[^s@]+@[^s@]+.[^s@]+$")]
  }
};

Arguments: matchesRegex(regexValue)

  • regexValue the regex value must be a string, excluding the first and last slash /
  • empty string always validates, keeping with validator convention (only required rejects an empty string)
regexValueValueValidates
"^hey.*joe$"""True
"^hey.*joe$""hey joe"True
"^hey.*joe$""hey joe!"False
"^hey.*joe$""hey how are you joe"True

isRoutingNumber

isRoutingNumber validates if this fields value is 9 digits long and has a valid checksum

import { isRoutingNumber } from "redux-freeform";

const formConfig = {
  email: {
    validators: [isRoutingNumber()]
  }
};

Note: checksum based on http://www.brainjar.com/js/validation/ and assumes value contains no letters or special characters

ValueValidates
"122105155"True
"122105156"False
"000000000"False

matchesField

matchesField validates if this fields value is equivalent to another given fields value

import { matchesField } from "redux-freeform";

const formConfig = {
  password: {
    validators: [hasLength(4, 6)]
  },
  confirmPassword: {
    validators: [matchesField("password")]
  }
};

Arguments: matchesField(fieldName)

  • fieldName the string name of another key in the form object

Note: this causes the field to essentially "inherit" the validators of the matching field, so you do not need to specify them on each one unless you want the errors to populate in both field states

ValueOther Field ValueValidates
"foo""foo"True
""""True
"foo""bar"False

validateWhen

validateWhen is a higher-order validator that runs a dependentValidator iff a precondition has been met. This precondition is called the primaryValidator and can optionally depend on another field. The error key resulting in a rejection will correspond to the dependentValidator. If the primaryValidator rejects, no error key will be added to the errors array.

import { required, onlyIntegers, validateWhen, numberGreaterThan } from "redux-freeform";

const formConfig = {
  age: {
    validators: [required(), onlyIntegers()]
  },
  favoriteDrink: {
    validators: [validateWhen(required(), numberGreaterThan(20), "age")]
  }
};

Arguments: validateWhen(dependentValidator, primaryValidator fieldName?)

  • dependentValidator validator to run if primaryValidator passes
  • primaryValidator validator that runs as precondition for dependentValidator (read validators section for more)
  • fieldName the string name of another key in the form object, determines what field primaryValidator runs against. If omitted, primaryValidator will run against the same field as dependentValidator (the field who's validators array when belongs to)
value (favoriteDrink)Other Field ValueValidates
""""True
"""20"True
"""21"False
"Manhattan""21"True

validateSum

validateSum is a higher-order validator that runs a validator on the sum of values in the current field a set of identified fields. The error key resulting in a rejection will correspond to the validator. If the validator rejects, no error key will be added to the errors array.

import { required, onlyNaturals, validateSum, numberLessThan } from "redux-freeform";

const formConfig = {
  numberOfCats: {
    validators: [required(), validateSum(numberLessThan(5), "numberOfDogs")],
    constraints: [onlyNaturals()]
  },
  numberOfDogs: {
    validators: [required(), validateSum(numberLessThan(5), "numberOfCats")],
    constraints: [onlyNaturals()]
  }
};

Arguments: validateSum(validator, fieldNamesArray)

  • validator validator to run on the sum
  • fieldNamesArray the array of string names of keys in the form object, determines what field validator runs against
value (numberOfCats)Other Field Value (numberOfDogs)Validates
""""True
"1"""True
"1""1"True
"4""1"False
"1""4"False
"5""5"False

hasNumber

hasNumber validates if this field contains a minimum of one integer

import { hasNumber } from "redux-freeform";

const formConfig = {
  password: {
    validators: [hasNumber()]
  }
};
ValueValidates
""True
"password"False
"Pa$$word"False
"Pa$$word123"True
"PASSWORD1!"True

hasLowercaseLetter

hasLowercaseLetter validates if this field contains a minimum of one lowercase letter

import { hasLowercaseLetter } from "redux-freeform";

const formConfig = {
  password: {
    validators: [hasLowercaseLetter()]
  }
};
ValueValidates
""True
"123!@#"False
"Pa$$word"True
"Pa$$word123"True
"PASSWORD1!"False

hasUppercaseLetter

hasUppercaseLetter validates if this field contains a minimum of one uppercase letter

import { hasUppercaseLetter } from "redux-freeform";

const formConfig = {
  password: {
    validators: [hasUppercaseLetter()]
  }
};
ValueValidates
""True
"123!@#"False
"password"False
"Pa$$word123"True
"PASSWORD1!"True

hasSpecialCharacter

hasSpecialCharacter validates if this field contains a minimum of one special character ("!@#$%^&*.?")

import { hasSpecialCharacter } from "redux-freeform";

const formConfig = {
  password: {
    validators: [hasSpecialCharacter()]
  }
};
ValueValidates
""True
"123!@#"True
"password"False
"Pa$$word123"True
"PASSWORD1"False
"PASSWORD2!"True

isProbablyEmail

isProbablyEmail validates if this field is most likely an email.

import { isProbablyEmail } from "redux-freeform";

const formConfig = {
  email: {
    validators: [isProbablyEmail()]
  }
};
ValueValidates
""True
"testEmail@email.com"True
"testEmail@.com"False
"@testEmail.com"False
"testEmail"False
"testEmail.com"False

includedIn

includedIn will compare the value of a field with a supplied array of values. If the value of the field is included in the array of values, the value is considered valid. If the supplied array is not actually an array, the validator will perform a direct equivalence comparison instead.

import { required, validateWhen, includedIn } from "redux-freeform";

const formConfig = {
  checkNumber: {
    validators: [
      validateWhen(
        required(),
        includedIn(['money_order', 'cashiers_check']),
        'check_type'
      )
    ]
  },
  favoriteDrink: {
    validators: [includedIn(["coca-cola", "pepsi"])]
  }
};

Arguments: includedIn(allowedValues)

  • allowedValues the array of values that are considered valid
value (favoriteDrink)Allowed ValuesValidates
"coca-cola"["coca-cola", "pepsi"]True
"pepsi"["coca-cola", "pepsi"]True
"rc-cola["coca-cola", "pepsi"]False
Last updated on 2/5/2025
← Constraints
  • required
  • onlyIntegers
  • onlyNaturals
  • numberLessThan
  • numberLessThanOrEqualTo
  • numberGreaterThan
  • numberGreaterThanOrEqualTo
  • hasLength
  • matchesRegex
  • isRoutingNumber
  • matchesField
  • validateWhen
  • validateSum
  • hasNumber
  • hasLowercaseLetter
  • hasUppercaseLetter
  • hasSpecialCharacter
  • isProbablyEmail
  • includedIn
Made with 💖 by Citybase.