colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
31.77k stars 1.1k forks source link

Feature Request: async preprocess #1014

Open FlorianWendelborn opened 2 years ago

FlorianWendelborn commented 2 years ago

I’m trying to parse some minecraft NBT data with zod. However, in order to do this, one key of the object (item_bytes) first needs to be converted from the binary NBT format to a JSON-like structure.

The best way to implement this in my opinion would be to use zod’s z.preprocess(). Unfortunately, the library I’m using prismarine-nbt only exposes parse as an async function.

As far as I can tell, async preprocessing is not supported right now. I think it would be great if it’s allowed as it would really simplify parsing with async libraries.

I’d suggest either z.preprocess(async () => { /* ... */ }, z.any()) or z.preprocessAsync(async () => { /* ... */ }, z.any()) as I think either would be reasonable from an API standpoint.

colinhacks commented 2 years ago

Yep, seems like an oversight, should be a quick addition. I'll look into it.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

FlorianWendelborn commented 2 years ago

@stale this issue is still relevant

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

FlorianWendelborn commented 1 year ago

@stale don’t

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

FlorianWendelborn commented 1 year ago

@stale don't

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

FlorianWendelborn commented 1 year ago

@stale don't

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

FlorianWendelborn commented 1 year ago

@stale don’t

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

lennerd commented 1 month ago

A simply workaround:

z.preprocess(
  async (value) => {
    // ...
  },
  z.promise(someSchema), //  Wrap your own schema with a promise schema.
);