Reusable field selections
Define a reusable UserSelect object for multiple Prisma Client queries.
Prisma · Function
Prisma.validator checks an object against a generated Prisma type and returns the type-safe object. It is commonly used to build reusable, schema-aware query parameters.
Prisma.validator is a type-level helper that receives a generated Prisma type and returns an object checked against that type. The resulting object can be reused as query arguments, such as a model selection, while preserving the generated field constraints.
Prisma.validator<GeneratedType>({ args });| Name | Type | Description |
|---|---|---|
GeneratedType | generated Prisma type | Generated Prisma type that constrains the object, such as Prisma.UserSelect. |
args | object | Object whose properties are checked against the generated type. |
A type-safe object conforming to the supplied generated Prisma type.
import { Prisma } from "@prisma/client";
const userEmail = Prisma.validator<Prisma.UserSelect>()({
email: true,
});
// Use with a Prisma Client query:
const user = await prisma.user.findUnique({
where: { id: 1 },
select: userEmail,
});Creates a reusable, type-checked selection object and passes it to a Prisma Client query.
Define a reusable UserSelect object for multiple Prisma Client queries.
Create typed include or select arguments before deriving a payload type.
Validate query argument objects against generated model-specific types.
Prisma.validator takes a generated Prisma type and checks an object against that type. For example, Prisma.validator<Prisma.UserSelect>()({ email: true }) creates a reusable, type-safe selection object.
Follow-up: How would you avoid this helper in modern TypeScript? Use the native satisfies operator to check an object against the generated Prisma type, when suitable.
It can define reusable query parameters while ensuring that selected or included fields match the generated model types. The returned object can then be supplied to a Prisma Client query.
Follow-up: What is another documented usage style? A selector can be created from an existing Prisma Client instance, rather than only from a generated type.
The documented generic form is Prisma.validator<GeneratedType>({ args });, where GeneratedType is a generated Prisma type and args is the object being checked. A common example uses Prisma.UserSelect.
Follow-up: What limitation affects generator choice? The API is unavailable with the new prisma-client generator and is limited to prisma-client-js.
Prisma.UserSelectPrisma.UserDefaultArgsPrisma.UserGetPayloadTypeScript satisfies operator