Request Validation
Relevant source files
The following files were used as context for generating this wiki page:
Request validation in this project is implemented as a multi-layered security and integrity measure. It ensures that data sent from the client-side React application meets specific criteria before being processed by the .NET backend API. The system employs both client-side UI feedback and server-side business logic validation using the FluentValidation library.
The validation architecture focuses on preventing malformed data, ensuring required fields are present, and enforcing data constraints such as minimum lengths or file size limits.
Server-Side Validation Architecture
The backend utilizes FluentValidation to define strongly-typed validation rules for ViewModels and Data Transfer Objects (DTOs). This is decoupled from the controller logic, allowing for reusable and testable validation sets.
FluentValidation Implementation
For the login process, the IniciarSesionVmValidador class defines the constraints for login attempts. It checks that both the username and password are provided.
public class IniciarSesionVmValidador : AbstractValidator<IniciarSesionVm>
{
public IniciarSesionVmValidador()
{
RuleFor(i => i.UsuarioNombre).NotEmpty();
RuleFor(i => i.Contrasena).NotEmpty();
}
}
Sources: Back/Alumnos/Api/Seguridad/IniciarSesionVmValidador.cs:5-14
Controller Integration
Controllers manually invoke validators and throw a ValidationException if the model state is invalid. This interrupts the request flow before any database interaction or service calls occur.
The diagram shows the synchronous validation check within the login endpoint.
Sources: Back/Alumnos/Api/Controllers/InicioSesionController.cs:30-40
Client-Side Validation Logic
The React frontend implements immediate validation within component state handlers. This provides instant feedback to the user and prevents unnecessary network requests to the API.
Form Validation Constraints
Different modules apply specific rules based on the data type. For example, the product publication module validates file sizes for images and numeric ranges for prices.
| Module | Field | Validation Rule |
|---|---|---|
| Product | Image | Must be present and < 5MB |
| Product | Price | Must be > 0 |
| Register | Password | Min 6 characters |
| Register | Confirm Password | Must match Password |
| Login | Email/Password | Must not be empty/whitespace |
Sources: Front/src/pages/publishProduct.jsx:42-45, Front/src/pages/register.jsx:36-54, Front/src/pages/login.jsx:30-33
Validation Flow in UI Components
Components use local state (error or setError) to display alerts when validation fails during the handleSubmit event.
The sequence demonstrates the "fail-fast" approach on the frontend to improve user experience.
Sources: Front/src/pages/register.jsx:31-62, Front/src/pages/publishProduct.jsx:65-88
Data Transfer Objects (DTO) Constraints
In addition to logic-based validation, the backend uses Data Annotations to enforce required fields at the model level for user creation and modification.
| Field | Requirement | File |
|---|---|---|
| Nombre | Required | CrearUsuarioDto.cs |
| ApellidoPaterno | Required | CrearUsuarioDto.cs |
| NombreUsuario | Required | CrearUsuarioDto.cs |
| Contraseña | Required | CrearUsuarioDto.cs |
| Slug | Required | ModificarUsuarioDto.cs |
Sources: Back/Alumnos/Api/Comun/Modelos/Usuarios/CrearUsuarioDto.cs:7-19, Back/Alumnos/Api/Comun/Modelos/Usuarios/ModificarUsuarioDto.cs:9
Conclusion
Request validation in the project is a dual-layered system. The frontend handles immediate user feedback and basic data sanity checks, while the backend provides a robust defense using FluentValidation and Data Annotations to ensure that only valid, safe data is processed by the business services and persisted in the database.