Skip to content

Schema with Enum type is given all entries of Enum #567

@baldwinboy

Description

@baldwinboy

I love the work done here to make Zod more production ready. However, I've found a bug on enum schemas.

Reproduction

import * as z from 'zod'
import { zx } from '@traversable/zod'

const schema = z.object({
   option: z.enum(['FOO', 'BAR', 'BAZ']).default('BAZ')
})

const defaultValues = zx.defaultValues(schema, { number: 0, string: '', array: [], boolean: false })
console.log(defaultValues)

Expected

[Object: null prototype] { option: 'BAZ' }

Actual

[Object: null prototype] { option: { FOO: 'FOO', BAR: 'BAR', BAZ: 'BAZ' } }

Suspected bug

case tagged('enum')(x): return x._zod.def.entries ?? CATCH_ALL

Working solution

case tagged('enum')(x):
- return x._zod.def.entries ??  CATCH_ALL
+ return x._zod.def.entries
+          ? Object_values(x._zod.def.entries)[0]
+          : CATCH_ALL

This does not cover enums with default values

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions