Thanks to visit codestin.com
Credit goes to github.com

Skip to content

object and looseRecord intersection when key has a transformation #5724

@AndreasHald

Description

@AndreasHald

According to the docs

this should work (and it does)

const schema = z.object({
  foo: z.string(),
  bar: z.number()
}).and(z.looseRecord(z.string().regex(/gitte/), z.boolean()))



schema.parse({
    foo: "hello",
    bar: 4,
    gitte: true
}) // success

However if I add a .transform to one of the keys it fails

like here

const schema = z.object({
  foo: z.string(),
  bar: z.number().transform(v => v+4)
}).and(z.looseRecord(z.string().regex(/gitte/), z.boolean()))

schema.parse({
    foo: "hello",
    bar: 4,
    gitte: true
}) // fails

This is an oversimplification of the issue I'm encountering but I'm unsure if this is a bug or if it's not intended to work?

Basically I'm trying to have a complex schema of known keys and various types, but also extend that type with any key that matches a certain regex. but because my different values use .transform it doesn't appear to work

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions