When developing applications with Typeform and Nest.js, using regular expressions for data validation is an effective method to ensure that user input data conforms to the expected format. Below, I will explain how to set up regular expression validation in both Typeform and Nest.js.
1. Setting up Regular Expression Validation in Typeform
In Typeform, regular expressions can enhance form validation capabilities. For instance, to validate that user input represents a valid email address, you can configure a regular expression within the corresponding text input field.
Steps:
- Log in to your Typeform account and open your form.
- Select or add a 'Text' question to collect email addresses.
- In the question settings, locate the 'Validations' option and click it.
- Choose 'Add a new rule', then in the condition configuration, select 'Text'.
- Enter the relevant email validation regular expression in the 'matches regex' field, such as
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$. - Save the form after completing the configuration.
This approach ensures that Typeform automatically prompts users to re-enter input when it does not conform to the regular expression format, thereby guaranteeing data accuracy.
2. Setting up Regular Expression Validation in Nest.js
In Nest.js applications, you can implement regular expression validation using the class-validator library. For example, to verify that user-provided phone numbers match a specific format.
Steps:
- First, ensure your project has installed
class-validatorandclass-transformer.
bashnpm install class-validator class-transformer
- Define a DTO (Data Transfer Object) and apply regular expression validation using the
@IsStringand@Matchesdecorators.
typescriptimport { IsString, Matches } from 'class-validator'; export class CreateUserDto { @IsString() name: string; @IsString() @Matches(/^\(\+\d{1,3}\s?\)?\d{10}$/ , { message: 'Phone number must be in a valid format', }) phone: string; }
Here, the @Matches decorator ensures the phone field adheres to a specific phone number format. If validation fails, it returns a custom error message.
- In your Nest.js controller, utilize this DTO and ensure
ValidationPipeis applied globally or locally.
typescriptimport { Body, Controller, Post, UsePipes, ValidationPipe } from '@nestjs/common'; import { CreateUserDto } from './create-user.dto'; @Controller('users') export class UsersController { @Post() @UsePipes(new ValidationPipe()) createUser(@Body() createUserDto: CreateUserDto) { return 'User created successfully!'; } }
With ValidationPipe, Nest.js automatically handles input validation and throws exceptions for invalid data, safeguarding your application from malformed inputs.
Summary
Through the examples provided for Typeform and Nest.js, regular expressions emerge as a powerful tool for validating user input. In Typeform, this is primarily achieved through form configuration, while in Nest.js, it is implemented via the class-validator library for data validation. Selecting the appropriate implementation based on your application's requirements can significantly improve robustness and user experience.