From 1b6c33e04713230616268de2250a7c7321104dcb Mon Sep 17 00:00:00 2001 From: Stef Winterswijk Date: Tue, 18 Jun 2024 13:52:49 +0200 Subject: [PATCH 1/3] Don't show error when field is empty --- src/validation/objectGeneralInformation.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/validation/objectGeneralInformation.ts b/src/validation/objectGeneralInformation.ts index e30492d0..33bb79fc 100644 --- a/src/validation/objectGeneralInformation.ts +++ b/src/validation/objectGeneralInformation.ts @@ -18,7 +18,7 @@ export const SCHEMA_GENERAL_INFORMATION = object({ }, ctx ) => { - if (Owner_1_UUID === Owner_2_UUID) { + if (!!Owner_1_UUID && !!Owner_2_UUID && Owner_1_UUID === Owner_2_UUID) { ctx.addIssue({ code: 'custom', message: @@ -34,7 +34,11 @@ export const SCHEMA_GENERAL_INFORMATION = object({ }) } - if (Portfolio_Holder_1_UUID === Portfolio_Holder_2_UUID) { + if ( + !!Portfolio_Holder_1_UUID && + !!Portfolio_Holder_2_UUID && + Portfolio_Holder_1_UUID === Portfolio_Holder_2_UUID + ) { ctx.addIssue({ code: 'custom', message: From 9b2539658735903e24fe92fca1fe046c561f52bc Mon Sep 17 00:00:00 2001 From: Stef Winterswijk Date: Tue, 18 Jun 2024 14:16:41 +0200 Subject: [PATCH 2/3] Update --- .../ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx | 4 ++-- src/utils/dynamicObject.tsx | 4 ++-- src/validation/objectGeneralInformation.ts | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx b/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx index 6c6c706d..707db52f 100644 --- a/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx +++ b/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx @@ -90,7 +90,7 @@ const ObjectPersonModal = ({ model }: ObjectPersonModalProps) => { )} validateOnChange enableReinitialize> - {({ isValid, isSubmitting }) => ( + {({ isValid, isSubmitting, dirty }) => (
{staticData.map(item => { @@ -147,7 +147,7 @@ const ObjectPersonModal = ({ model }: ObjectPersonModalProps) => { diff --git a/src/utils/dynamicObject.tsx b/src/utils/dynamicObject.tsx index 48d1557e..bb165403 100644 --- a/src/utils/dynamicObject.tsx +++ b/src/utils/dynamicObject.tsx @@ -50,12 +50,12 @@ export const getStaticDataPropertyRequired = ( key: keyof ModelPatchStaticType ) => { switch (key) { - case 'Client_1_UUID': case 'Owner_2_UUID': - case 'Portfolio_Holder_1_UUID': case 'Portfolio_Holder_2_UUID': return false case 'Owner_1_UUID': + case 'Client_1_UUID': + case 'Portfolio_Holder_1_UUID': return true default: throw new Error( diff --git a/src/validation/objectGeneralInformation.ts b/src/validation/objectGeneralInformation.ts index 33bb79fc..96a2b8bc 100644 --- a/src/validation/objectGeneralInformation.ts +++ b/src/validation/objectGeneralInformation.ts @@ -3,10 +3,10 @@ import { object } from 'zod' import { schemaDefaults } from '@/validation/zodSchema' export const SCHEMA_GENERAL_INFORMATION = object({ - Client_1_UUID: schemaDefaults.optionalString, + Client_1_UUID: schemaDefaults.requiredString(), Owner_1_UUID: schemaDefaults.requiredString(), Owner_2_UUID: schemaDefaults.optionalString, - Portfolio_Holder_1_UUID: schemaDefaults.optionalString, + Portfolio_Holder_1_UUID: schemaDefaults.requiredString(), Portfolio_Holder_2_UUID: schemaDefaults.optionalString, }).superRefine( ( From 7770a158a4f7f6eda960044f227e795993388fbd Mon Sep 17 00:00:00 2001 From: Stef Winterswijk Date: Tue, 18 Jun 2024 14:52:22 +0200 Subject: [PATCH 3/3] Update AB#16754 --- .../ObjectPersonModal/ObjectPersonModal.tsx | 5 +- src/validation/objectGeneralInformation.ts | 50 +++++++++++++------ src/validation/zodSchema.ts | 20 +++++++- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx b/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx index 707db52f..de6a7cd6 100644 --- a/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx +++ b/src/components/Modals/ObjectModals/ObjectPersonModal/ObjectPersonModal.tsx @@ -16,7 +16,7 @@ import { getStaticDataPropertyKey, getStaticDataPropertyRequired, } from '@/utils/dynamicObject' -import { SCHEMA_GENERAL_INFORMATION } from '@/validation/objectGeneralInformation' +import { generateSchema } from '@/validation/objectGeneralInformation' interface ObjectPersonModalProps { model: Model @@ -86,9 +86,8 @@ const ObjectPersonModal = ({ model }: ObjectPersonModalProps) => { onSubmit={handleFormSubmit} initialValues={initialValues} validationSchema={toFormikValidationSchema( - SCHEMA_GENERAL_INFORMATION + generateSchema(staticData) )} - validateOnChange enableReinitialize> {({ isValid, isSubmitting, dirty }) => ( diff --git a/src/validation/objectGeneralInformation.ts b/src/validation/objectGeneralInformation.ts index 96a2b8bc..221cf4d0 100644 --- a/src/validation/objectGeneralInformation.ts +++ b/src/validation/objectGeneralInformation.ts @@ -1,23 +1,39 @@ import { object } from 'zod' -import { schemaDefaults } from '@/validation/zodSchema' - -export const SCHEMA_GENERAL_INFORMATION = object({ - Client_1_UUID: schemaDefaults.requiredString(), - Owner_1_UUID: schemaDefaults.requiredString(), - Owner_2_UUID: schemaDefaults.optionalString, - Portfolio_Holder_1_UUID: schemaDefaults.requiredString(), - Portfolio_Holder_2_UUID: schemaDefaults.optionalString, -}).superRefine( - ( - { +import { ModelPatchStaticType } from '@/config/objects/types' +import { getStaticDataPropertyRequired } from '@/utils/dynamicObject' +import { schemaDefaults, zodAlwaysRefine } from '@/validation/zodSchema' + +/** + * Create SCHEMA based on provided values + */ +const generateSchema = (values: (keyof ModelPatchStaticType)[]) => { + let schema = object({}) + + values.forEach(val => { + const required = getStaticDataPropertyRequired(val) + + return (schema = schema.extend({ + [val]: required + ? schemaDefaults.requiredString() + : schemaDefaults.optionalString, + })) + }) + + return zodAlwaysRefine(schema).superRefine((data, ctx) => { + // Cast data to a type with optional properties to handle the case where they might not exist + const { Owner_1_UUID, Owner_2_UUID, Portfolio_Holder_1_UUID, Portfolio_Holder_2_UUID, - }, - ctx - ) => { + } = data as { + Owner_1_UUID?: string + Owner_2_UUID?: string + Portfolio_Holder_1_UUID?: string + Portfolio_Holder_2_UUID?: string + } + if (!!Owner_1_UUID && !!Owner_2_UUID && Owner_1_UUID === Owner_2_UUID) { ctx.addIssue({ code: 'custom', @@ -53,5 +69,7 @@ export const SCHEMA_GENERAL_INFORMATION = object({ path: ['Portfolio_Holder_2_UUID'], }) } - } -) + }) +} + +export { generateSchema } diff --git a/src/validation/zodSchema.ts b/src/validation/zodSchema.ts index df9c538a..39a799db 100644 --- a/src/validation/zodSchema.ts +++ b/src/validation/zodSchema.ts @@ -1,4 +1,11 @@ -import { custom, instanceof as instanceOf, number, string } from 'zod' +import { + ZodTypeAny, + any, + custom, + instanceof as instanceOf, + number, + string, +} from 'zod' export const schemaDefaults = { requiredString: (msg = 'Dit veld is verplicht.') => @@ -63,3 +70,14 @@ const customRteValidation = () => export type Validation = { [K in keyof typeof schemaDefaults]?: (typeof schemaDefaults)[K] } + +export function zodAlwaysRefine(zodType: T) { + return any().superRefine(async (value, ctx) => { + const res = await zodType.safeParseAsync(value) + + if (res.success === false) + for (const issue of res.error.issues) { + ctx.addIssue(issue) + } + }) as unknown as T +}