zod icon indicating copy to clipboard operation
zod copied to clipboard

Data transformation with zod schema

Open asherccohen opened this issue 3 years ago • 3 comments

Hi, I'm investigating ways to automate data transformation.

Since zod's main purpose is to validate data using a schema, would there be interest in also transforming a source object into the result described by the schema?

Probably an example would help understanding the use case.

const source = {
  foo: 'baz',
  bar: ['bar', 'foo'],
  baz: {
    qux: 'bazqux'
  }
};
const schema = {
  foo: 'foo', // Simple Projection
  bazqux: 'baz.qux' // Grab a value from a deep path
};

morphism(schema, source);
//=> { foo: 'baz', bazqux: 'bazqux' }```



This is inspired by a package called morphism: https://github.com/nobrainr/morphism

asherccohen avatar Jun 05 '22 12:06 asherccohen

That's a nice compact syntax, but this is something that is already achievable with transform which can be pretty powerful with destructuring and can do quite a bit more than just mapping:

const schema = z
  .object({
    foo: z.string(),
    bar: z.array(z.string()),
    baz: z.object({
      qux: z.string(),
    }),
  })
  .transform(({ foo, baz: { qux: bazqux } }) => ({
    foo,
    bazqux,
  }));

Would be cool to make it easier to use morphism with Zod, though rather than combining them since they really cover different use cases. If you have any ideas around that, feel free to reach out here or on the Discord!

scotttrinh avatar Jun 06 '22 15:06 scotttrinh

That's a great explanation, thanks. I was under the impression that .transform only affected the types, but now I see how it can map too. I'll experiment a bit (I'm new to both libs) and then reach out with my discovering.

Kind Regards Asher


From: Scott Trinh @.> Sent: Monday, June 6, 2022 5:28:08 PM To: colinhacks/zod @.> Cc: Asher Cohen @.>; Author @.> Subject: Re: [colinhacks/zod] Data transformation with zod schema (Issue #1189)

That's a nice compact syntax, but this is something that is already achievable with transform which can be pretty powerful with destructuring and can do quite a bit more than just mapping:

const schema = z .object({ foo: z.string(), bar: z.array(z.string()), baz: z.object({ qux: z.string(), }), }) .transform(({ foo, baz: { qux: bazqux } }) => ({ foo, bazqux, }));

Would be cool to make it easier to use morphism with Zod, though rather than combining them since they really cover different use cases. If you have any ideas around that, feel free to reach out here or on the Discord!

— Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fcolinhacks%2Fzod%2Fissues%2F1189%23issuecomment-1147579403&data=05%7C01%7C%7C2841d33ab8b0471a405e08da47d128c9%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637901260917178547%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=lk660fUa9beeEP8vO2uxCkpTR0Z62sL%2BJPFerRji35c%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAIIV4EMB7W6XU4W5EAJZE2DVNYKIRANCNFSM5X46PMFQ&data=05%7C01%7C%7C2841d33ab8b0471a405e08da47d128c9%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637901260917178547%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=yE0CNv%2FdIJ6Uyjq2T7%2FZVhEcbhzzhkHG1LGQzBJdK5Y%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>

asherccohen avatar Jun 06 '22 17:06 asherccohen

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] avatar Aug 05 '22 18:08 stale[bot]