Closed angelhdzmultimedia closed 6 months ago
I think I may have solved it.
export const UserValidationSchema = z.object({
id: z.string().mongooseTypeOptions({auto: true, type: 'String', }),
email: z.string().email(),
uuid: z.string(),
password: z.string().min(6),
profile: UserProfileValidationSchema.mongooseTypeOptions({
ref: 'UserProfile',
type: mongoose.Schema.Types.ObjectId
})
})
export type UserData = z.infer<typeof UserValidationSchema>
export const UserModelSchema = toMongooseSchema(UserValidationSchema.mongoose({
schemaOptions: {
collection: 'users',
query: {
}
}
}))
Moved the .mongoose
chained method to the toMongooseSchema
function call, and left the UserValidationSchema alone, now I got the .pick method back!!!
import { UserValidationSchema } from '~/user/user.schema'
const LoginValidationSchema = UserValidationSchema.pick({
email: true,
password: true,
})
Hello, this is a correct approach indeed. After calling .mongoose()
on a zod schema you can't use zod methods anymore because it returns a non-zod object.
Hello!
In a normal
z.object
, we can use .pick, .extend, and .omit. I usually useconst LoginSchema = UserSchema.pick({email: true, password: true})
andconst RegisterSchema = UserSchema.omit({id: true})
.But after using
z
frommongoose-zod
, I can't do that anymore.Any info will be appreciated.