How to define a validation regular expression pattern for a prisma model attribute type?

Viewed 597

In prisma schema, I want to define a simple model like:

model NaturalPerson {
    id  String @@id   
    cpf String?
}

but here is the catch: I want the attribute cpf to match a certain regular expression ^[0-9]{11}$.

Is it possible to define such validation?

1 Answers

You cannot define this validation in the schema file, you can enforce it while creating records.

Before invoking the prisma create query you could use Joi to validate the value which is being inserted.

Here's how you can match your regex pattern: Joi pattern regex

Related