Validation Rules
isRequired()
Tests if there is a value.
import { isRequired } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isRequired(),
message: 'This field is required',
},
]}
>
isNotEmptyString()
Tests if a string has not only spaces.
import { isNotEmptyString } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isNotEmptyString(),
message: 'This field can\'t be empty',
},
]}
>
isNotEmptyArray()
Tests if an array is not empty.
import { isNotEmptyArray } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isNotEmptyArray(),
message: 'This array can\'t be empty',
},
]}
>
isEmail()
Tests if a string is a valid email.
import { isEmail } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isEmail(),
message: 'This is not a valid email',
},
]}
>
isNumber()
Tests if the value is a number.
import { isNumber } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isNumber(),
message: 'This is not a number',
},
]}
>
isMinNumber(min)
Tests if the value is a number and above a min value.
import { isMinNumber } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isMinNumber(10),
message: 'Should be a number over 10',
},
]}
>
isMaxNumber(max)
Tests if the value is a number and under a max value.
import { isMaxNumber } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isMaxNumber(10),
message: 'Should be a number under 10',
},
]}
>
isInRangeNumber(min, max)
Tests if the value is a number and in a range between a min and a max.
import { isInRangeNumber } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isInRangeNumber(1, 5),
message: 'Should be a number between 1 and 5',
},
]}
>
isPercentage()
Tests if the value is a number between 0 and 100.
import { isInRangeNumber } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isPercentage(),
message: 'This is not a valid percentage',
},
]}
>
isLength(length)
Tests if a string or an array has a specific length.
import { isLength } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isLength(4),
message: 'This has not a length of 4',
},
]}
>
isMinLength(min)
Tests if a string or an array has a minimum length.
import { isMinLength } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isMinLength(4),
message: 'This should at least have a length of 4',
},
]}
>
isMaxLength(max)
Tests if a string or an array has a maximum length.
import { isMaxLength } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isMaxLength(4),
message: 'This should have a length of 4 or less',
},
]}
>
isPattern(pattern)
Tests if a string matches a REGEX pattern.
import { isMaxLength } from '@formiz/validations'
<MyField
name="fieldName"
validations={[
{
rule: isPattern('hello'),
message: 'Must contain "hello"',
},
{
rule: isPattern('^[a-z]*$'),
message: 'Must only contain lowercase letters',
},
]}
>