Skip to content

Commit 9db41cd

Browse files
author
Ruben van Leeuwen
committed
1904: Dont add zod validator for uncontrolled elements
1 parent e93b7e8 commit 9db41cd

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

frontend/packages/pydantic-forms/src/core/hooks/useGetZodValidator.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { useForm } from 'react-hook-form';
1212

1313
import { ZodRawShape, ZodTypeAny, z } from 'zod';
1414

15-
import { getClientSideValidationRule } from '@/core/helper';
15+
import {
16+
getClientSideValidationRule,
17+
getPydanticFormComponents,
18+
} from '@/core/helper';
1619
import {
1720
CustomValidationRule,
1821
Properties,
@@ -47,7 +50,7 @@ const getZodRule = (
4750
const arrayItem = pydanticFormField.arrayItem;
4851
const arrayItemRule = arrayItem
4952
? getZodRule(
50-
arrayItem,
53+
pydanticFormField,
5154
rhf,
5255
customValidationRule,
5356
customComponentMatcher,
@@ -94,11 +97,18 @@ const getZodValidationObject = (
9497
customValidationRule?: CustomValidationRule,
9598
customComponentMatcher?: PydanticFormsContextConfig['componentMatcher'],
9699
) => {
97-
const pydanticFormFields = Object.values(properties);
98-
if (!pydanticFormFields) return z.unknown();
100+
const pydanticFormComponents = getPydanticFormComponents(
101+
properties,
102+
customComponentMatcher,
103+
);
104+
if (!pydanticFormComponents) return z.unknown();
99105

100106
const validationObject: ZodRawShape = {};
101-
pydanticFormFields.forEach((pydanticFormField) => {
107+
pydanticFormComponents.forEach((component) => {
108+
const { Element, pydanticFormField } = component;
109+
110+
if (!pydanticFormField || !Element.isControlledElement) return;
111+
102112
const id =
103113
pydanticFormField.id.split('.').pop() || pydanticFormField.id;
104114

frontend/packages/pydantic-forms/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export type FormRenderer = React.JSXElementConstructor<{
317317
pydanticFormComponents: PydanticFormComponents;
318318
}>;
319319

320-
interface PydanticFormComponent {
320+
export interface PydanticFormComponent {
321321
Element: ElementMatch;
322322
pydanticFormField: PydanticFormField;
323323
}

0 commit comments

Comments
 (0)