question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow defining a different key into the final destination object after parsing

See original GitHub issue

Right now zod is becoming more and more an integral part of all my projects, however, there’s still a pretty common use case that doesn’t have a direct mapping in zod, basically, allowing to validate a different schema than the output one.

Most of the times, in TypeScript codebases, interfacing with an API involves firing a fetch request and validating the received data to make sure it corresponds to the shape we’re defining in our types. Zod is really great for this due to it’s practically 1:1 equivalence with TS typings. However, sometimes, the types we receive from the request don’t correspond directly to the types we’ve defined in our codebase (legacy code, different naming schemas, etc.) and that forces us to having to create yet another layer after validation to do processing.

So I was wondering, if it’d be possible and zod architecture would allow to define a way to “collect” keys from the input data and “assign” them to another key in the resulting data.

For example, let’s imagine we’re building a front-end app using TypeScript (camel case naming) and we request data to our back-end in Rails (snake case naming). Without the functionality described above, we have two solutions (without counting the one that is leaving the naming as is for obvious reasons):

  • Validate the data and then transform the data, so our validators will refer to something that doesn’t really exist in the app
  • Transform the data and then validate having to transform loosely typed data.

However, adding this functionality to zod, we could do it in just one single step and correctly coupling these two non-independent descriptions thus also providing clarity of intent.

An idea:

const UserSchema = z.object({
  firstName: z.fromKey('first_name').string()
});

The code above will validate that first_name is present and is a string, while at the same time, producing a firstName key with the correctly validated data.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:13
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
colinhackscommented, Jul 4, 2021

I’m not a fan of the z.fromKey proposal. This is a transformation that happens at the object-level, so defining the key remapping on a per-key basis is weird to me. I’d consider a .remap method on ZodObject:

const UserSchema = z.object({
  first_name: z.string(),
}).remap({
  first_name: 'firstName'
});

Under the hood this would just be syntactic sugar on top of .transform. Thoughts @larsbs?

5reactions
kevinmamaqicommented, May 1, 2022

@colinhacks Is there a remap method?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to parse JSON strings in Flutter - LogRocket Blog
It can be more difficult to parse JSON strings in Flutter compared to other languages. Let's learn how to encode and decode JSON...
Read more >
Key Value Parser Delivers Useful Information Fast - Graylog
The Key Value Parser looks for and returns information based on pairs of objects. The pairs are grouped by key and value. In...
Read more >
Fetch data dynamically - Dart programming language
Data can be serialized into a JSON string, which is then passed between a client and server, and revived as an object at...
Read more >
How to pass an object from one activity to another on Android
Think of properties as private instance variables. Since they're private, the only way they can be accessed from outside of their class is...
Read more >
Dockerfile reference - Docker Documentation
Dockerfiles use a simple DSL which allows you to automate the steps you would ... This may be after parser directives, comments, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found