hey! how are folks handling validation? I have to ...
# orm-help
m
hey! how are folks handling validation? I have to add a validation on a date column (say, cant be more than 1 yr into the future) where `create`/`update` for that model is used all over our codebase. Any ideas for how to centralize it like a traditional ORM’s validation hooks?
f
I created js models for that. Its basically a wrapper fo the prism client calls. If you need to validate stuff, you can write some validators and run them over the respons in your desired cases. If invalid, you can either update with a correct date or throw errors. Should be close to what you need.
m
cc @Danil Kolesnikov
@febreeze i see - your solution is to wrap prisma with functions and do the validation there. but the downside i see is you cant always enforce people using the functions, no?
f
kind of, my whole programm runs trough those models. you also cant stop devs from making plain queries 😄 I think its a matter of how you implement it
I’ve written a DAL that handles those models and also exports them at top level. so ->
import DAl from 'logic/Dal'; DAL.models.user.autenticate();
Makes things fairly easy to handle.
h
If you are using
nexus
, you can try our plugin. https://github.com/prisma-korea/nexus-validation-plugin
m
validation on the graphql layer works for frontend->api validation, but doesnt help with validation on operations happening in the api. for example, background jobs
👍 1
h
understood!
m
Thank you fit the suggestion!
d
@Ryan Can you please chime in? What would be a way to add SQL like constraint on a column via Prisma API?
m
You can try middleware https://www.prisma.io/docs/concepts/components/prisma-client/middleware Downside is it will run on every query, read the last paragraph for performance considerations. I assume it might be too draining on performance for you
👍 1
r
To add SQL like constraints, you need to do it via Migrate at the moment. 1. Create a migration using
prisma migrate dev --create-only
2. Add the required constraint in the generated
.sql
file 3. Apply the migration using
prisma migrate dev