-
Notifications
You must be signed in to change notification settings - Fork 25
Add user friendly validation errors for required
and pattern
fields on integration modal
#5212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/components/fields/fieldUtils.ts
Outdated
}; | ||
} | ||
|
||
if (schema.properties[key].pattern) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting a type error for pattern
on this type which is apparently JSONSchema7Definition
. Does anyone happen to know what type this object actually is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSONSchema has some types where boolean
is an allowed variant. So you likely need to handle that case
name: "controlRoomUrl", | ||
}); | ||
await userEvent.type(controlRoomUrlInput, "https://invalid.control.room/"); | ||
expect(controlRoomUrlInput).toHaveValue("https://invalid.control.room/"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is failing here - no value is actually getting modified. Any ideas what's going on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect you need to add an await tick()
or await waitForEffect()
. For controlled components, I think there's some React involved in changing the value of the input in the DOM
Could you add a code reference? Where/what is mutating the schema?
There's some prior art for how people declaratively define custom error messages. You're correct though that the official JSON Schema spec doesn't have them The AJV project, for example supports an errorMessage prop with handling for the different kinds of errors (pattern, required, etc.): https://github.com/ajv-validator/ajv-errors We use a different JSON Schema validator that doesn't rely on Discussion on the JSON Schema project here: json-schema-org/json-schema-spec#1075 I'm find with the JS approach you're taking for now to solve the immediate UI/UX pain. We'll likely want to move to supporting custom messages in the JSON Schema in the future, but that will require additional work to determine approach |
Codecov Report
@@ Coverage Diff @@
## main #5212 +/- ##
==========================================
+ Coverage 61.27% 61.28% +0.01%
==========================================
Files 989 989
Lines 30746 30764 +18
Branches 5922 5928 +6
==========================================
+ Hits 18840 18855 +15
- Misses 11906 11909 +3
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
@@ -54,4 +56,32 @@ describe("ServiceEditorModal", () => { | |||
const dialogRoot = screen.getByRole("dialog"); | |||
expect(dialogRoot).toMatchSnapshot(); | |||
}); | |||
|
|||
// eslint-disable-next-line jest/no-disabled-tests -- FIXME: for some reason, userEvent.type (the alternative approach of using fireEvent) is not modifying the value of the textarea | |||
test.skip("displays user-friendly pattern validation message", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ticket for this here: #5237
Spent several hours trying to debug this 🤷♀️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had some questions and code style suggestions. Depends on the answer to the questions as to whether they are blockers or not. Overall makes sense though! Sorry the test never worked out properly.
src/components/fields/fieldUtils.ts
Outdated
): Record<string, Record<string, string>> => { | ||
const errMessages: Record<string, Record<string, string>> = {}; | ||
|
||
if (schema) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do an early-return here instead for code readability, to avoid nesting the entire function one level:
if (!schema) {
return errMessages;
}
src/components/fields/fieldUtils.ts
Outdated
errMessages[key] = { | ||
// eslint-disable-next-line security/detect-object-injection | ||
...(errMessages[key] ? {} : errMessages[key]), | ||
required: `${key} is required`, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal if you like this syntax, but spreading props like this with a ternary inside is hard to parse at a glance for me. Maybe something like this would be more clear?
const messages = errMessages[key] ? {};
if (...) {
messages.required = ...
}
if (...) {
messages.pattern = ...
}
// Lodash _.isEmpty()
if (!isEmpty(messages)) {
errMessages[key] = messages;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this suggestion made the function look miles better. Thanks!
When the PR is merged, the first loom link found on this PR will be posted to |
What does this PR do?
Reviewer Tips
buildYup
has anerrMessages
options you can pass toconfig
to define custom error messages per field. The approach I've taken here is to walk the schema & add generic validation messages for properties withrequired
andpattern
fieldsDiscussion
errMessages
field to the schema itself which get collected viagetValidationErrMessages
(which would allow for more specific validation messages). I originally shied away from doing this, because it wouldn't match the JSONSchema7 type. However, it looks like we're mutating the schema anyways by addingconfig
?Demo/Discussion
https://www.loom.com/share/2affe022ae9848cebdbf336a2276f997
Checklist