-
Notifications
You must be signed in to change notification settings - Fork 10
RW-15004 [risk=low] validate js repacement #9291
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
- initial introduction of zod schema validation library. - created zod-validators util module which a handful of components have been converted to make use of. - converted components now have a componentx-valdation sister file which has the valdation logic moved into it. - zod is a very nice library with excellent Typescript type safety and flow. But it requires a passthru pattern for safer usage in form validation scenarious. See refineObject helper in zod-valdators.ts. - zod 3.x also lacks "pretty" field name functionality which we made use of in validate.js. So these are replaced with more explicit error message mechanics. - also did some unit test cleanup, mainly adding mocks for dependencies that were adding warning noise to test output.
- first wave of components swapped to use zod validation in place of validate.js. - zod validators are using refinedObject and refineFields helpers to avoid early-abort validation scenarios with the normal zod usage patterns. See zod-validators.ts - also fixed act() related warnings and masked async race conditions causing unit test flakiness in data-page.spec, data-access-requirements.spec, and egress-events-table.spec
…elper when used by demographics servey v2 component that passes null for some of the optional string fields.
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.
Looks good overall. I ran into a few unexpected results. Maybe we can jump on a call to talk them through.
newName: z | ||
.string() | ||
.trim() | ||
.min(1, "New name can't be blank") |
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.
{Object.keys(errors).map((key) => ( | ||
<li key={errors[key][0]}> | ||
{errors[key]} (Question {questionsIndex[key]}) | ||
{key}: {errors[key]} (Question {questionsIndex[key]}) |
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.
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.
removed
(fields, ctx) => { | ||
const noop = undefined; | ||
const needYearOfBirthMsg = | ||
'You must either fill in your year of birth or check the corresponding "Prefer not to answer" checkbox '; |
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.
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.
fixed with requiredNumber zod helper
refineFields(fields, ctx, { | ||
ethnicCategories: nonEmptyArray( | ||
z.string(), | ||
"Ethnic categories can't be blank" |
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.
), | ||
genderIdentities: nonEmptyArray( | ||
z.string(), | ||
"Gender identities can't be blank" |
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.
? requiredStringWithMaxLength(100, 'Other Specific Population') | ||
: noop, | ||
diseaseOfFocus: data.diseaseFocusedResearch | ||
? requiredStringWithMaxLength(80, 'Disease of Focus') |
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.
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.
data.disseminateResearchFindingList?.includes( | ||
DisseminateResearchEnum.OTHER | ||
) | ||
? requiredStringWithMaxLength( |
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.
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.
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.
When I have a large value in this field and it is checked, I get the error. If I uncheck the other dissemination box, the contents stay the same, I don't get the error, and I can submit.
I am in the process of seeing if submitting a workspace in this situation will break anything.
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.
The workspace creation errored out.
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.
defering this issue for now since it's existing logic outside of validation logic
'Other methods of disseminating research findings' | ||
) | ||
: noop, | ||
aianResearchType: askAboutAIAN |
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.
? z.nativeEnum(AIANResearchType, { required_error: "can't be blank" }) | ||
: noop, | ||
aianResearchDetails: askAboutAIAN | ||
? requiredStringWithMaxLength(1000) |
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.
ui/src/app/utils/zod-validators.ts
Outdated
const isHttp = url.protocol === 'http:' || url.protocol === 'https:'; | ||
const isLocalHost = | ||
url.hostname === 'localhost' || url.hostname === '127.0.0.1'; | ||
console.log( |
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 think that this might have been left from debugging.
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.
fixed
- fixed null issue with requiredNumber validator, minor cleanup.
PR checklist
[risk=no|low|moderate|severe]
in the PR title as outlined in CONTRIBUTING.md.