question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON utility type

See original GitHub issue

I just found myself needing a generic type for any valid JSON and put this together:

type Literal = boolean | null | number | string;
type Json = Literal | { [key: string]: Json } | Json[];

const Literal = Zod.union([Zod.boolean(), Zod.null(), Zod.number(), Zod.string()]);
const Json: Zod.ZodType<Json> = Zod.lazy(() =>
  Zod.union([Literal, Zod.array(Json), Zod.record(Json)])
);

Putting this in an issue for anyone who might be on the lookout or if the library might want to integrate a higher-level utility constructs like this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
deinicommented, Aug 9, 2022

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

1reaction
colinhackscommented, Sep 9, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON<T> utility type · Issue #48697 · microsoft/TypeScript
A JSON<T> utility type, where T is a record type of any shape and the output would be what you would get from...
Read more >
JSON Serialization - Unity - Manual
Use the JsonUtility class to convert Unity objects to and from the JSON format. For example, you can use JSON Serialization to interact...
Read more >
Documentation - Utility Types - TypeScript
TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.
Read more >
JSON Type Definition
JSON Type Definition is a lightweight schema language for JSON data. Describe the shape of your data once, and get portable validators and...
Read more >
MakeTypes from JSON samples - John Vilk
MakeTypes generates TypeScript classes that parse and typecheck JSON objects at runtime, and let you statically type check code that interacts with JSON ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found