From b514b02d4b0ce1d914bc0fabc8c2edfca1237d94 Mon Sep 17 00:00:00 2001 From: karan Date: Thu, 19 Oct 2023 19:47:27 +0530 Subject: [PATCH] refactore: schema attributes data type Signed-off-by: karan --- src/components/Issuance/Issuance.tsx | 35 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/components/Issuance/Issuance.tsx b/src/components/Issuance/Issuance.tsx index 7529dcebf..ae7f6491f 100644 --- a/src/components/Issuance/Issuance.tsx +++ b/src/components/Issuance/Issuance.tsx @@ -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)) ), });