Skip to main content

Validations

Validations let you define what makes a field valid or not, and how the user should be informed when it’s not. FormBuilder supports two kinds of validation:

  • Simple validation rules as strings, like required or email
  • Advanced object-based validations, for custom rules, messages, and cross-field logic

Simple validations (as strings)

This is the most concise way to validate a field. You just list built-in rule names:

{
"component": "text",
"label": "Email",
"validations": ["required", "email"]
}

If the value is missing or not an email, the field will show an error.

Available string validations

RuleDescription
requiredMust not be empty
date_formatMust be a valid date
only_intOnly integer numbers allowed
only_letterOnly letters allowed
only_int_priceInteger price format only
emailValid email address
required_emailRequired and must be a valid email
nissValid NISS (national ID)
ibanValid IBAN
required_tileAn item must be selected on the tile input
phone_numberValid phone number
required_phone_numberRequired and must be a valid phone number

You can combine several in a single array.


Advanced validations (as objects)

Use objects if you need:

  • Custom error messages
  • Comparisons against another field
  • Conditional validations
  • Regex patterns
  • Type coercion or derived values (like age)

Example: exact match

{
"value": "MyCompany",
"message": "The company name must be MyCompany",
"assertion": "equal"
}

Example: number comparison

{
"value": 17,
"message": "Minimum 18 years",
"assertion": "isAbove"
}

Using expectation instead of assertion

{
"value": 17,
"message": "Minimum 18 years",
"expectation": "to.be.above"
}

Compare to another field

{
"value": { "from": "age" },
"message": "You cannot have more experience than your age",
"expectation": "to.be.below"
}

Validate based on another property’s existence

{
"property": { "from": "age" },
"message": "You have to define your age first",
"assertion": "exist"
}

Regex-based validation

{
"regex": {
"pattern": "^[A-Z0-9]+$",
"flags": "i",
"must_match": true
},
"message": "Only uppercase letters and numbers allowed"
}
  • If must_match is true: value must match the regex
  • If false: value must not match the regex

Combining multiple conditions with operator

You can combine multiple object validations and control how they’re evaluated:

{
"assertion": "exists",
"message": "Name is required",
"operator": "or"
},
{
"assertion": "isEmpty",
"message": "Name is required",
"operator": "or"
}
warning

Currently, you cannot nest condition groups — everything is evaluated flatly.


propertyAs: Force a type or interpretation

Sometimes you want to compare a value as a number, or as an age, or as duration since a date. Use propertyAs to convert types at runtime.

Available coercion values

  • string
  • number
  • bool
  • age (years since date)
  • years, months, days
  • yearsFrom, monthsFrom, daysFrom

Example: compare derived age

{
"property": "date_of_birth",
"propertyAs": "age",
"value": 30,
"assertion": "isBelow",
"message": "The age must be below 30 years"
}

Example: duration from another date

{
"fromDate": "first_date",
"propertyAs": "yearsFrom",
"value": 17,
"assertion": "isAbove",
"message": "Min 17 years from above date"
}

Conditional validations

A validation can also be enabled only if a condition is met:

{
"assertion": "exists",
"message": "Name is required",
"conditions": [
{
"property": { "from": "name_is_required" },
"assertion": "isTrue"
}
]
}

This validation only runs if name_is_required is true.


Storybook examples

Simple Condition#

Condition Of Conditions#

Required Under Conditions#

Age Based Condition#


For assertion syntax, see Conditions