Skip to content

Commit

Permalink
refactore: schema attributes data type
Browse files Browse the repository at this point in the history
Signed-off-by: karan <[email protected]>
  • Loading branch information
16-karan committed Oct 19, 2023
1 parent 0c28f64 commit b514b02
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/components/Issuance/Issuance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,26 @@ const IssueCred = () => {
return JSON.parse(selectedUsers);
};

const createAttributeValidationSchema = (dataType: string) => {
let attributeSchema;

if (dataType === 'string') {
attributeSchema = Yup.string().typeError('Value must be a string');
} else if (dataType === 'number') {
attributeSchema = Yup.number().typeError('Value must be a number');
} else if (dataType === 'date') {
attributeSchema = Yup.date().typeError('Value must be a valid date');
} else {
attributeSchema = Yup.mixed();
}
return Yup.object().shape({
value: attributeSchema,
});
};

const validationSchema = Yup.object().shape({
attributes: Yup.array().of(
Yup.object().shape({
value: Yup.mixed().test(
'matchDataType',
'Value must match the specified data type',
function (value) {
const dataType = this.parent.dataType;
if (dataType === 'string') {
return typeof value === 'string';
} else if (dataType === 'number') {
return typeof value === 'number';
} else if (dataType === 'date') {
return !isNaN(Date.parse(value as string));
}
return true;
},
),
}),
Yup.lazy(({ dataType }) => createAttributeValidationSchema(dataType))
),
});

Expand Down

0 comments on commit b514b02

Please sign in to comment.