Skip to content

Commit

Permalink
fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Aug 17, 2024
1 parent 9b1c275 commit 3b8048e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions playground/app/routes/parse-with-zod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import { Form, useActionData, useLoaderData } from '@remix-run/react';
import { Playground, Field } from '~/components';
import { z } from 'zod';

const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
type Literal = z.infer<typeof literalSchema>;
type Json = Literal | { [key: string]: Json } | Json[];
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]),
);

const schema = z.object({
username: z.string().min(3, 'Username is too short'),
profile: jsonSchema,
profile: z.preprocess(
(json) => {
if (typeof json === 'string' && json !== '') {
return JSON.parse(json);
}

return;
},
z.object({
age: z.number().int().positive(),
}),
),
});

export async function loader({ request }: LoaderFunctionArgs) {
Expand Down Expand Up @@ -50,9 +54,9 @@ export default function Example() {
lastResult,
defaultValue: {
username: 'test',
profile: {
profile: JSON.stringify({
age: 35,
},
}),
},
onValidate: !noClientValidate
? ({ formData }) => parseWithZod(formData, { schema })
Expand Down

0 comments on commit 3b8048e

Please sign in to comment.