targetPlatforms JSON field accepted any input, causing potential downstream posting failures.
-
Custom Validator
@IsValidPlatforms()- Created in
src/videos/validators/is-valid-platforms.validator.ts - Validates array of supported platforms
- Provides detailed error messages
- Created in
-
Supported Platforms
tiktokinstagramyoutube-shortsyoutubefacebooktwittersnapchat
-
Automatic Normalization
- Converts to lowercase using
@Transformdecorator - Deduplicates using
Set - Applied in both CreateVideoDto and UpdateVideoDto
- Converts to lowercase using
-
Returns 400 on Invalid Values
- Validation integrated with class-validator
- NestJS automatically returns 400 Bad Request
- Includes detailed error messages listing invalid platforms
src/videos/validators/is-valid-platforms.validator.ts- Validator constraintsrc/videos/validators/decorators.ts-@IsValidPlatforms()decoratorsrc/videos/dto/create-video.dto.ts- Create video DTO with validationsrc/videos/dto/update-video.dto.ts- Update video DTO with validation
src/videos/validators/index.ts- Exports for validatorssrc/videos/dto/index.ts- Exports for DTOs
src/videos/validators/is-valid-platforms.validator.spec.ts- Validator unit testssrc/videos/dto/create-video.dto.spec.ts- DTO integration tests
src/videos/README.md- Complete usage guide
// Any value accepted - could cause failures
{
targetPlatforms: "invalid" // ❌ String instead of array
}
{
targetPlatforms: ["TIKTOK", "reddit"] // ❌ Invalid platform
}// Valid request
{
targetPlatforms: ["TikTok", "Instagram", "TIKTOK"]
}
// Automatically normalized to:
{
targetPlatforms: ["tiktok", "instagram"] // ✅ Lowercase + deduplicated
}
// Invalid request returns 400
{
targetPlatforms: ["tiktok", "reddit"]
}
// Response: 400 Bad Request
// "Invalid platform(s): reddit. Supported platforms: tiktok, instagram, ..."To integrate this into your video endpoints:
- Import the DTO in your controller:
import { CreateVideoDto, UpdateVideoDto } from './dto';- Use in controller methods:
@Post()
create(@Body() createVideoDto: CreateVideoDto) {
// targetPlatforms is now validated and normalized
return this.videosService.create(createVideoDto);
}
@Patch(':id')
update(@Param('id') id: string, @Body() updateVideoDto: UpdateVideoDto) {
// targetPlatforms is validated and normalized
return this.videosService.update(id, updateVideoDto);
}- Enable global validation pipe (if not already enabled):
// In main.ts
app.useGlobalPipes(new ValidationPipe({
transform: true, // Enable transformation
whitelist: true,
}));Run tests after installing dependencies:
# Install dependencies
npm install
# Run validator tests
npm test -- --testPathPattern=is-valid-platforms.validator.spec
# Run DTO tests
npm test -- --testPathPattern=create-video.dto.spec
# Run all tests
npm test- Type Safety - TypeScript types ensure compile-time safety
- Runtime Validation - Prevents invalid data from entering the system
- Automatic Normalization - Consistent data format in database
- Clear Error Messages - Developers know exactly what went wrong
- Extensible - Easy to add new platforms to the supported list
- Well Tested - Comprehensive unit and integration tests
- Install dependencies:
npm install - Run tests to verify:
npm test - Create a videos controller if it doesn't exist
- Apply the DTOs to your video endpoints
- Ensure global validation pipe is enabled in
main.ts