Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definition misconfigured #106

Open
wataruoguchi opened this issue Feb 23, 2024 · 1 comment
Open

Type definition misconfigured #106

wataruoguchi opened this issue Feb 23, 2024 · 1 comment
Labels

Comments

@wataruoguchi
Copy link

wataruoguchi commented Feb 23, 2024

First of all, thank you for creating the module!

I started using your zod-to-json-schema and json-schema-to-zod. While the code looks to be working, my VSCode displays errors.

"json-schema-to-zod": "^2.0.14",
"typescript": "^5.3.3",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.4"
import { jsonSchemaToZod } from "json-schema-to-zod";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";

const testSchema = z.object({
  name: z.string(),
  age: z.number().optional(),
});
const jsonSchema = zodToJsonSchema(testSchema);
const zodSchema = jsonSchemaToZod(jsonSchema);  // <-- 1st type error with "jsonSchema"
console.log({
  jsonSchema,
  zodSchema,
  properties: jsonSchema.properties,  // <-- 2nd type error with "properties"
});
Screenshot 2024-02-22 at 8 07 56 PM

The result of the console.log is below:
Screenshot 2024-02-22 at 8 08 28 PM

The variable jsonSchema has properties that JsonSchemaObject type expects in json-schema-to-zod.

@StefanTerdell
Copy link
Owner

Hi, thanks for opening an issue.

The first problem is valid. jsonSchemaToZod should accept the output types of zodToJsonSchema. I will look into this... at some point. ;)

The second problem (the "missing" properties) is not a bug. The output will not have inference, like the Zod schema does (meaning the return type just says that it's a JSON Schema, not what the schema contains). Since not all JSON Schemas have "properties" you have to check for it manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants