feat: allow form-level default error messages via options.errorMessages - #268
Open
amds92 wants to merge 2 commits into
Open
feat: allow form-level default error messages via options.errorMessages#268amds92 wants to merge 2 commits into
amds92 wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-fieldx-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 ownx-jsf-errorMessagefor that validation type.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
requiredmessage ("Please acknowledge this field") instead of the generic one.options.errorMessages.requiredoverrides that too, since it's the same validation type andapplyCustomErrorMessages()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:x-jsf-errorMessagestill wins over the global defaulttype,enum,minLengthvalidation types (not justrequired)requiredspecial caseerrorMessages: {}behaves like the option wasn't passedpnpm test,pnpm lint,pnpm typecheckall pass locally.