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

DX-1543: Introduce Zod Schema #55

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

DX-1543: Introduce Zod Schema #55

wants to merge 7 commits into from

Conversation

fahreddinozcan
Copy link
Collaborator

No description provided.

Copy link

linear bot commented Jan 23, 2025

@fahreddinozcan fahreddinozcan marked this pull request as draft January 23, 2025 20:25
@@ -61,7 +62,8 @@ export const processOptions = <TResponse extends Response = Response, TInitialPa

// try to parse the payload
try {
return JSON.parse(initialRequest) as TInitialPayload;
const parsed = JSON.parse(initialRequest) as TInitialPayload;
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it possible to call schema.parse directly on initialRequest?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No the body should be parsed to json in order to parse with zod, or else zod.string() would match everything, since they're all string without parsing to json

Comment on lines 256 to +259
export type PublicServeOptions<
TInitialPayload = unknown,
TResponse extends Response = Response,
> = Omit<WorkflowServeOptions<TResponse, TInitialPayload>, "onStepFinish" | "useJSONContent">;
> = Omit<WorkflowServeOptions<TResponse, TInitialPayload>, "onStepFinish" | "useJSONContent"> & ValidationOptions<TInitialPayload>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add ValidationOptions to WorkflowServeoptions

: undefined,
baseUrl: environment.UPSTASH_WORKFLOW_URL,
env: environment,
retries: DEFAULT_RETRIES,
useJSONContent: false,
disableTelemetry: false,
schema: z.any(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can add schema to non-required fields https://github.com/upstash/workflow-js/blob/github-workflows/src/serve/options.ts#L24 and not pass anything here

@@ -236,7 +239,7 @@ export const serve = <
TResponse extends Response = Response,
>(
routeFunction: RouteFunction<TInitialPayload>,
options?: Omit<WorkflowServeOptions<TResponse, TInitialPayload>, "useJSONContent">
options?: Omit<WorkflowServeOptions<TResponse, TInitialPayload>, "useJSONContent"> & ValidationOptions<TInitialPayload>
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can remove ValidationOptions from here if we add them to WorkflowServeOptions

Comment on lines 62 to +65

// try to parse the payload
try {
return JSON.parse(initialRequest) as TInitialPayload;
const parsed = JSON.parse(initialRequest) as TInitialPayload;
Copy link
Collaborator

Choose a reason for hiding this comment

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

since we make initialPayloadParser and schema mutually exclusive, this looks odd. The code looks correct theoretically but looks weird.

I am okay to write it this way but we should add tests:

  • what happens if schema and a correct payload (put a spy over schema.parse and make sure it's called)
  • what happens if schema and an incorrect payload is passed -> serve should return error
  • what happens if I override the type errors, pass schema and initial payload parser? schema should be ignored. this won't happen but we should verify the behavior with tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will be adding tests. I didn't want to distribute the zod parser everywhere. Besides even though they're mutually exclusive on public interface, initialParser always exists internally. That's why it made sense to me

@@ -233,7 +230,13 @@ export type WorkflowServeOptions<
* Set `disableTelemetry` to disable this behavior.
*/
disableTelemetry?: boolean;
};
} & ValidationOptions<TInitialPayload>;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@CahidArda I've actually added it there, but somehow they're not available on the PublicServeOptions automatically, that's why i added it again. Couldn't sort out the reason

@fahreddinozcan fahreddinozcan marked this pull request as ready for review January 24, 2025 11:32
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.

2 participants