Arky
Arky
PPrisma
Created by Arky on 10/18/2024 in #help-and-questions
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:
4 replies