Skip to content

Commit

Permalink
Merge pull request #828 from Provincie-Zuid-Holland/feature/AB#16754
Browse files Browse the repository at this point in the history
Feature/ab#16754
  • Loading branch information
Stefwint authored Jun 25, 2024
2 parents 522b31c + 7770a15 commit d1ea964
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,11 +86,10 @@ const ObjectPersonModal = ({ model }: ObjectPersonModalProps) => {
onSubmit={handleFormSubmit}
initialValues={initialValues}
validationSchema={toFormikValidationSchema(
SCHEMA_GENERAL_INFORMATION
generateSchema(staticData)
)}
validateOnChange
enableReinitialize>
{({ isValid, isSubmitting }) => (
{({ isValid, isSubmitting, dirty }) => (
<Form>
<div className="space-y-4">
{staticData.map(item => {
Expand Down Expand Up @@ -147,7 +146,7 @@ const ObjectPersonModal = ({ model }: ObjectPersonModalProps) => {
<Button
variant="cta"
type="submit"
isDisabled={!isValid || isSubmitting}
isDisabled={!isValid || isSubmitting || !dirty}
isLoading={isSubmitting}>
Opslaan
</Button>
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dynamicObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
58 changes: 40 additions & 18 deletions src/validation/objectGeneralInformation.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import { object } from 'zod'

import { schemaDefaults } from '@/validation/zodSchema'

export const SCHEMA_GENERAL_INFORMATION = object({
Client_1_UUID: schemaDefaults.optionalString,
Owner_1_UUID: schemaDefaults.requiredString(),
Owner_2_UUID: schemaDefaults.optionalString,
Portfolio_Holder_1_UUID: schemaDefaults.optionalString,
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
) => {
if (Owner_1_UUID === Owner_2_UUID) {
} = 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',
message:
Expand All @@ -34,7 +50,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:
Expand All @@ -49,5 +69,7 @@ export const SCHEMA_GENERAL_INFORMATION = object({
path: ['Portfolio_Holder_2_UUID'],
})
}
}
)
})
}

export { generateSchema }
20 changes: 19 additions & 1 deletion src/validation/zodSchema.ts
Original file line number Diff line number Diff line change
@@ -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.') =>
Expand Down Expand Up @@ -63,3 +70,14 @@ const customRteValidation = () =>
export type Validation = {
[K in keyof typeof schemaDefaults]?: (typeof schemaDefaults)[K]
}

export function zodAlwaysRefine<T extends ZodTypeAny>(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
}

0 comments on commit d1ea964

Please sign in to comment.