Skip to content

Validators

Validators are specialized actions that run automatically when a field's value changes. They populate field.errors and update field.valid.

All built-in validators are available on the Validators namespace:

typescript
import { Validators } from '@dynamicforms/vue-forms';

Pass validators to Field.create() via the validators array, or register them later with registerAction().

Validators.Validator(validationFn)

Base class for custom validators. Extend it or instantiate it directly for one-off rules.

typescript
import { Validators, ValidationErrorRenderContent } from '@dynamicforms/vue-forms';

const myValidator = new Validators.Validator(async (newValue, oldValue, field) => {
  if (newValue === 'forbidden') {
    return [new ValidationErrorRenderContent('This value is not allowed')];
  }
  return null; // no errors
});

validationFn signature:

typescript
(newValue: T, oldValue: T, field: IField<T>) => ValidationError[] | null | Promise<ValidationError[] | null>

Return null or [] to indicate no errors. The validator is eager — it runs once immediately at field creation with the initial value.

Built-in validators

Validators.Required(message?)

Fails when the value is empty (zero-length string, empty array, empty object, or null/undefined).

typescript
Field.create({ value: '', validators: [new Validators.Required('This field is required')] })
ParameterTypeDefault
messageRenderContentRef'Please enter a value'

Validators.Pattern(pattern, message?)

Fails when the string representation of the value does not match pattern.

typescript
new Validators.Pattern(/^\d{4}$/, 'Must be a 4-digit number')
ParameterTypeDefault
patternRegExprequired
messageRenderContentRef'Value must match pattern "{pattern}"'

Validators.MinValue(minValue, message?)

Fails when value < minValue.

ParameterTypeDefault
minValueTrequired
messageRenderContentRef'Value must be larger or equal to {minValue}'

Validators.MaxValue(maxValue, message?)

Fails when value > maxValue.

ParameterTypeDefault
maxValueTrequired
messageRenderContentRef'Value must be less than or equal to {maxValue}'

Validators.ValueInRange(minValue, maxValue, message?)

Fails when value < minValue or value > maxValue.

typescript
new Validators.ValueInRange(0, 100, 'Must be between 0 and 100')
ParameterTypeDefault
minValueTrequired
maxValueTrequired
messageRenderContentRef'Value must be between {minValue} and {maxValue}'

Validators.MinLength(minLength, message?)

Fails when the length of the value is less than minLength. Supports strings, arrays, and plain objects.

ParameterTypeDefault
minLengthnumberrequired
messageRenderContentRef'Length must be larger or equal to {minLength}'

Validators.MaxLength(maxLength, message?)

Fails when the length of the value exceeds maxLength.

ParameterTypeDefault
maxLengthnumberrequired
messageRenderContentRef'Length must be less than or equal to {maxLength}'

Validators.LengthInRange(minLength, maxLength, message?)

Fails when the length of the value is outside [minLength, maxLength].

typescript
new Validators.LengthInRange(10, 200, 'Must be between 10 and 200 characters')
ParameterTypeDefault
minLengthnumberrequired
maxLengthnumberrequired
messageRenderContentRef'Length must be between {minLength} and {maxLength}'

Validators.InAllowedValues(allowedValues, message?)

Fails when the value is not in allowedValues.

typescript
new Validators.InAllowedValues(['admin', 'user', 'guest'])
ParameterTypeDefault
allowedValuesT[]required
messageRenderContentRef'Must be one of [{allowedAsText}]'

Validators.CompareTo(otherField, isValidComparison, message)

Cross-field validator that re-validates whenever this field or otherField changes.

typescript
new Validators.CompareTo(
  passwordField,
  (myValue, otherValue) => myValue === otherValue,
  'Passwords must match'
)
ParameterTypeDescription
otherFieldIFieldThe field to compare against
isValidComparison(myValue: T, otherValue: T) => booleanReturn true when valid
messageRenderContentRefError message

Error types

ValidationError

Base class. Renders as an empty HTML comment. Override componentName, componentBindings, and componentBody for custom rendering.

ValidationErrorText

typescript
new ValidationErrorText('Something went wrong', /* optional CSS classes */)

Renders as plain text. Accessible via .text and .classes.

ValidationErrorRenderContent

typescript
new ValidationErrorRenderContent(message, /* optional CSS classes */)

Accepts string, MdString (for markdown), or a SimpleComponentDef object. The consuming UI component reads componentName, componentBindings, and componentBody to render it.

MdString

typescript
import { MdString } from '@dynamicforms/vue-forms';
new MdString('**bold** error message')

Wraps a string to signal that it should be rendered as markdown.

Message placeholders

All built-in error messages support {placeholder} substitution. The available placeholders depend on the validator:

PlaceholderAvailable in
{newValue}all
{oldValue}all
{field}all
{pattern}Pattern
{minValue}MinValue, ValueInRange
{maxValue}MaxValue, ValueInRange
{minLength}MinLength, LengthInRange
{maxLength}MaxLength, LengthInRange
{allowedValues}InAllowedValues
{allowedAsText}InAllowedValues
{otherField}CompareTo

See also: Validators example

Released under the MIT License.