Monday, December 4, 2023
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  55] [ 7]  / answers: 1 / hits: 6986  / 2 Years ago, sun, december 19, 2021, 12:00:00

I'm using zod for validation. It seems like if I define a schema and then parse() some input with some extra properties that aren't even in the schema, zod parses the input as valid but just removes those keys.


import { z } from 'zod'

const schema = z.object({
foo: z.string(),
bar: z.number()
})

// this validates fine, printing { foo: 'hello', bar: 1 }
console.log(schema.parse({ foo: 'hello', bar: 1, baz: true }))

However, extra input properties is not something I'd like to ignore, instead I'd like to throw a useful error when that happens, reporting the keys of the extra properties.


Is there a way to do that with zod?


More From » typescript

 Answers
9

You can use the strict option:


const schema = z.object({
foo: z.string(),
bar: z.number()
}).strict();

[#574] Thursday, December 9, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marvinm

Total Points: 406
Total Questions: 104
Total Answers: 121

Location: Iceland
Member since Tue, Jan 25, 2022
2 Years ago
marvinm questions
;