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

When parsing an Object, keys with undefined value should only be returned if they are provided in input #199

Closed
xinha-sh opened this issue Oct 5, 2023 Discussed in #196 · 2 comments
Assignees
Labels
enhancement New feature or request priority This has priority

Comments

@xinha-sh
Copy link

xinha-sh commented Oct 5, 2023

Discussed in #196

Originally posted by xinha-sh October 4, 2023

import * as v from "valibot";

const baseSchema = v.object({
  x: v.optional(v.array(v.string())),
  y: v.optional(v.array(v.string())),
 z: v.optional(v.array(v.string())),
})

const schemaA = v.object({
  key1: v.optional(baseSchema), 
  key2: v.optional(baseSchema)
})

const input = {key1:  {x: ["hello", "world"]}}
const result = v.parse(schemaA, input)

// result.output
// {key1: {x:  ["hello", "world"]}, key2: undefined}

Why do I get key2: undefined in the output ? What's the best way to ignore them.

My use case is that upon successful parsing I want to loop over array values via Object.values(catalog).map(item => objectKeys(item)).flat() which breaks with error Cannot convert undefined or null to object

This used to work as intended with zod schema.
Unless my input is const input = {key1: {x: ["hello", "world"]}, key2: undefined} the output shouldn't contain undefined.

What's valibot's way of stripping away undefined keys ?

@fabian-hiller fabian-hiller self-assigned this Oct 5, 2023
@fabian-hiller fabian-hiller added enhancement New feature or request priority This has priority labels Oct 5, 2023
@fabian-hiller
Copy link
Owner

Thank you! I will investigate this.

@fabian-hiller
Copy link
Owner

Fixed in v0.19.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority This has priority
Projects
None yet
Development

No branches or pull requests

2 participants