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' } }| Field | Holds |
|---|---|
type | The 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. |
source | Who said so: validation (a rule), native (the browser), server (a response) or submission (the form as a whole, never held in the snapshot). |
ruleId | The id the author gave the rule, so two minLength rules on one form can read differently. Only on a rule's issue. |
params | The numbers a sentence needs, under the names the platform uses: minLength, max, inputType. They are serializable. |
message | Text 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:
- On the engine, for every form:
createFormEngine({ formatIssue }). - On one rendered form, which replaces the engine's for that form only: the
formatIssueprop of the ReactFormRendererorFormProvider, or of the RadiantFormRenderer.
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:
| Issue | Text shown |
|---|---|
minLength, rule password-min, minLength: 8 | Use at least 8 characters for your password |
minLength, rule bio-min, minLength: 20 | Use at least 20 characters (falls back to the type key) |
required, rule email-required, at email | Email 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_ROOT | Something went wrong. Please try again. |
What gets formatted
- The shown error of every field, and each entry of its
alllist. - Warnings, with the same function. A warning never blocks.
- Submission failures, as issues of the form itself: the
pathisFORM_ROOTand thesourceissubmission.
Where to read the text
| Binding | The text | The raw issue, to word it yourself |
|---|---|---|
| React field | useFormField(config).error and .warning | useFormField(config).issue |
| React, anywhere | useFormContext().formatIssue(issue, { path }) | useFormField(path).issue, or the container snapshot |
| Radiant field | Rendered by rui-field through rui-form's formatError | The container snapshot |
| Radiant custom element | new 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.