Issue text

The engine never writes text. Every issue it produces is data: what went wrong, where it came from, which rule and with which numbers. There is no English inside the engine and no default message. Turning an issue into words, in the user's language, is the application's job.

The issue

{ type: 'minLength', source: 'validation', ruleId: 'password-min', params: { minLength: 8 } }
{ type: 'typeMismatch', source: 'native', params: { inputType: 'email' }, message: 'Please include an "@"' }
{ type: 'taken', source: 'server', message: 'This email is already registered' }
{ type: 'actionFailed', source: 'submission', params: { actionId: 'save' } }
FieldHolds
typeThe problem, for a program: a rule type, a ValidityState flag of a native control, the server's code or the name of a submission failure.
sourceWho said so: validation (a rule), native (the browser), server (a response) or submission (the form as a whole, never held in the snapshot).
ruleIdThe id the author gave the rule, so two minLength rules on one form can read differently. Only on a rule's issue.
paramsThe numbers a sentence needs, under the names the platform uses: minLength, max, inputType. They are serializable.
messageText that someone wrote: the rule's author, the server or the browser. It is a fallback, never the first choice. The schema library's own text is never used.

A field's error is the issue to show plus every issue of that path in all. See Form state.

One formatter, one override

A formatter is a FormIssueFormatter: (issue, { path }) => string. There are two places to put one:

  1. On the engine, for every form: createFormEngine({ formatIssue }).
  2. On one rendered form, which replaces the engine's for that form only: the formatIssue prop of the React FormRenderer or FormProvider, or of the Radiant FormRenderer.

The text of an issue is the form's override, else the engine's formatter, else issue.message, else nothing. A formatter that returns an empty string falls back to message too. The rule type is never shown, so a field can be invalid with no text: a widget marks itself invalid from invalid, never from the presence of text.

In React, keep the formatter's identity stable: define it at module level or memoize it, because a new function on each render rebuilds the form state context.

A complete formatter

This one looks a translation up by the most specific key first (the rule's ruleId, then the problem's type), fills the numbers from params and falls back to the issue's own message. Text a server already wrote for people is shown as it is. Swap t for your i18n library.

import { createFormEngine } from '@schema-engine/forms';
import type { FormIssueFormatter } from '@schema-engine/forms/state-container';
import { textInput } from './engine';

export const catalogue: Record<string, string> = {
	'form.fields.email': 'Email',
	'form.rules.password-min': 'Use at least {minLength} characters for your password',
	'form.validation.minLength': 'Use at least {minLength} characters',
	'form.validation.required': '{field} is required',
	'form.native.typeMismatch': 'Enter a valid {inputType}',
	'form.native.badInput': 'Enter a number',
	'form.submission.actionFailed': 'Something went wrong. Please try again.',
};

/** The first catalogue entry among `keys`, with `{name}` filled from `values`. */
function t(keys: string[], values: Record<string, unknown> = {}): string | undefined {
	const key = keys.find((candidate) => candidate in catalogue);
	return (
		key &&
		catalogue[key]!.replace(/\{(\w+)\}/g, (_, name: string) => {
			const value = values[name];
			return typeof value === 'string' || typeof value === 'number' ? String(value) : '';
		})
	);
}

export const formatIssue: FormIssueFormatter = (issue, { path }) => {
	if (issue.source === 'server' && issue.message) return issue.message;

	const keys = [
		'ruleId' in issue && issue.ruleId ? `form.rules.${issue.ruleId}` : undefined,
		`form.${issue.source}.${issue.type}`,
	].filter((key): key is string => key !== undefined);

	return t(keys, { ...issue.params, field: t([`form.fields.${path}`]) ?? path }) ?? issue.message ?? '';
};

export const engine = createFormEngine({ elements: [textInput], formatIssue });

The catalogue and the engine's formatter give these texts:

IssueText shown
minLength, rule password-min, minLength: 8Use at least 8 characters for your password
minLength, rule bio-min, minLength: 20Use at least 20 characters (falls back to the type key)
required, rule email-required, at emailEmail is required
typeMismatch, native, inputType: 'email'Enter a valid email
taken, server, message: 'This email is already registered'This email is already registered
actionFailed, submission, at FORM_ROOTSomething went wrong. Please try again.

What gets formatted

  • The shown error of every field, and each entry of its all list.
  • Warnings, with the same function. A warning never blocks.
  • Submission failures, as issues of the form itself: the path is FORM_ROOT and the source is submission.

Where to read the text

BindingThe textThe raw issue, to word it yourself
React fielduseFormField(config).error and .warninguseFormField(config).issue
React, anywhereuseFormContext().formatIssue(issue, { path })useFormField(path).issue, or the container snapshot
Radiant fieldRendered by rui-field through rui-form's formatErrorThe container snapshot
Radiant custom elementnew FormController(this).formatIssue(issue, { path })controller.select((snapshot) => snapshot.errors[path])

useFormContext().formState.errors holds the texts, flat by path. In an action, context.errors holds the same issues by path, each with its all list.