Skip to content
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

Conversation

Muppasanipraneeth
Copy link
Contributor

Refactor(validation): O3-4221 Replace form validation with Zod

Requirements

  • PR title includes conventional commit label and ticket number
  • Conforms to OpenMRS 3.0 Styleguide and design documentation
  • Includes tests validating the new Zod-based validation

Summary

This PR replaces the existing form validation approach with Zod, a TypeScript-first schema validation library. The key improvements include:

  • More type-safe validation
  • Simplified validation logic
  • Better developer experience with inline schema definitions
  • Reduced boilerplate code for form validation

Key changes:

  • Introduced Zod schemas for form validation
  • Replaced manual validation with schema-based validation
  • Updated test cases to work with new validation approach

Screenshots

N/A - No UI changes, purely refactoring validation logic

Related Issue

https://issues.openmrs.org/browse/O3-4221

Other

  • Minimal impact on existing component behavior
  • Performance improvements through more efficient validation

@Muppasanipraneeth
Copy link
Contributor Author

@denniskigen can you please review the changes

@NethmiRodrigo NethmiRodrigo changed the title Refactor/o3 4221 replace validation with zod (refactor) O3-4221: Replace validation with zod Dec 16, 2024
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",
Copy link
Collaborator

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",
Copy link
Collaborator

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();
Copy link
Collaborator

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-

Suggested change
jest.clearAllMocks();

Comment on lines +41 to +52
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(),
};
});

Copy link
Collaborator

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?

Suggested change
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(),
}));

Comment on lines +7 to +12
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string, defaultValue: string) => defaultValue,
}),
}));

Copy link
Collaborator

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';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import * as z from 'zod';
import { z } from 'zod';

Comment on lines 25 to 29
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' }),
});
Copy link
Collaborator

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.

Suggested change
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 }) => {
Copy link
Collaborator

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

@Muppasanipraneeth Muppasanipraneeth deleted the refactor/O3-4221-replace-validation-with-zod branch December 17, 2024 03:15
@Muppasanipraneeth Muppasanipraneeth restored the refactor/O3-4221-replace-validation-with-zod branch December 17, 2024 03:16
@Muppasanipraneeth Muppasanipraneeth deleted the refactor/O3-4221-replace-validation-with-zod branch December 17, 2024 03:34
@Muppasanipraneeth Muppasanipraneeth restored the refactor/O3-4221-replace-validation-with-zod branch December 17, 2024 03:36
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.

2 participants