Form Validation
Form Validation with express-validator
In Etherial.TS, we've integrated the popular express-validator library for robust and customizable form validation. This integration simplifies the process of validating incoming data from HTTP requests, ensuring that your application handles user input with precision and accuracy.
Creating Forms
To manage and organize form validation logic, we introduce the concept of Forms in Etherial.TS. Forms are classes designed to validate specific HTTP request data. Each Form class corresponds to a particular model and contains validation rules for various CRUD operations. Here's an example of how to create a Form:
import { Form } from "etherial/components/http/validator";
@Form()
export class Create {}It's good practice to create a separate Form class for each model. For instance, if you have a User model, you should create a UserForm in a file like src/forms/user_form.ts. Each Form class can handle different operations like Create, Update, Read, and Delete.
Middleware for Form Validation
Etherial.TS simplifies the validation process by providing decorators to attach validation logic to your route handlers. You can use the @ShouldValidateForm decorator to specify which Form should be used for validation in a route handler:
import { ShouldValidateForm } from "etherial/components/http/validator";
import * as UserForm from "./../forms/user_form";
@Post("/users")
@ShouldValidateForm(UserForm.CreateUserForm)
public async createUser() {}The @ShouldValidateForm decorator ensures that the request data (req.body) is validated according to the rules defined in the specified Form class.
Form Validation Rules
Etherial.TS offers a wide range of validation decorators that you can use within your Form classes to define validation rules. Here's a list of available validation decorators and their descriptions:
@Body()
Will validate everything coming from req.body
@Query()
Will validate everything coming from req.query
-
-
@ShouldExist()
Should exist
@ShouldBeNotEmpty()
Should not be empty
@ShouldMatch(regex: RegExp)
Should match the regex on the param
@ShouldBeEmail(options: any)
Should be email with an email normalization
@ShouldBeEqualTo(field: string)
Should be equal to an other field
@ShouldBeISO8601Date()
Should be an ISO8601 Date
@ShouldHaveMinMaxLength(min: number, max: number)
Should have min and max length
@ShouldHaveMinLength(min: number)
Should have min length
@ShouldHaveMaxLength(max: number)
Should have max length
@ShouldBeMobilePhone(locale: string)
Should be a mobile phone depending of the locale set in th eparam, exemple: "fr"
@ShouldExistInModel(model: any, column: string)
Should exist in the model set on the first param, exemple: @ShouldExistInModel(Address, "address_id") we're cheking if the current property (can be anything from @Query or @Body) is existing in the Address model on the column address_id exemple of request Address.findOne({where: { address_id: req.body.address_id }})
@ShouldNotExistInModel(model: any, column: string)
Should not exist in the model set on the first param, exemple: @ShouldNotExistInModel(User, "email") we're cheking if the current property (can be anything from @Query or @Body) is existing in the User model on the column email exemple of request User.findOne({where: { email: req.body.email }})
@ShouldCustom(cb: (value: string, req: Request) => Promise<never>)
Should validate a custom function by returning a Promise.reject or a Promise.resolve
Example
Best Practices
In Etherial.TS, it's recommended to have one Form class per model, and within each Form, include different CRUD operation validation logic. This approach mirrors the REST API's logic but within the Form structure. By following this practice, you ensure that your application's input data is thoroughly validated for each specific use case.
With express-validator integration and Etherial.TS's validation decorators, you can confidently handle form validation in your application, ensuring data integrity and security.
Last updated
Was this helpful?