Skip to content

🎥 Make videos programmatically with React

Restore type safety in SchemaEditor by replacing `any` with `AnyZodSchema`

Jonny Burger38e97b21503f
diff --git a/packages/studio/src/api/save-default-props.ts b/packages/studio/src/api/save-default-props.ts
index c0736bbf2e..5f4e660283 100644
--- a/packages/studio/src/api/save-default-props.ts
+++ b/packages/studio/src/api/save-default-props.ts
@@ -1,6 +1,7 @@
import {getRemotionEnvironment} from 'remotion';
import type {ZodTypesType} from '../components/get-zod-if-possible';
import {extractEnumJsonPaths} from '../components/RenderModal/SchemaEditor/extract-enum-json-paths';
+import type {AnyZodSchema} from '../components/RenderModal/SchemaEditor/zod-schema-type';
import {callUpdateDefaultPropsApi} from '../components/RenderQueue/actions';
import type {UpdateDefaultPropsFunction} from './helpers/calc-new-props';
import {calcNewProps} from './helpers/calc-new-props';
@@ -48,7 +49,7 @@ export const saveDefaultProps = async ({
generatedDefaultProps,
composition.schema
? extractEnumJsonPaths({
- schema: composition.schema,
+ schema: composition.schema as AnyZodSchema,
zodRuntime: z,
currentPath: [],
zodTypes,
diff --git a/packages/studio/src/components/RenderModal/DataEditor.tsx b/packages/studio/src/components/RenderModal/DataEditor.tsx
index 688ef01ac5..57ccdad01e 100644
--- a/packages/studio/src/components/RenderModal/DataEditor.tsx
+++ b/packages/studio/src/components/RenderModal/DataEditor.tsx
@@ -29,7 +29,11 @@ import {
ZodNotInstalled,
} from './SchemaEditor/SchemaErrorMessages';
import {extractEnumJsonPaths} from './SchemaEditor/extract-enum-json-paths';
-import {getZodSchemaType} from './SchemaEditor/zod-schema-type';
+import type {
+ AnyZodSchema,
+ ZodSafeParseResult,
+} from './SchemaEditor/zod-schema-type';
+import {getZodSchemaType, zodSafeParse} from './SchemaEditor/zod-schema-type';
import {WarningIndicatorButton} from './WarningIndicatorButton';
import type {TypeCanSaveState} from './get-render-modal-warnings';
import {
@@ -48,8 +52,7 @@ export type State =
str: string;
value: Record<string, unknown>;
validJSON: true;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- zodValidation: {success: boolean; error?: any};
+ zodValidation: ZodSafeParseResult;
}
| {
str: string;
@@ -185,7 +188,7 @@ export const DataEditor: React.FC<{
);
}
- return unresolvedComposition.schema;
+ return unresolvedComposition.schema as AnyZodSchema;
}, [unresolvedComposition.schema, z]);
const zodValidationResult = useMemo(() => {
@@ -197,9 +200,7 @@ export const DataEditor: React.FC<{
return 'no-schema' as const;
}
- return (
- schema as {safeParse: (v: unknown) => {success: boolean; error?: unknown}}
- ).safeParse(defaultProps);
+ return zodSafeParse(schema, defaultProps);
}, [defaultProps, schema]);
const setShowWarning: React.Dispatch<React.SetStateAction<boolean>> =
diff --git a/packages/studio/src/components/RenderModal/RenderModalJSONPropsEditor.tsx b/packages/studio/src/components/RenderModal/RenderModalJSONPropsEditor.tsx
index 06cc8bcdae..90ed3e1e49 100644
--- a/packages/studio/src/components/RenderModal/RenderModalJSONPropsEditor.tsx
+++ b/packages/studio/src/components/RenderModal/RenderModalJSONPropsEditor.tsx
@@ -11,6 +11,8 @@ import {Flex, Row, Spacing} from '../layout';
import type {State} from './DataEditor';
import {ZodErrorMessages} from './SchemaEditor/ZodErrorMessages';
import {deepEqual} from './SchemaEditor/deep-equal';
+import type {AnyZodSchema} from './SchemaEditor/zod-schema-type';
+import {zodSafeParse} from './SchemaEditor/zod-schema-type';
const style: React.CSSProperties = {
fontFamily: 'monospace',
@@ -24,11 +26,10 @@ const scrollable: React.CSSProperties = {
flex: 1,
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const parseJSON = (str: string, schema: any): State => {
+const parseJSON = (str: string, schema: AnyZodSchema): State => {
try {
const value = NoReactInternals.deserializeJSONWithSpecialTypes(str);
- const zodValidation = schema.safeParse(value);
+ const zodValidation = zodSafeParse(schema, value);
return {str, value, validJSON: true, zodValidation};
} catch (e) {
return {str, validJSON: false, error: (e as Error).message};
@@ -44,8 +45,7 @@ export const RenderModalJSONPropsEditor: React.FC<{
readonly showSaveButton: boolean;
readonly serializedJSON: SerializedJSONWithCustomFields | null;
readonly defaultProps: Record<string, unknown>;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
}> = ({
setValue,
value,
@@ -78,7 +78,7 @@ export const RenderModalJSONPropsEditor: React.FC<{
const parsed = parseJSON(e.target.value, schema);
if (parsed.validJSON) {
- const validationResult = schema.safeParse(parsed.value);
+ const validationResult = zodSafeParse(schema, parsed.value);
setLocalValue({
str: e.target.value,
value: parsed.value,
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaEditor.tsx
index 04f13a00b1..f079c4613a 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaEditor.tsx
@@ -14,7 +14,8 @@ import {deepEqual} from './deep-equal';
import type {RevisionContextType} from './local-state';
import {RevisionContext} from './local-state';
import {defaultPropsEditorScrollableAreaRef} from './scroll-to-default-props-path';
-import {getZodSchemaType} from './zod-schema-type';
+import type {AnyZodSchema, ZodSafeParseResult} from './zod-schema-type';
+import {getZodSchemaType, zodSafeParse} from './zod-schema-type';
const scrollable: React.CSSProperties = {
display: 'flex',
@@ -22,15 +23,13 @@ const scrollable: React.CSSProperties = {
overflowY: 'auto',
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const SchemaEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly unsavedDefaultProps: Record<string, unknown>;
readonly setValue: React.Dispatch<
React.SetStateAction<Record<string, unknown>>
>;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly zodValidationResult: {success: boolean; error?: any};
+ readonly zodValidationResult: ZodSafeParseResult;
readonly savedDefaultProps: Record<string, unknown>;
readonly onSave: (
updater: (oldState: Record<string, unknown>) => Record<string, unknown>,
@@ -117,7 +116,7 @@ export const SchemaEditor: React.FC<{
}, [savedDefaultProps, setValue]);
if (!zodValidationResult.success) {
- const defaultPropsValid = schema.safeParse(savedDefaultProps);
+ const defaultPropsValid = zodSafeParse(schema, savedDefaultProps);
if (!defaultPropsValid.success) {
return <InvalidDefaultProps zodValidationResult={zodValidationResult} />;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
index 0fee096d7d..e4073fc70c 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
@@ -4,6 +4,7 @@ import {Button} from '../../Button';
import {inlineCodeSnippet} from '../../Menu/styles';
import {Spacing} from '../../layout';
import {ZodErrorMessages} from './ZodErrorMessages';
+import type {ZodSafeParseResult} from './zod-schema-type';
const explainer: React.CSSProperties = {
display: 'flex',
@@ -86,8 +87,7 @@ export const NoDefaultProps = () => {
};
export const InvalidDefaultProps: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- zodValidationResult: {success: boolean; error?: any};
+ zodValidationResult: ZodSafeParseResult;
}> = ({zodValidationResult}) => {
return (
<div style={errorContainer}>
@@ -112,8 +112,7 @@ export const InvalidDefaultProps: React.FC<{
};
export const InvalidSchema: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- zodValidationResult: {success: boolean; error?: any};
+ zodValidationResult: ZodSafeParseResult;
reset: () => void;
}> = ({zodValidationResult, reset}) => {
return (
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaSeparationLine.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaSeparationLine.tsx
index 97122827be..2cded5139e 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaSeparationLine.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaSeparationLine.tsx
@@ -8,6 +8,7 @@ import {
import {Spacing} from '../../layout';
import {fieldSetText} from '../layout';
import {createZodValues} from './create-zod-values';
+import type {AnyZodSchema} from './zod-schema-type';
import {getArrayElement} from './zod-schema-type';
export const VERTICAL_GUIDE_HEIGHT = 24;
@@ -35,8 +36,7 @@ export const SchemaArrayItemSeparationLine: React.FC<{
increment: boolean,
) => void;
readonly index: number;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly showAddButton: boolean;
readonly isLast: boolean;
}> = ({onChange, index, schema, isLast, showAddButton}) => {
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
index 38225a23ee..e8a72ed50e 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayEditor.tsx
@@ -13,12 +13,12 @@ import type {UpdaterFunction} from './ZodSwitch';
import {createZodValues} from './create-zod-values';
import {deepEqual} from './deep-equal';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import {getArrayElement} from './zod-schema-type';
import type {JSONPath} from './zod-types';
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodArrayEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: unknown[];
readonly defaultValue: unknown[];
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
index b9e5679faf..5a1dd5029a 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodArrayItemEditor.tsx
@@ -2,13 +2,13 @@ import {useCallback, useMemo} from 'react';
import {useZodIfPossible} from '../../get-zod-if-possible';
import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
export const ZodArrayItemEditor: React.FC<{
jsonPath: JSONPath;
onChange: UpdaterFunction<unknown[]>;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- elementSchema: any;
+ elementSchema: AnyZodSchema;
index: number;
value: unknown;
defaultValue: unknown;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx
index b516ff86bf..6e34555c8f 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodBooleanEditor.tsx
@@ -4,6 +4,7 @@ import {Fieldset} from './Fieldset';
import {SchemaLabel} from './SchemaLabel';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const fullWidth: React.CSSProperties = {
@@ -11,8 +12,7 @@ const fullWidth: React.CSSProperties = {
};
export const ZodBooleanEditor: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: boolean;
readonly setValue: UpdaterFunction<boolean>;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodColorEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
index 402b6be410..ec1c38af18 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodColorEditor.tsx
@@ -13,6 +13,7 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const fullWidth: React.CSSProperties = {
@@ -20,8 +21,7 @@ const fullWidth: React.CSSProperties = {
};
export const ZodColorEditor: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: string;
readonly defaultValue: string;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodDateEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodDateEditor.tsx
index 511d4432e5..d287f683a6 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodDateEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodDateEditor.tsx
@@ -7,6 +7,7 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const fullWidth: React.CSSProperties = {
@@ -49,8 +50,7 @@ const formatDate = (date: Date) => {
};
export const ZodDateEditor: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: Date;
readonly defaultValue: Date;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodDefaultEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodDefaultEditor.tsx
index 1bc893217c..5ae5ef2c86 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodDefaultEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodDefaultEditor.tsx
@@ -1,16 +1,16 @@
import React from 'react';
+import type {AnyZodSchema} from './zod-schema-type';
import {getInnerType} from './zod-schema-type';
import type {JSONPath} from './zod-types';
import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodDefaultEditor: React.FC<{
readonly showSaveButton: boolean;
readonly jsonPath: JSONPath;
readonly value: unknown;
readonly defaultValue: unknown;
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly setValue: UpdaterFunction<unknown>;
readonly onSave: UpdaterFunction<unknown>;
readonly onRemove: null | (() => void);
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodDiscriminatedUnionEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodDiscriminatedUnionEditor.tsx
index 2e2c744d0d..f6610fa804 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodDiscriminatedUnionEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodDiscriminatedUnionEditor.tsx
@@ -13,6 +13,7 @@ import {ZodObjectEditor} from './ZodObjectEditor';
import type {UpdaterFunction} from './ZodSwitch';
import {createZodValues} from './create-zod-values';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import {
getDiscriminatedOption,
getDiscriminatedOptionKeys,
@@ -21,7 +22,7 @@ import {
import type {JSONPath} from './zod-types';
export const ZodDiscriminatedUnionEditor: React.FC<{
- schema: any;
+ schema: AnyZodSchema;
setValue: UpdaterFunction<Record<string, unknown>>;
value: Record<string, unknown>;
defaultValue: Record<string, unknown>;
@@ -153,6 +154,10 @@ export const ZodDiscriminatedUnionEditor: React.FC<{
value[discriminator] as string,
);
+ if (!currentOptionSchema) {
+ throw new Error('No matching option found for discriminated union');
+ }
+
return (
<ZodObjectEditor
// Re-render the object editor when the discriminator changes
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
index a8834bc59c..24ad5bfc84 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodEffectEditor.tsx
@@ -4,6 +4,7 @@ import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import {getEffectsInner, getZodSchemaType} from './zod-schema-type';
import type {JSONPath} from './zod-types';
@@ -11,9 +12,8 @@ const fullWidth: React.CSSProperties = {
width: '100%',
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodEffectEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: unknown;
readonly setValue: UpdaterFunction<unknown>;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx
index eda8d85967..e830f53bbf 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodEnumEditor.tsx
@@ -7,6 +7,7 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import {getEnumValues} from './zod-schema-type';
import type {JSONPath} from './zod-types';
@@ -14,9 +15,8 @@ const container: React.CSSProperties = {
width: '100%',
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodEnumEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: string;
readonly defaultValue: string;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodErrorMessages.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodErrorMessages.tsx
index 920735158d..016d5a7e34 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodErrorMessages.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodErrorMessages.tsx
@@ -2,6 +2,7 @@ import React, {useMemo} from 'react';
import {FAIL_COLOR, LIGHT_TEXT} from '../../../helpers/colors';
import {WarningTriangle} from '../../NewComposition/ValidationMessage';
import {Spacing} from '../../layout';
+import type {ZodSafeParseResult} from './zod-schema-type';
const schemaLabel: React.CSSProperties = {
fontSize: 14,
@@ -24,8 +25,7 @@ const triangleStyle: React.CSSProperties = {
};
export const ZodErrorMessages: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly zodValidationResult: {success: boolean; error?: any};
+ readonly zodValidationResult: ZodSafeParseResult;
readonly viewTab: 'schema' | 'json';
}> = ({zodValidationResult, viewTab}) => {
if (zodValidationResult.success) {
@@ -46,37 +46,33 @@ export const ZodErrorMessages: React.FC<{
if (viewTab === 'json') {
return (
<div>
- {zodValidationResult.error.errors.map(
- (error: {path: (string | number)[]; message: string}) => {
- return (
- <div key={error.path.join('.')} style={style}>
- <WarningTriangle style={triangleStyle} />
- <Spacing x={1} />
- {error.path.length === 0 ? 'Root' : error.path.join('.')}:{' '}
- {error.message}
- </div>
- );
- },
- )}
+ {zodValidationResult.error.issues.map((error) => {
+ return (
+ <div key={error.path.join('.')} style={style}>
+ <WarningTriangle style={triangleStyle} />
+ <Spacing x={1} />
+ {error.path.length === 0 ? 'Root' : error.path.join('.')}:{' '}
+ {error.message}
+ </div>
+ );
+ })}
</div>
);
}
return (
<div>
- {zodValidationResult.error.errors.map(
- (error: {path: (string | number)[]; message: string}) => {
- return (
- <div key={error.path.join('.')} style={style}>
- -{' '}
- <code style={code}>
- {error.path.length === 0 ? 'Root' : error.path.join('.')}
- </code>
- : {error.message}
- </div>
- );
- },
- )}
+ {zodValidationResult.error.issues.map((error) => {
+ return (
+ <div key={error.path.join('.')} style={style}>
+ -{' '}
+ <code style={code}>
+ {error.path.length === 0 ? 'Root' : error.path.join('.')}
+ </code>
+ : {error.message}
+ </div>
+ );
+ })}
</div>
);
};
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodFieldValidation.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodFieldValidation.tsx
index 218d72423e..ad92d27852 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodFieldValidation.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodFieldValidation.tsx
@@ -22,7 +22,8 @@ export const ZodFieldValidation: React.FC<{
localValue: LocalState<unknown>;
path: JSONPath;
}> = ({localValue, path}) => {
- if (localValue.zodValidation.success) {
+ const {zodValidation} = localValue;
+ if (zodValidation.success) {
return null;
}
@@ -30,24 +31,22 @@ export const ZodFieldValidation: React.FC<{
<div style={legend}>
<ValidationMessage
align="flex-start"
- message={localValue.zodValidation.error.format()._errors[0]}
+ message={zodValidation.error.format()._errors[0]}
type="error"
/>
<Spacing x={0.5} />
<InfoBubble title="Zod validation failure">
<div style={stackTrace}>
<div style={stackTraceLabel}>Zod Validation has failed:</div>
- {localValue.zodValidation.error.errors.map(
- (error: {code: string; message: string}, index: number) => (
- // eslint-disable-next-line react/no-array-index-key
- <div key={index} style={stackTraceLabel}>
- Type: {error.code} <br />
- Message: {error.message}
- <br />
- Path: {path.join('.')}
- </div>
- ),
- )}
+ {zodValidation.error.issues.map((error, index: number) => (
+ // eslint-disable-next-line react/no-array-index-key
+ <div key={index} style={stackTraceLabel}>
+ Type: {error.code} <br />
+ Message: {error.message}
+ <br />
+ Path: {path.join('.')}
+ </div>
+ ))}
</div>
</InfoBubble>
<Spacing x={0.5} />
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodMatrixEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodMatrixEditor.tsx
index fb827e41cd..e69275c8d8 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodMatrixEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodMatrixEditor.tsx
@@ -13,6 +13,7 @@ import type {UpdaterFunction} from './ZodSwitch';
import {createZodValues} from './create-zod-values';
import {deepEqual} from './deep-equal';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import {getArrayElement} from './zod-schema-type';
import type {JSONPath} from './zod-types';
@@ -22,9 +23,8 @@ const rowStyle: React.CSSProperties = {
width: '100%',
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodMatrixEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: unknown[];
readonly defaultValue: unknown[];
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
index 9b3259fcb5..f35d456aac 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodNullableEditor.tsx
@@ -1,14 +1,15 @@
import {ZodOrNullishEditor} from './ZodOrNullishEditor';
import type {UpdaterFunction} from './ZodSwitch';
+import type {AnyZodSchema} from './zod-schema-type';
+import {getInnerType} from './zod-schema-type';
import type {JSONPath} from './zod-types';
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodNullableEditor: React.FC<{
showSaveButton: boolean;
jsonPath: JSONPath;
value: unknown;
defaultValue: unknown;
- schema: any;
+ schema: AnyZodSchema;
setValue: UpdaterFunction<unknown>;
onSave: UpdaterFunction<unknown>;
onRemove: null | (() => void);
@@ -28,8 +29,7 @@ export const ZodNullableEditor: React.FC<{
saveDisabledByParent,
mayPad,
}) => {
- // Both v3 and v4 have _def.innerType
- const {innerType} = schema._def;
+ const innerType = getInnerType(schema);
return (
<ZodOrNullishEditor
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
index 66c1c650a8..78c46c3d9b 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodNumberEditor.tsx
@@ -5,16 +5,16 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
-import {isZodV3Schema} from './zod-schema-type';
+import type {AnyZodSchema} from './zod-schema-type';
+import {getZodDef, isZodV3Schema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const fullWidth: React.CSSProperties = {
width: '100%',
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const getMinValue = (schema: any) => {
- const {checks} = schema._def;
+const getMinValue = (schema: AnyZodSchema) => {
+ const {checks} = getZodDef(schema);
if (!checks) return -Infinity;
if (isZodV3Schema(schema)) {
@@ -35,9 +35,8 @@ const getMinValue = (schema: any) => {
return -Infinity;
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const getMaxValue = (schema: any) => {
- const {checks} = schema._def;
+const getMaxValue = (schema: AnyZodSchema) => {
+ const {checks} = getZodDef(schema);
if (!checks) return Infinity;
if (isZodV3Schema(schema)) {
@@ -58,9 +57,8 @@ const getMaxValue = (schema: any) => {
return Infinity;
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const getStep = (schema: any): number | undefined => {
- const {checks} = schema._def;
+const getStep = (schema: AnyZodSchema): number | undefined => {
+ const {checks} = getZodDef(schema);
if (!checks) return undefined;
if (isZodV3Schema(schema)) {
@@ -83,9 +81,8 @@ const getStep = (schema: any): number | undefined => {
return undefined;
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodNumberEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: number;
readonly setValue: UpdaterFunction<number>;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
index e1b38af015..eddcdd4257 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodObjectEditor.tsx
@@ -8,7 +8,8 @@ import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
import {deepEqual} from './deep-equal';
import {useLocalState} from './local-state';
-import {getZodSchemaType, isZodV3Schema} from './zod-schema-type';
+import type {AnyZodSchema} from './zod-schema-type';
+import {getObjectShape, getZodSchemaType} from './zod-schema-type';
import type {JSONPath} from './zod-types';
export type ObjectDiscrimatedUnionReplacement = {
@@ -16,9 +17,8 @@ export type ObjectDiscrimatedUnionReplacement = {
markup: React.ReactNode;
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodObjectEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly unsavedValue: Record<string, unknown>;
readonly savedValue: Record<string, unknown>;
@@ -52,18 +52,12 @@ export const ZodObjectEditor: React.FC<{
savedValue,
});
- const def = schema._def;
-
const typeName = getZodSchemaType(schema);
if (typeName !== 'object') {
throw new Error('expected object');
}
- // v3: _def.shape() is a function, v4: _def.shape is an object
- const shape =
- isZodV3Schema(schema) && typeof def.shape === 'function'
- ? def.shape()
- : def.shape;
+ const shape = getObjectShape(schema);
const keys = Object.keys(shape);
const isRoot = jsonPath.length === 0;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
index 1d1ac4a3a9..92bbb1a78a 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodOptionalEditor.tsx
@@ -1,14 +1,15 @@
import {ZodOrNullishEditor} from './ZodOrNullishEditor';
import type {UpdaterFunction} from './ZodSwitch';
+import type {AnyZodSchema} from './zod-schema-type';
+import {getInnerType} from './zod-schema-type';
import type {JSONPath} from './zod-types';
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodOptionalEditor: React.FC<{
showSaveButton: boolean;
jsonPath: JSONPath;
value: unknown;
defaultValue: unknown;
- schema: any;
+ schema: AnyZodSchema;
setValue: UpdaterFunction<unknown>;
onSave: UpdaterFunction<unknown>;
onRemove: null | (() => void);
@@ -28,8 +29,7 @@ export const ZodOptionalEditor: React.FC<{
saveDisabledByParent,
mayPad,
}) => {
- // Both v3 and v4 have _def.innerType
- const {innerType} = schema._def;
+ const innerType = getInnerType(schema);
return (
<ZodOrNullishEditor
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
index 045dbbb6ab..df14fe275e 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodOrNullishEditor.tsx
@@ -12,6 +12,7 @@ import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
import {createZodValues} from './create-zod-values';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const labelStyle: React.CSSProperties = {
@@ -32,10 +33,8 @@ export const ZodOrNullishEditor: React.FC<{
jsonPath: JSONPath;
value: unknown;
defaultValue: unknown;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- schema: any;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- innerSchema: any;
+ schema: AnyZodSchema;
+ innerSchema: AnyZodSchema;
setValue: UpdaterFunction<unknown>;
onSave: UpdaterFunction<unknown>;
onRemove: null | (() => void);
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx
index 4ceffc6b23..cbc2d15bc8 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodStaticFileEditor.tsx
@@ -8,15 +8,15 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const container: React.CSSProperties = {
width: '100%',
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodStaticFileEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: string;
readonly defaultValue: string;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodStringEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
index a6eaa86492..0eff61d301 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodStringEditor.tsx
@@ -6,6 +6,7 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const fullWidth: React.CSSProperties = {
@@ -13,8 +14,7 @@ const fullWidth: React.CSSProperties = {
};
export const ZodStringEditor: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: string;
readonly defaultValue: string;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx
index b4f2ff03a8..e8f244cbfb 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -1,6 +1,11 @@
import React from 'react';
import {useZodTypesIfPossible} from '../../get-zod-if-possible';
-import {getZodSchemaDescription, getZodSchemaType} from './zod-schema-type';
+import type {AnyZodSchema} from './zod-schema-type';
+import {
+ getEffectsInner,
+ getZodSchemaDescription,
+ getZodSchemaType,
+} from './zod-schema-type';
import type {JSONPath} from './zod-types';
import {ZodArrayEditor} from './ZodArrayEditor';
import {ZodBooleanEditor} from './ZodBooleanEditor';
@@ -28,11 +33,8 @@ export type UpdaterFunction<T> = (
increment: boolean,
) => void;
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-type AnySchema = any;
-
export const ZodSwitch: React.FC<{
- readonly schema: AnySchema;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: unknown;
readonly defaultValue: unknown;
@@ -361,7 +363,7 @@ export const ZodSwitch: React.FC<{
setValue={setValue as UpdaterFunction<unknown[]>}
value={value as unknown[]}
jsonPath={jsonPath}
- schema={schema._def.schema}
+ schema={getEffectsInner(schema)}
defaultValue={defaultValue as unknown[]}
onSave={onSave as UpdaterFunction<unknown[]>}
showSaveButton={showSaveButton}
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodTextareaEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodTextareaEditor.tsx
index 94d9955248..b84487ba7c 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodTextareaEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodTextareaEditor.tsx
@@ -9,6 +9,7 @@ import {SchemaLabel} from './SchemaLabel';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {useLocalState} from './local-state';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
const fullWidth: React.CSSProperties = {
@@ -21,8 +22,7 @@ const textareaStyle: React.CSSProperties = {
};
export const ZodTextareaEditor: React.FC<{
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: string;
readonly defaultValue: string;
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleEditor.tsx
index 9891adffad..bcb5935d49 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleEditor.tsx
@@ -10,14 +10,15 @@ import {useLocalState} from './local-state';
import {SchemaLabel} from './SchemaLabel';
import {SchemaArrayItemSeparationLine} from './SchemaSeparationLine';
import {SchemaVerticalGuide} from './SchemaVerticalGuide';
+import type {AnyZodSchema} from './zod-schema-type';
+import {getTupleItems} from './zod-schema-type';
import type {JSONPath} from './zod-types';
import {ZodFieldValidation} from './ZodFieldValidation';
import type {UpdaterFunction} from './ZodSwitch';
import {ZodTupleItemEditor} from './ZodTupleItemEditor';
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodTupleEditor: React.FC<{
- readonly schema: any;
+ readonly schema: AnyZodSchema;
readonly jsonPath: JSONPath;
readonly value: unknown[];
readonly defaultValue: unknown[];
@@ -49,7 +50,7 @@ export const ZodTupleEditor: React.FC<{
});
const [expanded, setExpanded] = useState(true);
- const def = schema._def;
+ const tupleItems = getTupleItems(schema);
const suffix = useMemo(() => {
return expanded ? ' [' : ' [...] ';
@@ -100,12 +101,12 @@ export const ZodTupleEditor: React.FC<{
<ZodTupleItemEditor
onChange={onChange}
value={child}
- def={def}
+ tupleItems={tupleItems}
index={i}
jsonPath={jsonPath}
defaultValue={
defaultValue?.[i] ??
- createZodValues(def.items[i], z, zodTypes)
+ createZodValues(tupleItems[i], z, zodTypes)
}
onSave={onSave}
showSaveButton={showSaveButton}
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleItemEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleItemEditor.tsx
index 28310f0419..33c7d4fa58 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleItemEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodTupleItemEditor.tsx
@@ -2,13 +2,13 @@ import {useCallback, useMemo} from 'react';
import {useZodIfPossible} from '../../get-zod-if-possible';
import type {UpdaterFunction} from './ZodSwitch';
import {ZodSwitch} from './ZodSwitch';
+import type {AnyZodSchema} from './zod-schema-type';
import type {JSONPath} from './zod-types';
export const ZodTupleItemEditor: React.FC<{
jsonPath: JSONPath;
onChange: UpdaterFunction<unknown[]>;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- def: any;
+ tupleItems: AnyZodSchema[];
index: number;
value: unknown;
defaultValue: unknown;
@@ -18,7 +18,7 @@ export const ZodTupleItemEditor: React.FC<{
saveDisabledByParent: boolean;
mayPad: boolean;
}> = ({
- def,
+ tupleItems,
onChange,
jsonPath,
index,
@@ -71,7 +71,7 @@ export const ZodTupleItemEditor: React.FC<{
<div>
<ZodSwitch
jsonPath={newJsonPath}
- schema={def.items[index]}
+ schema={tupleItems[index]}
value={value}
setValue={setValue}
defaultValue={defaultValue}
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
index 66914b2dc1..5f1f09d430 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodUnionEditor.tsx
@@ -1,11 +1,11 @@
import {ZonNonEditableValue} from './ZodNonEditableValue';
import {ZodOrNullishEditor} from './ZodOrNullishEditor';
import type {UpdaterFunction} from './ZodSwitch';
-import {getZodSchemaType} from './zod-schema-type';
+import type {AnyZodSchema} from './zod-schema-type';
+import {getUnionOptions, getZodSchemaType} from './zod-schema-type';
import type {JSONPath} from './zod-types';
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const findNull = (value: readonly any[]) => {
+const findNull = (value: readonly AnyZodSchema[]) => {
const nullIndex = value.findIndex((v) => {
const type = getZodSchemaType(v);
return type === 'null' || type === 'undefined';
@@ -31,13 +31,12 @@ const findNull = (value: readonly any[]) => {
};
};
-// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const ZodUnionEditor: React.FC<{
showSaveButton: boolean;
jsonPath: JSONPath;
value: unknown;
defaultValue: unknown;
- schema: any;
+ schema: AnyZodSchema;
setValue: UpdaterFunction<unknown>;
onSave: UpdaterFunction<unknown>;
onRemove: null | (() => void);
@@ -57,7 +56,7 @@ export const ZodUnionEditor: React.FC<{
saveDisabledByParent,
mayPad,
}) => {
- const {options} = schema._def;
+ const options = getUnionOptions(schema);
if (options.length > 2) {
return (
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/local-state.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/local-state.tsx
index 02042cccaa..9cf27d67ad 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/local-state.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/local-state.tsx
@@ -10,11 +10,12 @@ import React, {
import {Internals} from 'remotion';
import type {UpdaterFunction} from './ZodSwitch';
import {deepEqual} from './deep-equal';
+import type {AnyZodSchema, ZodSafeParseResult} from './zod-schema-type';
+import {zodSafeParse} from './zod-schema-type';
export type LocalState<T> = {
value: T;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- zodValidation: {success: boolean; error?: any};
+ zodValidation: ZodSafeParseResult;
keyStabilityRevision: number;
};
@@ -34,8 +35,7 @@ export const useLocalState = <T,>({
savedValue,
}: {
unsavedValue: T;
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- schema: any;
+ schema: AnyZodSchema;
setValue: UpdaterFunction<T>;
savedValue: T;
}) => {
@@ -49,7 +49,7 @@ export const useLocalState = <T,>({
[parentRevision]: {
value: unsavedValue,
keyStabilityRevision: 0,
- zodValidation: schema.safeParse(unsavedValue),
+ zodValidation: zodSafeParse(schema, unsavedValue),
},
};
});
@@ -59,7 +59,7 @@ export const useLocalState = <T,>({
return {
value: unsavedValue,
keyStabilityRevision: 0,
- zodValidation: schema.safeParse(unsavedValue),
+ zodValidation: zodSafeParse(schema, unsavedValue),
};
}
@@ -84,7 +84,7 @@ export const useLocalState = <T,>({
localUnsavedValue ?? {
value: savedValue,
keyStabilityRevision: 0,
- zodValidation: schema.safeParse(savedValue),
+ zodValidation: zodSafeParse(schema, savedValue),
}
);
}, [localUnsavedValue, savedValue, schema]);
@@ -102,7 +102,7 @@ export const useLocalState = <T,>({
return;
}
- const safeParse = schema.safeParse(newValue);
+ const safeParse = zodSafeParse(schema, newValue);
if (safeParse.success || forceApply) {
setValue(updater, forceApply, increment);
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/zod-schema-type.ts b/packages/studio/src/components/RenderModal/SchemaEditor/zod-schema-type.ts
index e374482ae8..5aff45065a 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/zod-schema-type.ts
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/zod-schema-type.ts
@@ -16,6 +16,19 @@
* v4 core schemas have `_zod.def` with `type` property.
* At runtime, all zod schemas have `_def` (classic layer adds it for v4).
*/
+export interface ZodValidationError {
+ format(): {_errors: string[]};
+ issues: readonly {
+ code: string;
+ message: string;
+ path: readonly PropertyKey[];
+ }[];
+}
+
+export type ZodSafeParseResult =
+ | {success: true; data: unknown}
+ | {success: false; error: ZodValidationError};
+
export interface AnyZodSchema {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly _def?: Record<string, any>;
@@ -24,6 +37,19 @@ export interface AnyZodSchema {
readonly description?: string;
}
+/**
+ * Call safeParse on any Zod schema (v3 or v4).
+ * All Zod schemas have safeParse at runtime, but some v4 internal types
+ * (like $ZodObject) don't declare it in their type definition.
+ */
+export const zodSafeParse = (
+ schema: AnyZodSchema,
+ data: unknown,
+): ZodSafeParseResult => {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ return (schema as any).safeParse(data);
+};
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getZodDef = (schema: AnyZodSchema): Record<string, any> => {
if (schema._def) return schema._def;
diff --git a/packages/studio/src/visual-controls/VisualControls.tsx b/packages/studio/src/visual-controls/VisualControls.tsx
index 6eb2f5ed85..90b0427b62 100644
--- a/packages/studio/src/visual-controls/VisualControls.tsx
+++ b/packages/studio/src/visual-controls/VisualControls.tsx
@@ -132,7 +132,7 @@ export const VisualControlsProvider: React.FC<{
const {changed, currentValue} = setControl(key, {
valueInCode: value,
- schema: schema ?? getZodSchemaFromPrimitive(value, z),
+ schema: (schema ?? getZodSchemaFromPrimitive(value, z)) as AnyZodSchema,
stack: new Error().stack as string,
});