Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zod record with required keys

Tags:

zod

typescript

I'm trying to create a dictionary like zod schema for an object that has many keys, defined elsewhere, and they all have the same value type. And all keys are required.

const KeysSchema = z.enum(['a', 'b', 'c', /*...*/]) // long key list
const ObjSchema = z.record(KeysSchema, z.number())

But the yielded type is:

{
    a?: number | undefined;
    b?: number | undefined;
    c?: number | undefined;
}

But what I want is:

{
    a: number;
    b: number;
    c: number;
}

I want: Record<'a', 'b', 'c', number>

but it's giving me: Partial<Record<"a" | "b" | "c", number>>

And I can't call z.record(...).required() because that's not a thing for zod records.

How can I make a record type with required keys?

See Typescript playground

like image 415
Alex Wayne Avatar asked Oct 16 '25 04:10

Alex Wayne


1 Answers

Managed to solve it with a .refine()

  .refine((obj): obj is Required<typeof obj> =>
    KeysSchema.options.every((key) => obj[key] != null),
  )

It checks every key in the KeysSchema and asserts that all keys are present, and it's not a partial at all.

See Playground

like image 173
Alex Wayne Avatar answered Oct 17 '25 16:10

Alex Wayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!