Skip to content

Add zod module #502

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

Merged
merged 1 commit into from
Mar 12, 2025
Merged

Add zod module #502

merged 1 commit into from
Mar 12, 2025

Conversation

igalshilman
Copy link
Contributor

@igalshilman igalshilman commented Mar 12, 2025

const User = z.object({
  name: z.string(),
  age: z.number().optional(),
});

type User = z.infer<typeof User>;

const greeter = restate.service({
  name: "greeter",
  handlers: {
    greet: restate.handlers.handler(
      { input: serde.zod(User, "user") },
      async (ctx: restate.Context, user: User) => {
        return `Hello ${user.name}`;
      }
    ),
  },
});

@igalshilman igalshilman requested a review from nikrooz March 12, 2025 18:21
Copy link
Contributor

@nikrooz nikrooz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🤩

"files": [
"dist"
],
"devDependencies": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be

Suggested change
"devDependencies": {
"dependencies": {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never mind, we are only using the types from @restatedev/restate-sdk-core

"devDependencies": {
"@restatedev/restate-sdk-core": "^1.4.0"
},
"dependencies": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and maybe this should be

Suggested change
"dependencies": {
"peerDependencies": {

@igalshilman igalshilman merged commit 5cacb0e into restatedev:main Mar 12, 2025
2 checks passed
@marcus-sa
Copy link

I appreciate that this is being made optional and not mandatory.

@mupperton
Copy link
Contributor

@igalshilman - What am I doing wrong?

image

image

With deps:

    "@restatedev/restate-sdk": "0.0.0-SNAPSHOT-20241220152906",
    "@restatedev/restate-sdk-zod": "0.0.0-SNAPSHOT-20250312193516",
    "zod": "3.23.8",

@igalshilman
Copy link
Contributor Author

@mupperton take the latest deps (delete the package-lock)

here is a usage example here: https://github.com/restatedev/sdk-typescript/blob/main/packages/restate-sdk-examples/src/zod_greeter.ts

@mupperton
Copy link
Contributor

@igalshilman Found my issue, I had [email protected] but zod-to-json-schema required ^3.24.1 so had 2 versions installed with differing zod types

Btw that example doesn't have the zod packages listed in the package dependencies

@igalshilman
Copy link
Contributor Author

@mupperton ok good to hear that is resolved!

But also the serde.zod( is a single argument parameter, it used to have 2, but not anymore. Did you had to change the code snippet as-well?

Also, how would you recommend to specify the dependencies in restate-sdk-zod package?

@mupperton
Copy link
Contributor

mupperton commented Mar 26, 2025

Ah, the @latest tag on npm is pointing to an older snapshot, that's why I needed 2 args, but yeah the @dev tag does not

For specifying the deps, my only suggestion would be aligning the minimum zod version with that of zod-to-json-schema's minimum version, although it may be a pain to maintain if you bump versions on either

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

Successfully merging this pull request may close these issues.

4 participants