Skip to content

Commit

Permalink
Code changes to validate user time request field in instrument picker…
Browse files Browse the repository at this point in the history
… question
  • Loading branch information
Bhaswati1148 committed Jun 18, 2024
1 parent 3655099 commit f61f1b7
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions packages/validation/src/Questionary/instrumentPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,65 @@ export const instrumentPickerValidationSchema = (field: any) => {
const config = field.config;

interface ValidationSchema {
instrumentId: string | undefined;
instrumentId: string | null | undefined;
timeRequested: string | null | undefined;
}

let schema:
| Yup.ArraySchema<SchemaOf<ValidationSchema>, AnyObject>
| SchemaOf<ValidationSchema>;

schema = Yup.object().shape({
instrumentId: Yup.string(),
timeRequested: Yup.string(),
});

if (config.isMultipleSelect) {
if (config.required) {
schema = Yup.array().of(
Yup.object().shape({
instrumentId: Yup.string().required(),
schema = Yup.array().of(
Yup.object()
.shape({
instrumentId: Yup.string(),
timeRequested: Yup.string(),
})
);
.nullable()
);
if (config.required) {
schema = schema.required().min(1);
}
if (config.requestTime) {
schema = Yup.array()
.of(
Yup.object().shape({
instrumentId: Yup.string(),
timeRequested: Yup.string()
.required('Request time field is required')
.test('is-number?', 'Requested time is not valid', (value) => {
if (Number(value) <= 0 || isNaN(Number(value))) return false;
else return true;
}),
})
)
.required()
.min(1);
}
} else {
schema = Yup.object()
.shape({
instrumentId: Yup.string(),
timeRequested: Yup.string(),
})
.nullable();
if (config.required) {
schema = schema.required();
}
if (config.requestTime) {
schema = Yup.array().of(
Yup.object().shape({
schema = Yup.object()
.shape({
instrumentId: Yup.string(),
timeRequested: Yup.string()
.required('Requested time is required')
.required('Request time field is required')
.test('is-number?', 'Requested time is not valid', (value) => {
if (Number(value) < 0 || isNaN(Number(value))) return false;
if (Number(value) <= 0 || isNaN(Number(value))) return false;
else return true;
}),
})
);
}
} else {
if (config.required) {
schema = Yup.object().shape({
instrumentId: Yup.string().required(),
timeRequested: Yup.string(),
});
}
if (config.requestTime) {
schema = Yup.object().shape({
instrumentId: Yup.string(),
timeRequested: Yup.string()
.required('Requested time is required')
.test('is-number?', 'Requested time is not valid', (value) => {
if (Number(value) < 0 || isNaN(Number(value))) return false;
else return true;
}),
});
.nullable()
.required('Field is required');
}
}

Expand Down

0 comments on commit f61f1b7

Please sign in to comment.