Skip to content

Commit 9a08f0c

Browse files
authored
type fixes for zod v4 (#6604)
## 📝 Summary <!-- Provide a concise summary of what this pull request is addressing. If this PR fixes any issues, list them here by number (e.g., Fixes #123). --> PR Looks good, just small type fixes. ## 🔍 Description of Changes <!-- Detail the specific changes made in this pull request. Explain the problem addressed and how it was resolved. If applicable, provide before and after comparisons, screenshots, or any relevant details to help reviewers understand the changes easily. --> ## 📋 Checklist - [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md). - [ ] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable). - [ ] I have added tests for the changes made. - [x] I have run the code and verified that it works as expected.
1 parent d8640e0 commit 9a08f0c

File tree

4 files changed

+26
-33
lines changed

4 files changed

+26
-33
lines changed

frontend/src/components/forms/form-utils.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { z } from "zod";
44
import { Logger } from "@/utils/Logger";
55
import { isZodArray, isZodPipe, isZodTuple } from "@/utils/zod-utils";
66

7-
export function maybeUnwrap<T extends z.ZodType<unknown>>(
8-
schema: T,
9-
): z.ZodType<unknown> {
7+
export function maybeUnwrap<T extends z.ZodType>(schema: T): z.ZodType {
108
if ("unwrap" in schema) {
11-
return (schema as unknown as z.ZodOptional).unwrap() as z.ZodType<unknown>;
9+
return (schema as unknown as z.ZodOptional).unwrap() as z.ZodType;
1210
}
1311
return schema;
1412
}
@@ -76,18 +74,16 @@ export function getDefaults<TSchema extends z.ZodType<T>, T>(
7674
}
7775

7876
return Object.fromEntries(
79-
Object.entries(schema.shape).map(
80-
([key, value]: [string, z.ZodType<unknown>]) => {
81-
return [key, getDefaultValue(value)];
82-
},
83-
),
77+
Object.entries(schema.shape).map(([key, value]: [string, z.ZodType]) => {
78+
return [key, getDefaultValue(value)];
79+
}),
8480
) as T;
8581
}
8682

8783
/**
8884
* Get the literal value of a union
8985
*/
90-
export function getUnionLiteral<T extends z.ZodType<unknown>>(
86+
export function getUnionLiteral<T extends z.ZodType>(
9187
schema: T,
9288
): z.ZodLiteral<string> {
9389
if (schema instanceof z.ZodLiteral) {

frontend/src/components/forms/form.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
import { cn } from "@/utils/cn";
2828
import { Events } from "@/utils/events";
2929
import { Strings } from "@/utils/strings";
30+
import { isZodPipe } from "@/utils/zod-utils";
3031
import { Objects } from "../../utils/objects";
3132
import { Button } from "../ui/button";
3233
import { Checkbox } from "../ui/checkbox";
@@ -101,7 +102,7 @@ export function renderZodSchema<T extends FieldValues, S>(
101102
} = FieldOptions.parse(schema.description || "");
102103

103104
if (schema instanceof z.ZodDefault) {
104-
let inner = schema.unwrap() as z.ZodType<unknown>;
105+
let inner = schema.unwrap() as z.ZodType;
105106
inner =
106107
!inner.description && schema.description
107108
? inner.describe(schema.description)
@@ -110,7 +111,7 @@ export function renderZodSchema<T extends FieldValues, S>(
110111
}
111112

112113
if (schema instanceof z.ZodOptional) {
113-
let inner = schema.unwrap() as z.ZodType<unknown>;
114+
let inner = schema.unwrap() as z.ZodType;
114115
inner =
115116
!inner.description && schema.description
116117
? inner.describe(schema.description)
@@ -134,7 +135,7 @@ export function renderZodSchema<T extends FieldValues, S>(
134135
{Objects.entries(schema.shape).map(([key, value]) => {
135136
const isLiteral = value instanceof z.ZodLiteral;
136137
const childForm = renderZodSchema(
137-
value as z.ZodType<unknown>,
138+
value as z.ZodType,
138139
form,
139140
joinPath(path, key),
140141
renderers,
@@ -322,7 +323,7 @@ export function renderZodSchema<T extends FieldValues, S>(
322323
<div className="flex flex-col gap-1">
323324
<Label>{label}</Label>
324325
<FormArray
325-
schema={schema.element as z.ZodType<unknown>}
326+
schema={schema.element as z.ZodType}
326327
form={form}
327328
path={path}
328329
key={path}
@@ -335,7 +336,7 @@ export function renderZodSchema<T extends FieldValues, S>(
335336

336337
if (schema instanceof z.ZodDiscriminatedUnion) {
337338
const def = schema.def;
338-
const options = def.options as z.ZodType<unknown>[];
339+
const options = def.options as z.ZodType[];
339340
const discriminator = def.discriminator;
340341
const getSchemaValue = (value: string) => {
341342
return options.find((option) => {
@@ -402,7 +403,7 @@ export function renderZodSchema<T extends FieldValues, S>(
402403
control={form.control}
403404
name={path}
404405
render={({ field }) => {
405-
const options = schema.options as z.ZodType<unknown>[];
406+
const options = schema.options as z.ZodType[];
406407
let value: string = field.value;
407408
const types = options.map((option) => {
408409
return getUnionLiteral(option).value;
@@ -460,13 +461,8 @@ export function renderZodSchema<T extends FieldValues, S>(
460461
// Handle ZodEffects (transforms/refinements)
461462
return renderZodSchema(maybeUnwrap(schema), form, path, renderers);
462463
}
463-
if (schema instanceof z.ZodPipe) {
464-
return renderZodSchema(
465-
schema.in as z.ZodType<unknown>,
466-
form,
467-
path,
468-
renderers,
469-
);
464+
if (isZodPipe(schema)) {
465+
return renderZodSchema(schema.in, form, path, renderers);
470466
}
471467

472468
return (
@@ -487,7 +483,7 @@ const FormArray = ({
487483
minLength,
488484
renderers,
489485
}: {
490-
schema: z.ZodType<unknown>;
486+
schema: z.ZodType;
491487
form: UseFormReturn<any>;
492488
path: Path<any>;
493489
renderers: FormRenderer[];

frontend/src/core/config/config-schema.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export function parseConfigOverrides(config: unknown): {} {
291291
}
292292

293293
export function defaultUserConfig(): UserConfig {
294-
return UserConfigSchema.parse({
294+
const defaultConfig: Partial<Record<keyof UserConfig, unknown>> = {
295295
completion: {},
296296
save: {},
297297
formatting: {},
@@ -302,5 +302,6 @@ export function defaultUserConfig(): UserConfig {
302302
server: {},
303303
ai: {},
304304
package_management: {},
305-
}) as unknown as UserConfig;
305+
};
306+
return UserConfigSchema.parse(defaultConfig) as UserConfig;
306307
}

frontend/src/utils/zod-utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1+
/* Copyright 2024 Marimo. All rights reserved. */
2+
13
import z from "zod";
24

35
export function isZodArray<T>(
4-
schema: z.ZodType<unknown>,
5-
): schema is z.ZodArray<z.ZodType<unknown>> {
6+
schema: z.ZodType,
7+
): schema is z.ZodArray<z.ZodType> {
68
return schema instanceof z.ZodArray;
79
}
810

9-
export function isZodPipe(
10-
schema: z.ZodType<unknown>,
11-
): schema is z.ZodPipe<z.ZodType<unknown>> {
11+
export function isZodPipe(schema: z.ZodType): schema is z.ZodPipe<z.ZodType> {
1212
return schema instanceof z.ZodPipe;
1313
}
1414

1515
export function isZodTuple(
16-
schema: z.ZodType<unknown>,
17-
): schema is z.ZodTuple<z.ZodType<unknown>[]> {
16+
schema: z.ZodType,
17+
): schema is z.ZodTuple<z.ZodType[]> {
1818
return schema instanceof z.ZodTuple;
1919
}

0 commit comments

Comments
 (0)