Skip to main content
Version: Next

Validations

Validation is an essential part of any form building. Whether you want to make sure the user has actually entered an email or a password is strong enough, you will want to implement both a per input validation or a full form validation using conditional validations. Formulaik takes the validation out of the components onto the inputs. The formulaik engine you use is responsible for implementing the validation. For instance the official React engine uses yup for validation.

Define validations​

Let's take the case of an email input:

{
component: 'input',
id: 'email',
label: 'Email',
type: "string",
params: {
placeholder: "email@domain.com"
},
validations: [
{
kind: "format",
value: "email",
message: 'Invalid email format',
},
{
kind: "required",
value: true,
message: "This field can't be blank",
},
],
}
  • We expect a string data type. This is important for the validation. By default it is set to string.
  • We have declared two validations
    • format: make sure it's an email
    • required: this field is required

Formulaik processes both validations in sequential order. It stops when the validation fails and displays the error message.

Validation kinds per data type​

Formulaik supports these validations per data type out of the box:

String​

KindConditionOptions
requiredText is requiredmessage?: string error message
lengthSet a required length for the string value. The ${value} interpolation can be used in the message argumentmessage?: string error message
value?: number desired length
minSet a minimum length limit for the string value. The ${value} interpolation can be used in the message argumentmessage?: string error message
value: number minimum desired length
maxSet a maximum length limit for the string value. The ${value} interpolation can be used in the message argumentmessage?: string error message
value: number maximum desired length
matchesProvide an arbitrary regex to match the value againstmessage?: string error message
value: string regex
emailValidates the value as an email address using the same regex as defined by the HTML spec.message?: string error message
WATCH OUT: Validating email addresses is nearly impossible with just code. Different clients and servers accept different things and many diverge from the various specs defining "valid" emails. The ONLY real way to validate an email address is to send a verification email to it and check that the user got it. With that in mind, yup picks a relatively simple regex that does not cover all cases, but aligns with browser input validation behavior since HTML forms are a common use case for yup. If you have more specific needs please override the email method with your own logic or regex
urlValidates the value as a valid URL via a regexmessage?: string error message
uuidValidates the value as a valid UUID via a regexmessage?: string error message
datetimeValidates the value as an ISO datetime via a regex. Defaults to UTC validation; timezone offsets are not permittedmessage?: string error message
allowOffset?: boolean Allow offset
precision?: string Precision
Unlike .date(), datetime will not convert the string to a Date object. datetime also provides greater customization over the required format of the datetime string than date does. options.allowOffset: Allow a time zone offset. False requires UTC 'Z' timezone. (default: false) options.precision: Require a certain sub-second precision on the date. (default: null -- any (or no) sub-second precision)

Number​

KindConditionOptions
minSet the minimum value allowed. The ${value} interpolation can be used in the message argumentmessage?: string error message
value: number minimum value
maxSet the maximum value allowed. The ${value} interpolation can be used in the message argumentmessage?: string error message
value: number maximum value
lessThanValue must be less than value. The ${less} interpolation can be used in the message argumentmessage?: string error message
value: number maximum value
moreThanValue must be strictly greater than min. The ${value} interpolation can be used in the message argumentmessage?: string error message
value: number minimum value
positiveValue must be a positive numbermessage?: string error message
negativeValue must be a negative numbermessage?: string error message
integerValidates that a number is an integermessage?: string error message

Boolean​

#TODO

Date​

KindConditionOptions
minSet the minimum date allowed. When a string is provided it will attempt to cast to a date first and use the result as the limitmessage?: string error message
value: number minimum value
maxSet the maximum value allowed. The ${value} interpolation can be used in the message argumentmessage?: string error message
value: number maximum value

Array​

KindConditionOptions
lengthSet a specific length requirement for the array. The ${value} interpolation can be used in the message argumentmessage?: string error message
value?: number desired length
minSet a minimum length limit for the array. The ${value} interpolation can be used in the message argument.message?: string error message
value: number minimum value
maxSet a maximum length limit for the array. The ${value} interpolation can be used in the message argument.message?: string error message
value: number maximum value