Replies: 4 comments 8 replies
-
Would love to have that! 👍 |
Beta Was this translation helpful? Give feedback.
-
I tried using your example, but even putting the code in a sandbox like https://playcode.io/typescript gives me errors: Somehow eventhough for instance 'name' is not Nullable or Optional, a ZodNullable<ZodOptional<ZodType>> definition is required in the schema. What's going wrong here? |
Beta Was this translation helpful? Give feedback.
-
I have followed this through a chain of closed issues suggesting the feature of inferring a Zod schema from an existing type. There sure are quite a few closed issues requesting this feature. 🤔 Here's my take on why this should be included in Zod...Say you have a enum VehicleType {
SUV
Van
Compact
Luxury
Sedan
Other
}
enum VehicleFuelType {
Gasoline
Diesel
Electric
Hybrid
PluginHybrid
Propane
Hydrogen
CNG
Other
}
model Vehicle {
id Int @id @default(autoincrement()) @map("vehicle_id") @unique
created_by String
date_added DateTime
last_updated DateTime
name String
description String?
make String
model String
year Int
type VehicleType
fuel_type VehicleFuelType
departing_from String
@@map("vehicle")
} Well, let's say I want to do some form validation. I have a "submit" form that captures and sends a POST request to my backend, and I can, of course, import this as a type into my project like so: import type { Vehicle } from '@prisma/client' But the thing is, I can't simply infer a Zod schema. import type { Vehicle } from '@prisma/client'
import { z } from 'zod'
const vehicleSchema = z.inferSchema(Vehicle) Which means instead of writing 3 elegant lines of code, if I want to reach for Zod to handle some client-side validation, I have to do some ugly garbage like this: import { VehicleType, VehicleFuelType } from '@prisma/client'
const vehicleFormSchema = z.object({
id: number(),
date_added: z.date(),
last_updated: z.date(),
name: z.string(),
description: z.string(),
make: z.string(),
model: z.string(),
year: z.number(),
type: z.nativeEnum(VehicleType),
fuel_type: z.nativeEnum(VehicleFuelType),
departing_from: z.string()
});
type VehicleForm = z.infer<typeof formSchema>;
// ... etc All just to effectively re-declare a type that I already have already defined. Now, yes, I get that using Zod is most valuable when you're writing custom error messages for each type of validation error/issue, and you could argue that you should do all Zod validation as above. But in my use case, I have no custom error messages in this validation, because maybe that's all I need. If I do want custom error messages from Zod, and I wanted to const vehicleFormSchema = z.inferSchema(Vehicle).enhance({
// ... custom fields & error messages go here
}) In my mind the above idea would give you some kind of hinting/feedback inside of |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
What about adding a helper that takes a type and "forces" you to create a schema according to this type?
Can be useful, for example, to create a schema derived from a Prisma Model.
This is maybe not perfect, but I use this on my projects and I'm pretty happy with It.
Force to put
nullable
optional
ornullish
when Model requires it.Unknown properties are forbidden and cause a TS error.
Discussed here : #372 (comment)
Beta Was this translation helpful? Give feedback.
All reactions