Skip to content

feat: allow form-level default error messages via options.errorMessages - #268

Open
amds92 wants to merge 2 commits into
remoteoss:mainfrom
amds92:fix/custom-error-messages-jsf-config
Open

feat: allow form-level default error messages via options.errorMessages#268
amds92 wants to merge 2 commits into
remoteoss:mainfrom
amds92:fix/custom-error-messages-jsf-config

Conversation

@amds92

@amds92 amds92 commented Jul 30, 2026

Copy link
Copy Markdown

Summary

Closes #69.

createHeadlessForm() had no way to set a message for a validation type (e.g. required) that applies to every field at once. The only override mechanism was per-field x-jsf-errorMessage, which meant repeating the same string on every property of a schema — painful for i18n and for schemas with many fields.

This adds options.errorMessages, a dictionary from validation type (required, type, minLength, ...) to a message string, applied to any field that doesn't already define its own x-jsf-errorMessage for that validation type.

createHeadlessForm(schema, {
  errorMessages: {
    required: 'This field is required.',
    minLength: 'This value is too short.',
  },
})

Precedence (most specific wins): field's own x-jsf-errorMessage > options.errorMessages > built-in default message.

One behavior worth a second pair of eyes: checkboxes get a special built-in required message ("Please acknowledge this field") instead of the generic one. options.errorMessages.required overrides that too, since it's the same validation type and applyCustomErrorMessages() doesn't special-case checkboxes. I think that's the right call (the option is about the validation type, not about how the default happens to be computed), but flagging it explicitly in case the intended behavior is different.

Test plan

Added to test/errors/messages.test.ts:

  • Global message applied across fields of different input types
  • Field-level x-jsf-errorMessage still wins over the global default
  • Falls back to the built-in default when no override matches the validation type
  • Covers type, enum, minLength validation types (not just required)
  • Multiple validation types overridden at once
  • Checkbox required special case
  • Valid data produces no errors (no false positives)
  • Nested/sub-object fields are covered
  • Empty errorMessages: {} behaves like the option wasn't passed

pnpm test, pnpm lint, pnpm typecheck all pass locally.

amds92 added 2 commits July 30, 2026 15:27
createHeadlessForm() had no way to set a message for a validation type
(e.g. `required`) that applies to every field at once. The only override
mechanism was per-field `x-jsf-errorMessage`, which meant repeating the
same string on every property of a schema.

Add `options.errorMessages`, a dictionary from validation type to message,
applied to any field that doesn't define its own `x-jsf-errorMessage`.
Precedence: field's x-jsf-errorMessage > options.errorMessages > built-in
default.

Fixes remoteoss#69
Cover more validation types (type, enum, minLength), multiple overrides
at once, the checkbox required-message special case, valid data (no
false positives), nested fields, and an empty errorMessages object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Error Messages doesn't work in JSF Config

1 participant