From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Validating regex pattern - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Validating regex pattern
To validate regex patterns in NestJS, the class validator offers a dedicated decorator called MatchesDecorator, which is commonly used for pattern-based validation. Let's see how it works. In the AuthDTO class, let's apply the pattern for the phone property. I'll remove the max length validator, and instead I will give matches decorator. It takes a regular expression pattern as an argument. So let me define the pattern. I'll give the caret sign and in square brackets, I'll give 0 to 9 to represent the character set. So in this case, it matches any single digit from 0 to 9. Then we need a range as well for the characters set as the numbers may be repeated. So in the curly braces, I'll give 10 comma 11 as range values. So the phone number has to be exactly 10 or 11 digits. I'll add the dollar sign to mark the end of the pattern. Let's also state a message. I'll give the message property and the message as phone number must be exactly 10 or 11 digits. Let's check the postman. I'll give…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Introduction to pipes1m 59s
-
(Locked)
Built-in pipes (ParseIntPipe)2m 40s
-
(Locked)
Built-in pipes (ParseFloatPipe)1m 7s
-
(Locked)
Built-in pipes (ParseBooleanPipe)2m 25s
-
(Locked)
Built-in pipes (ParseArrayPipe)2m 59s
-
(Locked)
Built-in pipes (ParseUUIDPipe)4m 12s
-
(Locked)
Built-in pipes (ValidationPipe)5m 52s
-
(Locked)
Validating empty fields3m 34s
-
(Locked)
Validating field length3m 4s
-
(Locked)
Custom validation messages3m 2s
-
(Locked)
Validating field using @IsEnum() validator4m 7s
-
(Locked)
Validating dates in Nest.js2m 31s
-
(Locked)
Validating optional fields2m 33s
-
(Locked)
Validating regex pattern3m 11s
-
(Locked)
Creating a custom pipe6m 39s
-
(Locked)
Understanding ArgumentMetadata (metadata.type)3m 40s
-
(Locked)
Assignment: Custom pipe (handling different "type" arguments)3m 56s
-
(Locked)
Understanding ArgumentMetadata (metadata.metatype)5m
-
(Locked)
Understanding ArgumentMetadata (metadata.data)3m 37s
-
(Locked)
Implementing global pipes2m 30s
-
(Locked)
-
-
-
-
-
-
-
-
-