Skip to content

Commit 802ae08

Browse files
committed
fix: implement custom refine for impactOnFunction from Tom
1 parent 0fc4099 commit 802ae08

File tree

1 file changed

+26
-14
lines changed
  • packages/instrument-library/src/forms/patient-health-questionnaire-9

1 file changed

+26
-14
lines changed

packages/instrument-library/src/forms/patient-health-questionnaire-9/index.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,30 @@ export default defineInstrument({
221221
value: ({ questions }) => sum(Object.values(omit(questions, 'impactOnFunctioning')))
222222
}
223223
},
224-
validationSchema: z.object({
225-
questions: z.object({
226-
interestPleasure: $Response,
227-
feelingDown: $Response,
228-
sleepIssues: $Response,
229-
energyLevel: $Response,
230-
appetiteChanges: $Response,
231-
selfWorth: $Response,
232-
concentrationIssues: $Response,
233-
psychomotorChanges: $Response,
234-
suicidalThoughts: $Response
235-
}),
236-
impactOnFunctioning: $Response.optional()
237-
})
224+
validationSchema: z
225+
.object({
226+
questions: z.object({
227+
interestPleasure: $Response,
228+
feelingDown: $Response,
229+
sleepIssues: $Response,
230+
energyLevel: $Response,
231+
appetiteChanges: $Response,
232+
selfWorth: $Response,
233+
concentrationIssues: $Response,
234+
psychomotorChanges: $Response,
235+
suicidalThoughts: $Response
236+
}),
237+
impactOnFunctioning: $Response.optional()
238+
})
239+
.superRefine(({ impactOnFunctioning, questions }, ctx) => {
240+
const isAnyNonZero = sum(Object.values(questions)) > 0;
241+
// If any response is not zero, then impactOnFunctioning is required
242+
if (isAnyNonZero && impactOnFunctioning === undefined) {
243+
ctx.addIssue({
244+
code: 'custom',
245+
message: 'This question is required / Cette question est obligatoire',
246+
path: ['impactOnFunctioning']
247+
});
248+
}
249+
})
238250
});

0 commit comments

Comments
 (0)