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

(feat) O3-3061: Support for conditional answered validation #297

Merged
merged 9 commits into from
Jun 4, 2024

Conversation

arodidev
Copy link
Contributor

@arodidev arodidev commented May 28, 2024

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR aims to introduce conditional answered validation to the form's engine.

Screenshots

Screen.Recording.2024-05-30.at.19.59.17.mov

Related Issue

https://openmrs.atlassian.net/browse/O3-3061

Other

@arodidev arodidev marked this pull request as draft May 28, 2024 10:40
@arodidev arodidev marked this pull request as ready for review May 30, 2024 17:19
@arodidev
Copy link
Contributor Author

Opened this work for review purposes pending tests.

@arodidev arodidev changed the title (WIP) Conditional answered validation (feat) O3-3061: Support for conditional answered validation May 30, 2024
if (field.meta?.submission) {
setErrors((prevErrors) => [...prevErrors, ...(field.meta.submission.errors || [])]);
setWarnings((prevWarnings) => [...prevWarnings, ...(field.meta.submission.warnings || [])]);
if (field.meta?.submission?.errors) {
Copy link
Member

Choose a reason for hiding this comment

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

The meta object is initialised at a earlier stage so it's not necessary to optionally chain it.

@@ -37,6 +37,16 @@ export function isInlineView(
return renderingType == 'single-line';
}

export function evaluateConditionalAnswered(field: FormField, flattenedFields: FormField[]) {
Copy link
Member

Choose a reason for hiding this comment

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

allFields reads better

const { referenceQuestionId, referenceQuestionAnswers, values, fields, message } = config;

const referencedField = fields.find((field) => field.id === referenceQuestionId);
const referencedFieldValue = values[referencedField.id] || referencedField.meta?.submission?.newValue?.value;
Copy link
Member

Choose a reason for hiding this comment

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

The second part of the expressions presupposes the underlying field to be capturing an obs. I think referencing the values object should suffice.

const referencedField = fields.find((field) => field.id === referenceQuestionId);
const referencedFieldValue = values[referencedField.id] || referencedField.meta?.submission?.newValue?.value;

if (!referencedFieldValue || !referenceQuestionAnswers.includes(referencedFieldValue)) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we being doing something like:

if (!isEmpty(value) && !referenceQuestionAnswers.includes(referencedFieldValue)) {
    return [{ resultType: 'error', errCode: 'invalid.valueSelected', message: message }];
}

@arodidev arodidev requested a review from samuelmale June 4, 2024 05:42
Copy link
Contributor

@CynthiaKamau CynthiaKamau left a comment

Choose a reason for hiding this comment

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

Thanks @arodidev , left a comment

@@ -156,6 +157,52 @@ describe('Form engine component', () => {
});
});

describe('conditional answered validation', () => {
it('should fail if target field has a value but no value is selected on the referenced field', () => {});
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@arodidev arodidev merged commit f09bef5 into openmrs:main Jun 4, 2024
4 checks passed
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.

3 participants