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

Suggestion for intersection types #109

Open
rsan-one-network opened this issue Aug 12, 2024 · 2 comments
Open

Suggestion for intersection types #109

rsan-one-network opened this issue Aug 12, 2024 · 2 comments

Comments

@rsan-one-network
Copy link

Right now when the json schema has intersection types you are using the ".and" command to join them together. Would it not be better to use the .merge command as it's suggested in the documentation?

image

In my implementation i need to access the shape attribute of the generated zod objects but ZodIntersection does not have it.

@rsan-one-network
Copy link
Author

rsan-one-network commented Aug 12, 2024

Json schema example:

{
  "type" : "object",
  "properties" : {
    "attribute1" : {
      "type" : "boolean"
    },
    "attribute2" : {
      "type" : "integer"
    },
    "attribute3" : {
      "type" : "integer"
    }
  },
  "additionalProperties" : false,
  "allOf" : [ {
    "type" : "object",
    "properties" : {
      "attribute4" : {
        "type" : "string"
      },
      "attribute5" : {
        "type" : "integer"
      }
    },
    "additionalProperties" : false
  } ]
}

Current output:

export default z
  .object({
    attribute1: z.boolean().optional(),
    attribute2: z.number().int().optional(),
    attribute3: z.number().int().optional(),
  })
  .strict()
  .and(
    z
      .object({
        attribute4: z.string().optional(),
	    attribute5: z.number().int().optional(),
      })
      .strict()
  );

Proposed output:

export default z
  .object({
    attribute1: z.boolean().optional(),
    attribute2: z.number().int().optional(),
    attribute3: z.number().int().optional(),
  })
  .strict()
  .merge(
    z
      .object({
        attribute4: z.string().optional(),
	    attribute5: z.number().int().optional(),
      })
      .strict()
  );

`
``

@StefanTerdell
Copy link
Owner

Sure, let's try it out. Open to PRs

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

No branches or pull requests

2 participants