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

RN-1165: Add validation handler for file questions #5416

Merged
merged 6 commits into from
Feb 25, 2024
Merged
Changes from 3 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
20 changes: 12 additions & 8 deletions packages/datatrak-web/src/features/Survey/useValidationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
import { useCallback } from 'react';
import * as yup from 'yup';
import { QuestionType } from '@tupaia/types';
import { getAllSurveyComponents, useSurveyForm } from '.';
import { SurveyScreenComponent } from '../../types';
import { getAllSurveyComponents, useSurveyForm } from '.';

const transformNumberValue = (value: string | number, originalValue: string | number) => {
// This is a workaround for yup not handling empty number fields (https://github.com/jquense/yup/issues/298)
return originalValue === '' || isNaN(originalValue as number) ? null : value;
};

const definedObjectShapeSchema = objShape => yup.object().shape(objShape).nullable().default(null); // Allow this value to be empty to stop a typeError. The mandatory validation will handle this instead

const getBaseSchema = (type: QuestionType) => {
switch (type) {
case QuestionType.Number:
return yup.number().transform(transformNumberValue).nullable();
case QuestionType.Autocomplete:
return yup
.object()
.shape({
value: yup.string(),
})
.nullable()
.default(null); // Allow this value to be empty to stop a typeError. The mandatory validation will handle this instead
return definedObjectShapeSchema({
value: yup.string(),
});
case QuestionType.File:
return definedObjectShapeSchema({
value: yup.string(),
name: yup.string(),
});
case QuestionType.Date:
case QuestionType.SubmissionDate:
case QuestionType.DateOfData:
Expand Down
Loading