P
Prisma4d ago
Arky

Validate Model

Problematic I would like to use Prisma.validator (or something else provided by Prisma) in order to validate a model Context
import { Prisma, Model1, Model2, Model3 } from "@prisma/client";
import { prisma } from "@/libraries/prisma";

type Multiple = Model1 | Model2 | Model3;

const search = async (query: string): Promise<Multiple[]> => {
const model1 = await prisma.model1.findMany({ where: { name: { contains: query } } });
const model2 = await prisma.model2.findMany({ where: { name: { contains: query } } });
const model3 = await prisma.model3.findMany({ where: { name: { contains: query } } });

return [...model1, ...model2, ...model3];
}

const displayData = (model: Multiple) => {
// How can i use the validator there (or something else) to validate the type of my model?
if (model Prisma.validator) return "model1";
if (model Prisma.validator) return "model2";
if (model Prisma.validator) return "model3";
}

const response = await search("hello world");

const data = displayData(response);
// do something with data
import { Prisma, Model1, Model2, Model3 } from "@prisma/client";
import { prisma } from "@/libraries/prisma";

type Multiple = Model1 | Model2 | Model3;

const search = async (query: string): Promise<Multiple[]> => {
const model1 = await prisma.model1.findMany({ where: { name: { contains: query } } });
const model2 = await prisma.model2.findMany({ where: { name: { contains: query } } });
const model3 = await prisma.model3.findMany({ where: { name: { contains: query } } });

return [...model1, ...model2, ...model3];
}

const displayData = (model: Multiple) => {
// How can i use the validator there (or something else) to validate the type of my model?
if (model Prisma.validator) return "model1";
if (model Prisma.validator) return "model2";
if (model Prisma.validator) return "model3";
}

const response = await search("hello world");

const data = displayData(response);
// do something with data
Solutions I know i can override the Model in order to pass the name of the model but would really like to make it the cleanest possible and use only Prisma. I also know you can create type guard functions using the uniques fields of my model but same thing not as clean as using a Prisma function. ex:
const isModel1 = (model: Multiple): model is Model1 => {
return (model as Model1).model1UniqueField !== undefined; // Replace with a real field that exists only in Model1
}
const isModel1 = (model: Multiple): model is Model1 => {
return (model as Model1).model1UniqueField !== undefined; // Replace with a real field that exists only in Model1
}
Resources - https://github.com/prisma/prisma/issues/3528 Thank you for your time if you help me with this :prismasmile:
Solution:
Finally created a function that answers this question in case anyone needs it ```ts export type GetModel<T extends Prisma.ModelName> = Exclude< Awaited<ReturnType<PrismaClient[Uncapitalize<T>]["findUnique"]>>, null...
Jump to solution
1 Reply
Solution
Arky
Arky20h ago
Finally created a function that answers this question in case anyone needs it
export type GetModel<T extends Prisma.ModelName> = Exclude<
Awaited<ReturnType<PrismaClient[Uncapitalize<T>]["findUnique"]>>,
null
>;

export const validateModel = <T extends Prisma.ModelName>(
model: T,
data: GetModel<Prisma.ModelName>
): data is GetModel<T> => {
const prismaModelEnum = Prisma[`${model}ScalarFieldEnum`];
return Object.keys(prismaModelEnum)
.map((v) => v in data)
.every((v) => v);
};
export type GetModel<T extends Prisma.ModelName> = Exclude<
Awaited<ReturnType<PrismaClient[Uncapitalize<T>]["findUnique"]>>,
null
>;

export const validateModel = <T extends Prisma.ModelName>(
model: T,
data: GetModel<Prisma.ModelName>
): data is GetModel<T> => {
const prismaModelEnum = Prisma[`${model}ScalarFieldEnum`];
return Object.keys(prismaModelEnum)
.map((v) => v in data)
.every((v) => v);
};
Example usage:
import { Prisma, Model1, Model2, Model3 } from "@prisma/client";
import { prisma } from "@/libraries/prisma";

type Multiple = Model1 | Model2 | Model3;

const search = async (query: string): Promise<Multiple[]> => {
const model1 = await prisma.model1.findMany({ where: { name: { contains: query } } });
const model2 = await prisma.model2.findMany({ where: { name: { contains: query } } });
const model3 = await prisma.model3.findMany({ where: { name: { contains: query } } });

return [...model1, ...model2, ...model3];
}

const displayData = (model: Multiple) => {
// How can i use the validator there (or something else) to validate the type of my model?
if (validateModel("Model1", model)) return "model1";
if (validateModel("Model2", model)) return "model2";
if (validateModel("Model3", model)) return "model3";
}

const response = await search("hello world");

const data = displayData(response);
// do something with data
import { Prisma, Model1, Model2, Model3 } from "@prisma/client";
import { prisma } from "@/libraries/prisma";

type Multiple = Model1 | Model2 | Model3;

const search = async (query: string): Promise<Multiple[]> => {
const model1 = await prisma.model1.findMany({ where: { name: { contains: query } } });
const model2 = await prisma.model2.findMany({ where: { name: { contains: query } } });
const model3 = await prisma.model3.findMany({ where: { name: { contains: query } } });

return [...model1, ...model2, ...model3];
}

const displayData = (model: Multiple) => {
// How can i use the validator there (or something else) to validate the type of my model?
if (validateModel("Model1", model)) return "model1";
if (validateModel("Model2", model)) return "model2";
if (validateModel("Model3", model)) return "model3";
}

const response = await search("hello world");

const data = displayData(response);
// do something with data
Want results from more Discord servers?
Add your server