Skip to content

Validators

Validators provide contextual messages for errors in input widgets. Each validator validates a particular shape of data — string, number, boolean, array — and supports a set of rules tailored to that shape. You can also create custom validators for application-specific rules.

Add a validator property to any input widget. The example below requires a non-empty password:

import { gui } from '@golemui/gui-shared';
gui.inputs.textInput('user.password', {
validator: {
type: 'string',
required: true,
},
});

Validators stack — combine multiple rules and you’ll see one message per failing rule. The next example checks the password is between 8 and 20 characters long, and that it contains both letters and numbers:

import { gui } from '@golemui/gui-shared';
gui.inputs.textInput('user.password', {
validator: {
type: 'string',
required: true,
minLength: 8,
maxLength: 20,
pattern: '^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]+$',
},
});

By default, validators use Zod’s built-in messages. Override them by providing a messages property — a record where each key corresponds to a validation rule and the value is either a plain string or a translation config for i18n.

gui.inputs.textInput('user.password', {
validator: {
type: 'string',
required: true,
minLength: 8,
messages: {
required: 'Please enter your password',
minLength: 'Password must be at least 8 characters',
},
},
});
gui.inputs.textInput('user.password', {
validator: {
type: 'string',
required: true,
messages: {
required: {
key: 'errors.password.required',
default: 'Please enter your password',
},
},
},
});

A more complete example combining several rule types:

Each validator type supports a specific set of rules and message keys.

PropertyMessage keyDescription
(type check)invalidValue is not a string
requiredrequiredEmpty string when required: true
minLengthminLengthString length is below the minimum
maxLengthmaxLengthString length exceeds the maximum
patternpatternString does not match the regex pattern
formatformatString does not match the format (email, url, uuid, hostname, ipv4, ipv6, date, time, date-time, duration)
enumenumValue is not one of the allowed values
constconstValue does not match the exact value
PropertyMessage keyDescription
(type check)invalidValue is not a number
requiredrequiredEmpty string when required: true
minimumminimumValue is below the minimum
maximummaximumValue exceeds the maximum
exclusiveMinimumexclusiveMinimumValue is not greater than the exclusive minimum
exclusiveMaximumexclusiveMaximumValue is not less than the exclusive maximum
multipleOfmultipleOfValue is not a multiple of the specified number
enumenumValue is not one of the allowed values
constconstValue does not match the exact value
PropertyMessage keyDescription
(type check)invalidValue is not a boolean
requiredrequiredEmpty string when required: true
constconstValue does not match the expected boolean value
PropertyMessage keyDescription
(type check)invalidValue is not an array
requiredrequiredArray is empty when required: true
minItemsminItemsArray has fewer items than the minimum
maxItemsmaxItemsArray has more items than the maximum

The validateOn property in formConfig controls when field validation runs.

type ValidateOn =
| 'eager'
| 'change'
| 'blur'
| 'submit'
| ('change' | 'blur' | 'submit')[];

When not set, the default validateOn behaviour is 'eager'.

ModeTriggers when
'change'The user changes the field value
'blur'The user leaves the field
'submit'A submit event is emitted (all fields are touched first)
'eager'Any of the above happens (default)
gui.actions.submitButton({ label: 'Create User' });
const formConfig = { validateOn: 'change' };
export function FormPage() {
return (
<div>
<React.FormComponent
(...)
formConfig={formConfig}
/>
</div>
);
}