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

Functions refactored for defensive coding #1682

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const config = {
'react-markdown': '<rootDir>/__mocks__/react-markdown.tsx',
'^dexie$': require.resolve('dexie'),
},
testEnvironment: 'jsdom',
};

module.exports = config;
11 changes: 11 additions & 0 deletions packages/esm-form-render-app/src/form-validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @jest-environment jsdom */

import { handleFormValidation } from './form-validator';

describe('handleFormValidation', () => {
it('should fail when no schema is passed to the function', async () => {
await expect(() => handleFormValidation(null, null)).rejects.toThrow(
'Invalid argument: "schema" cannot be null, undefined or an empty object. Please provide a valid object.',
);
});
});
67 changes: 45 additions & 22 deletions packages/esm-form-render-app/src/form-validator.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,62 @@
import { openmrsFetch } from '@openmrs/esm-framework';
import { OHRIFormSchema } from '@openmrs/openmrs-form-engine-lib';
import { async } from 'rxjs';

export const handleFormValidation = async (schema, configObject) => {
const errors = [];
const warnings = [];

if (schema) {
const parsedForm = typeof schema == 'string' ? JSON.parse(schema) : schema;
if (!schema || !Object.keys(schema)?.length) {
throw new Error(
'Invalid argument: "schema" cannot be null, undefined or an empty object. Please provide a valid object.',
);
}

const asyncTasks = [];
if (!configObject || !Object.keys(configObject)?.length) {
throw new Error(
'Invalid argument: "configObject" cannot be null, undefined or an empty object. Please provide a valid object.',
);
}

parsedForm.pages?.forEach((page) =>
page.sections?.forEach((section) =>
section.questions?.forEach((question) => {
asyncTasks.push(
handleQuestionValidation(question, errors, configObject, warnings),
handleAnswerValidation(question, errors, warnings),
const parsedForm = typeof schema == 'string' ? JSON.parse(schema) : schema;

const asyncTasks = [];

parsedForm.pages?.forEach((page) =>
page.sections?.forEach((section) =>
section.questions?.forEach((question) => {
asyncTasks.push(
handleQuestionValidation(question, errors, configObject, warnings),
handleAnswerValidation(question, errors, warnings),
);
question.type === 'obsGroup' &&
question.questions?.forEach((obsGrpQuestion) =>
asyncTasks.push(
handleQuestionValidation(obsGrpQuestion, errors, configObject, warnings),
handleAnswerValidation(question, errors, warnings),
),
);
question.type === 'obsGroup' &&
question.questions?.forEach((obsGrpQuestion) =>
asyncTasks.push(
handleQuestionValidation(obsGrpQuestion, errors, configObject, warnings),
handleAnswerValidation(question, errors, warnings),
),
);
}),
),
);
await Promise.all(asyncTasks);
}),
),
);
await Promise.all(asyncTasks);

return [errors, warnings];
}
return [errors, warnings];
};

const handleQuestionValidation = async (conceptObject, errorsArray, configObject, warningsArray) => {
if (!configObject || !Object.keys(configObject)?.length) {
throw new Error(
'Invalid argument: "configObject" cannot be null, undefined or an empty object. Please provide a valid object.',
);
}

if (!conceptObject || !Object.keys(conceptObject)?.length) {
throw new Error(
'Invalid argument: "conceptObject" cannot be null, undefined or an empty object. Please provide a valid object.',
);
}

const conceptRepresentation =
'custom:(uuid,display,datatype,answers,conceptMappings:(conceptReferenceTerm:(conceptSource:(name),code)))';

Expand Down