Giter Club home page Giter Club logo

Comments (6)

deini avatar deini commented on May 18, 2024 4

Now that the requirement is 4.1+, any chance z.json() gets revisited?

from zod.

colinhacks avatar colinhacks commented on May 18, 2024 1

@ggoodman I decided to add this snippet to the README so it's easier to find 👍 Thanks again!

from zod.

colinhacks avatar colinhacks commented on May 18, 2024 1

Yikes! When I copied the snippet above into the README I renamed Json to jsonSchema (to avoid confusion with the built-in JSON class) but I forgot to change it everywhere 😬 Here's the correct snippet:

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

jsonSchema.parse({
  // data
})

Thanks for pointing this out! I fixed this in the README as well.

from zod.

colinhacks avatar colinhacks commented on May 18, 2024

Love this! I'd add it to the standard API (z.json()) but the recursive type alias required TypeScript 3.7. Currently everything else in the lib only requires 3.2+ so I don't want to bump the version without a really compelling reason. But thanks for sharing!

from zod.

danalexilewis avatar danalexilewis commented on May 18, 2024

Hey @ggoodman or @vriad, thanks for this helpful snippet.

I'm slowly getting more comfortable with Zod. But every now and then I feel like I am just missing the point of something.

Now is such a time.

When using this snippet with say jsonSchema.parse(data)

I get this in the log.

    types/util.ts:190:68 - error TS2693: 'Literal' only refers to a type, but is being used as a value here.

    190 export const jsonSchema: z.ZodSchema<Json> = z.lazy(() => z.union([Literal, z.array(Json), z.record(Json)]));
                                                                           ~~~~~~~
    types/util.ts:190:85 - error TS2693: 'Json' only refers to a type, but is being used as a value here.

    190 export const jsonSchema: z.ZodSchema<Json> = z.lazy(() => z.union([Literal, z.array(Json), z.record(Json)]));
                                                                                            ~~~~
    types/util.ts:190:101 - error TS2693: 'Json' only refers to a type, but is being used as a value here.

    190 export const jsonSchema: z.ZodSchema<Json> = z.lazy(() => z.union([Literal, z.array(Json), z.record(Json)]));

After trying to work this out I realised I am just playing whack a mole.

Any direction would be appreciated :)

from zod.

bryantabaird avatar bryantabaird commented on May 18, 2024

Sharing here in case it will help somebody else, for our application, we had to change the string to be a symbol to handle some of our more complex types:

type Json = Literal | { [key: symbol]: Json } | Json[];

from zod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.