-
Notifications
You must be signed in to change notification settings - Fork 234
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
(refactor) O3-4221: Replace validation with zod #1413
(refactor) O3-4221: Replace validation with zod #1413
Conversation
@denniskigen can you please review the changes |
package.json
Outdated
@@ -27,7 +27,7 @@ | |||
"devDependencies": { | |||
"@babel/core": "^7.11.6", | |||
"@carbon/react": "^1.71.0", | |||
"@openmrs/esm-framework": "next", | |||
"@openmrs/esm-framework": "^6.0.1-pre.2553", |
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.
Please revert this back to next. Read here - https://o3-docs.openmrs.org/docs/frontend-modules/development#i-see-errors-about-missing-apis-when-i-run-the-app on instructions on how to update the core and framework, and importantly the step about checking out package.json
when doing so.
package.json
Outdated
@@ -65,7 +65,7 @@ | |||
"jest-cli": "^29.7.0", | |||
"jest-environment-jsdom": "^29.7.0", | |||
"lint-staged": "^15.2.1", | |||
"openmrs": "next", | |||
"openmrs": "^6.0.1-pre.2553", |
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.
Ditto
describe('QueueServiceForm', () => { | ||
beforeEach(() => { | ||
mockUseLayoutType.mockReturnValue('tablet'); | ||
jest.clearAllMocks(); |
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.
We don't need to do this, since this is jest does this as its set in the config by default-
clearMocks: true, |
jest.clearAllMocks(); |
jest.mock('@openmrs/esm-framework', () => { | ||
return { | ||
showSnackbar: jest.fn(), | ||
restBaseUrl: '/ws/rest/v1', | ||
useLayoutType: jest.fn(), | ||
|
||
useExtensionSlot: jest.requireActual('@openmrs/esm-framework').useExtensionSlot, | ||
|
||
importDynamic: jest.fn(), | ||
}; | ||
}); | ||
|
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.
See this PR about partial mocks - openmrs/openmrs-esm-patient-chart#1933. But I'm confused as to why these require mocking for this test case, like the restBaseUrl?
jest.mock('@openmrs/esm-framework', () => { | |
return { | |
showSnackbar: jest.fn(), | |
restBaseUrl: '/ws/rest/v1', | |
useLayoutType: jest.fn(), | |
useExtensionSlot: jest.requireActual('@openmrs/esm-framework').useExtensionSlot, | |
importDynamic: jest.fn(), | |
}; | |
}); | |
jest.mock('@openmrs/esm-framework', () => ({ | |
...jest.requireActual('@openmrs/esm-framework'), | |
showSnackbar: jest.fn(), | |
restBaseUrl: '/ws/rest/v1', | |
useLayoutType: jest.fn(), | |
importDynamic: jest.fn(), | |
})); |
jest.mock('react-i18next', () => ({ | ||
useTranslation: () => ({ | ||
t: (key: string, defaultValue: string) => defaultValue, | ||
}), | ||
})); | ||
|
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.
Why do we need to mock this?
@@ -1,5 +1,9 @@ | |||
import React, { useCallback, useState } from 'react'; | |||
import { useTranslation } from 'react-i18next'; | |||
import { useForm, Controller } from 'react-hook-form'; | |||
import * as z from 'zod'; |
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.
import * as z from 'zod'; | |
import { z } from 'zod'; |
const QueueServiceSchema = z.object({ | ||
queueName: z.string().min(1, { message: 'Queue name is required' }), | ||
queueConcept: z.string().min(1, { message: 'Queue concept is required' }), | ||
userLocation: z.string().min(1, { message: 'Queue location 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.
This suggestion would require moving this inside the component definition itself so that it could use the t
function.
@denniskigen looking at the other places where we've used zod in patient-management, we've done this in different ways, so I don't know if what I've suggested is the best way to go about this.
const QueueServiceSchema = z.object({ | |
queueName: z.string().min(1, { message: 'Queue name is required' }), | |
queueConcept: z.string().min(1, { message: 'Queue concept is required' }), | |
userLocation: z.string().min(1, { message: 'Queue location is required' }), | |
}); | |
const QueueServiceSchema = z.object({ | |
queueName: z.string({ | |
required_error: t('queueNameRequired', 'Queue name is required'), | |
}), | |
queueConcept: z.string({ | |
required_error: t('queueConceptRequired', 'Queue concept is required'), | |
}), | |
userLocation: z.string({ | |
required_error: t('queueLocationRequired', 'Queue location is required'), | |
}), | |
}); |
(error) => { | ||
const createQueue = (data: QueueServiceFormData) => { | ||
saveQueue(data.queueName, data.queueConcept, data.queueName, data.userLocation).then( | ||
({ status }) => { |
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.
.then
and showing the snackbar for success a .catch
to catch the error afterwards should be sufficient, rather than checking for the status and doing an error inside the .then
Refactor(validation): O3-4221 Replace form validation with Zod
Requirements
Summary
This PR replaces the existing form validation approach with Zod, a TypeScript-first schema validation library. The key improvements include:
Key changes:
Screenshots
N/A - No UI changes, purely refactoring validation logic
Related Issue
https://issues.openmrs.org/browse/O3-4221
Other