Skip to content

Pull Request: Implement Comprehensive Input Validation (#117)#171

Merged
LaGodxy merged 2 commits into
MettaChain:mainfrom
JerryIdoko:feature/dto-input-validation-117
Mar 26, 2026
Merged

Pull Request: Implement Comprehensive Input Validation (#117)#171
LaGodxy merged 2 commits into
MettaChain:mainfrom
JerryIdoko:feature/dto-input-validation-117

Conversation

@JerryIdoko
Copy link
Copy Markdown
Contributor

📝 Description
This PR addresses the lack of input validation across our API endpoints. By implementing strict DTO (Data Transfer Object) validation, we ensure that all incoming requests are sanitized and conform to our expected data types and business constraints before reaching the service layer.

🎯 Key Changes
Global Validation Pipe: Integrated a global ValidationPipe to automatically intercept and validate all incoming requests.

Declarative DTOs: Enhanced existing DTOs with class-validator decorators (e.g., @IsString(), @isemail(), @min(0)).

Custom Business Validators: Created a custom decorator to validate PropChain-specific logic, such as ensuring property ownership IDs match the required format.

Error Formatting: Implemented a standardized error response structure so the frontend receives clear, actionable feedback when a field fails validation.

💻 Implementation Snippet (NestJS/DTO)
Example of the new validation rules applied to a property listing:

TypeScript
import { IsString, IsNumber, Min, IsEnum, IsNotEmpty } from 'class-validator';

export class CreatePropertyDto {
@IsString()
@isnotempty()
title: string;

@IsNumber()
@min(1000) // Minimum investment threshold
price: number;

@IsEnum(['Residential', 'Commercial', 'Industrial'])
propertyType: string;
}
✅ Acceptance Criteria Checklist
[x] Comprehensive Validation: All major DTOs (Auth, Property, Investment) now have type and constraint checks.

[x] Custom Rules: Implemented a validator for checking blockchain address formats.

[x] Graceful Error Handling: API now returns 400 Bad Request with a detailed message array instead of internal server errors.

[x] Stripping Non-Whitelisted Properties: Configured the pipe to strip any properties not explicitly defined in the DTO (prevents mass-assignment vulnerabilities).

🚀 How to Verify
Start the Server: npm run start:dev

Trigger a Validation Failure: Send a POST request to /properties with an empty title or negative price.

Confirm Response: Verify the API returns a structured error like:

JSON
{
"statusCode": 400,
"message": ["price must not be less than 1000"],
"error": "Bad Request"
}
🔗 Linked Issues
Closes #117

@LaGodxy LaGodxy merged commit 775d2b9 into MettaChain:main Mar 26, 2026
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing Input Validation

2 participants