question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add runtime validation to models

See original GitHub issue

Problem

Before saving data to the database, it’s common to perform validation.

When using Prisma to save user data (in an API), Prisma will perform runtime validation before saving to the database and throw an error if the user data doesn’t match the type.

Even with TypeScript, these checks cannot happen at compile time because the user data is not known.

Since there are situations where you want to validate the data as a separate step to saving to the database, it’d be nice if Prisma exposed the same validation functionality as a standalone method on models.

Suggested solution

Provide a validate function for models which is based on the Prisma schema:

model User {
  id        Int     @default(autoincrement()) @id
  email     String  @unique
  firstName String?
  lastName  String?
  social    Json?
  isAdmin   Boolean @default(false)
}

API ideas:

const { isValid, error } = prisma.user.validate(user)

The idea is that If the input is valid, then the error will be undefined. If the input is invalid, error is assigned an Error object providing more information.

OR

const user = { firstName: 'Laura' }
try {
  const validatedUser = await prisma.user.validate(user)
} catch (error) {
  // handle error
}

Additional context

For inspiration, Joi’s validation errors might be useful (albeit more extensive as Joi has richer validation rules).

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:245
  • Comments:36 (4 by maintainers)

github_iconTop GitHub Comments

29reactions
GCastilhocommented, May 9, 2021

The ideas presented in #3102 seems really good, like:

directive Email {
  type String
  validate <email regex here>
}

directive Salary {
  type Number
  validate v => v > 0
}

Then being used like this:

model User {
  id    Int @id
  email String @unique @email
  salary Number @salary
}

An implicit conversion, say, a numeric string to a number (like mongoose does) would also be nice to prevent from manually having to cast the values or being too strict about types that JS considers “interchangeable” in some cases

18reactions
orenmizrcommented, Jan 16, 2021

so… until that happens… what is the best way to do model validation today with prisma 2 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically apply validation rules at runtime with ASP ...
suppose i want to add validation at run time dynamically. suppose my model has one property called name and name has one validation...
Read more >
Dynamically apply validation rules at runtime with ASP.NET ...
On the model we have a List<RequiredField> which is basically a class with two strings, one for the field name and one for...
Read more >
How to Validate Your Data with Custom Validators of Pydantic ...
When we have multiple models for which the same validator can apply. We can set allow_reuse to be True so the same validator...
Read more >
Data Validation - 2.x - CakePHP Cookbook
This last example shows how validation rules can be added to model fields. For the login field, only letters and numbers will be...
Read more >
Spring MVC Custom Validation - Baeldung
Creating a custom validator entails rolling out our own annotation and using it in our model to enforce the validation rules. So let's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found