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.

Strip unknown keys from object schemas by default

See original GitHub issue

Hi!

First of all, looks like a great job you did with this library. Looks very clean and developer friendly, and has some features I’m missing from io-ts. Actually, I extended io-ts with some of them your library offers out of the box, but before I would jumping into and trying whether your library would work in my project, I’d like to ask something in advance to see if I could achieve it with it: Is there a way to ‘export’ a structure (object) that way Zod would delete the keys that are masked out (with omit)?

The use case: I’d like to use Zod to serialize (‘export’) and unserialize (parse) custom structures (actually React component states, but could happen for multiple other use cases). But sometimes, there are data in the component state which is needed for runtime, but not needed (and I don’t want) to serialize/unserialize.

At unserialization, masking is OK, it will ignore the not needed ones if I create a new ‘masked’ type. But at serialization… I have the full data structure, and if I serialize it, it would contain all, even masked data as well. That’s what I don’t want.

An example based on a doc example:

const Recipe = z.object({
  id: z.string(),
  name: z.string(),
  ingredients: z.array(z.string()),
});

const NoIDRecipe = Recipe.omit({ id: true });
type NoIDRecipe = z.infer<typeof NoIDRecipe>;

const data: Recipe = { id: 1, name: 'test', ingredients: ['x', 'y', 'z'] };

const serializable = NoIDRecipe.<serialize/export/clean?>(data);

Here, I’d like to make a copy of the ‘data’ variable, but with the NoIDRecipe schema, and in the copy (serializable) structure I want the ‘id’ keys to be removed, based on the schema (as NoIDRecipe doesn’t contain it). Is it possible somehow?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
colinhackscommented, Sep 17, 2020

Zod 2 is now in beta and strips all unknown keys by default!

@dexx086 @johannessjoberg

0reactions
johannessjobergcommented, Sep 11, 2020

Looking forward to this feature as an avid joi user 🙌

Yep this will definitely be possible/supported whenever the transformation API drops.

Is there a timeline regarding this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stripping unknown keys when validating with Joi
You need to use the stripUnknown option if you want to strip the unknown keys from the objects that you are validating.
Read more >
17.7.0 API Reference - joi.dev
The schema can be a plain JavaScript object where every key is assigned a joi type, ... Values (or keys in case of...
Read more >
zod - npm
Object schemas now strip unknown keys by default, instead of throwing an error; Optional and nullable schemas are now represented with the ...
Read more >
How To Use Joi for Node API Schema Validation - DigitalOcean
You can learn more about how to use Joi and the supported schema ... stripUnknown: true // remove unknown keys from the validated...
Read more >
Query nested objects in a document with unknown key
Hi, I have a collection with many documents that do have a “item” property where the value is an object with key-value-pairs: {...
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