Is there a way to set `allowUnknown` to true by default
talbet opened this issue · comments
Talbet Fulthorpe commented
Is there a way to set the default behavior for objects so that allowUnknown
will be true for all instances of object? I tried created a wrapping function that looked like this:
const laxObject = (shape: object, options = {}) =>
object(shape, { ...options, allowUnknown: true })
But couldn't get it to work without loosing type information on validated objects.
In terms of the use case, I am writing a front end against an API that is rapidly being developed. Hoping not to break the frontend as new features are added to the api. It's very easy to forget to set allowUnknown
to new objects.
David Desmarais-Michaud commented
No worries 👍
import * as mz from 'myzod';
function laxObject<T extends Record<string, mz.Type<any>>>(shape: T) {
return mz.object(shape).allowUnknownKeys();
}
const schema = laxObject({ name: mz.string(), age: mz.number() });
schema.parse({name: 'Bob', age: 20, other: true }) // All good.
Let me know if this does the trick !
Talbet Fulthorpe commented
Thanks, really appreciate you taking the time to figure this out.