-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field becomes optional when value is any or unknown #1628
Comments
I'd say this is intentional for the moment. Currently Zod can't really differentiate between const schema = z.object({ x: z.unknown() });
schema.parse({}); // works The broader question of how/whether to provide an API that differentiates between different types of optionality is tracked here: #635 |
How does one write |
I came up to this object schema definition export type ZodObjectIdentity<T extends z.ZodRawShape, Side extends '_input' | '_output'> = {
[K in keyof T]: T[K][Side];
};
export function zodObjectIdentity<T extends z.ZodRawShape>(
t: T,
): z.ZodObject<T, 'strip', z.ZodTypeAny, ZodObjectIdentity<T, '_output'>, ZodObjectIdentity<T, '_input'>> {
// @ts-ignore
return z.object(t);
} It removes question marks magic, so you can use it like this const s = zodObjectIdentity({ x: z.unknown() })
type s = z.infer<typeof s>
//^? { x: unknown } |
I am also running into this issue- as I'm creating a generic parser for a first pass on some inputs and one of the properties just has to be an Right now, it seems like there's no way for us to define a schema like so: interface ISchemaExample {
inputs: object;
otherProperty: string;
} Where z.object() It always requires that I give it a specific shape. It would be really nice if we could define "any object" as a property (and it is a required property when it does schema validation). |
This is the best I've got: const schema = z.object({
field: z.custom((x) => x !== undefined),
}); |
Cool idea! Open to this as a PR. Just setting the "default shape" to z.object().passthrough() |
If you guys like me need to make sure the return type is compatible with existing interfaces, then you could do something like this: object({
field: custom<MyType>((val) => val !== undefined && val !== null),
})
// Or
object({
field: object({}).passthrough().refine<MyType>((val): val is MyType => val !== undefined && val !== null),
}) |
I've just added a PR which pretty much does exactly that (makes the shape an empty object and set the object to |
Isn't that the purpose of Possible today: z.record(z.unknown()) // don't care about key type. Proposal: z.record() // don't care about anything. Intuitively, when writing typescript, you'd use |
What does it infer to? I'm almost certain it will infer to Of course, in reality, Zod is smart enough to let only objects pass. But if you then use this Schema as a function parameter validator, for example, Zod will parse the argument correctly, but TypeScript will not complain if you pass it a There are some things we can do to solve this:
|
Awesome! Thanks for looking into that. |
Has this been resolved? Is so, I'd like to close this issue. |
Sorry for late response @JacobWeisenburger - but I don't think this has been resolved until #1675 gets merged. |
Error |
The text was updated successfully, but these errors were encountered: