P
Prisma•3mo ago
Uncle

Validator questions

I'm trying to operate partial type of my User model as I omit the passwordHash field at instantiation. When I follow the docs I end up with the select included in the type, this is from my sveltekit app:
import { Prisma, type User } from 'prisma/prisma-client'
const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({
select:{ email: true, givenName: true }
})
export let nameBlock: typeof partialUser;
import { Prisma, type User } from 'prisma/prisma-client'
const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({
select:{ email: true, givenName: true }
})
export let nameBlock: typeof partialUser;
Property 'givenName' does not exist on type '{ select: { email: true; givenName: true; }; }'.ts(2339)
Solution:
Can you try this code? ```js import { Prisma } from '@prisma/client' const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({...
Jump to solution
5 Replies
RaphaelEtim
RaphaelEtim•3mo ago
Hi @Uncle 👋 Can you please share the User model? Does givenName field exist on the User model?
Uncle
UncleOP•3mo ago
my User model:
model User {
id String @id @default(cuid()) @map("id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
email String? @unique
givenName String?
familyName String?
organizationName String?
emailVerified Boolean? @map("email_verified")
passwordHash String? @map("password_hash")
employee Boolean @default(false)
admin Boolean @default(false)
customerLeases Lease[] @relation(name: "customer")
employeeLeases Lease[] @relation(name: "employee")
contactInfo ContactInfo[]
paymentMade PaymentRecord[] @relation(name: "customer")
paymentReceived PaymentRecord[] @relation(name: "employee")
customerInvoices Invoice[]
session Session[]
verification Verification[]

@@unique([id, email])
@@unique([email, givenName, familyName])
@@index([id, email])
@@map("users")
}
model User {
id String @id @default(cuid()) @map("id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
email String? @unique
givenName String?
familyName String?
organizationName String?
emailVerified Boolean? @map("email_verified")
passwordHash String? @map("password_hash")
employee Boolean @default(false)
admin Boolean @default(false)
customerLeases Lease[] @relation(name: "customer")
employeeLeases Lease[] @relation(name: "employee")
contactInfo ContactInfo[]
paymentMade PaymentRecord[] @relation(name: "customer")
paymentReceived PaymentRecord[] @relation(name: "employee")
customerInvoices Invoice[]
session Session[]
verification Verification[]

@@unique([id, email])
@@unique([email, givenName, familyName])
@@index([id, email])
@@map("users")
}
My client instantiation
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient({
omit:{
user: {
passwordHash: true
}
}
})
export default prisma
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient({
omit:{
user: {
passwordHash: true
}
}
})
export default prisma
Solution
RaphaelEtim
RaphaelEtim•3mo ago
Can you try this code?
import { Prisma } from '@prisma/client'

const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({
select: { email: true, givenName: true }
})

// Create a type-safe object using Prisma.UserGetPayload
type PartialUser = Prisma.UserGetPayload<typeof partialUser>

// Example usage in your SvelteKit app
export let nameBlock: PartialUser
import { Prisma } from '@prisma/client'

const partialUser = Prisma.validator<Prisma.UserDefaultArgs>()({
select: { email: true, givenName: true }
})

// Create a type-safe object using Prisma.UserGetPayload
type PartialUser = Prisma.UserGetPayload<typeof partialUser>

// Example usage in your SvelteKit app
export let nameBlock: PartialUser
You may need regenerate your Prisma client or restart your typescript server in your IDE
Dkyc
Dkyc•3mo ago
hello. try it like this
const postWithUser = Prisma.validator<Prisma.PostDefaultArgs>()({
include: {
User: true
}
});
export type PostWithUser = Prisma.PostGetPayload<typeof postWithUser>;
const postWithUser = Prisma.validator<Prisma.PostDefaultArgs>()({
include: {
User: true
}
});
export type PostWithUser = Prisma.PostGetPayload<typeof postWithUser>;
i didn't see you already answered Raphael. sry 😅 i copy pasted from another question i answered that was asking about the same thing
Uncle
UncleOP•3mo ago
That worked. Can somebody explain what I was missing / why that worked? Thanks by the way
Want results from more Discord servers?
Add your server