Skip to content

Commit

Permalink
Functions refactored for defensive coding
Browse files Browse the repository at this point in the history
  • Loading branch information
arodidev committed Nov 15, 2023
1 parent f20548e commit 5ebcbfc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
Empty file.
60 changes: 37 additions & 23 deletions packages/esm-form-render-app/src/form-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,49 @@ export const handleFormValidation = async (schema, configObject) => {
const errors = [];
const warnings = [];

if (schema) {
const parsedForm = typeof schema == 'string' ? JSON.parse(schema) : schema;

const asyncTasks = [];
if (!schema) {
throw new Error('Invalid argument: "schema" cannot be null or undefined. 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

0 comments on commit 5ebcbfc

Please sign in to comment.