Global Validation

Validate DTOs and business rules with class-validator decorators and Prisma transactions.

1|Updated Dec 4, 2024
One-click install
npx skills add https://github.com/imkdw/imkdw-dev --skill global-validation-imkdw
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Global Validation
Source: https://github.com/imkdw/imkdw-dev/tree/main/.claude/skills/global-validation
Command: npx skills add https://github.com/imkdw/imkdw-dev --skill global-validation-imkdw

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Unvalidated or improperly validated user input can lead to security vulnerabilities, data corruption, and application crashes, undermining trust and stability. This Skill enforces robust server-side validation to protect your application and data.

Core Features & Use Cases

  • DTO Validation: Apply class-validator decorators for declarative and type-safe input validation on Data Transfer Objects (DTOs).
  • Business Rule Validation: Implement dedicated validator service classes for complex existence, uniqueness, and cross-field business rule checks.
  • Fail-Fast Approach: Integrate validation at the earliest possible stage (e.g., at the start of use cases) to prevent unnecessary processing and provide immediate feedback.
  • Use Case: When a user submits a form to create an article, this Skill ensures the title is not empty, within length limits, and unique before any database operations are attempted, preventing invalid data from reaching the persistence layer.

Quick Start

Help me add validation decorators to a CreateUserDto to ensure the email field is a valid email format and the password meets minimum length requirements, using class-validator.

Frequently Asked Questions about Global Validation

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate user input in NestJS before it reaches my database?

Input validation in NestJS happens at the DTO layer using class-validator decorators. Apply decorators like @IsNotEmpty, @IsEmail, and @MaxLength to DTO properties, then use NestJS pipes to validate incoming requests automatically before business logic executes, preventing invalid data from reaching your persistence layer.

What's the best way to enforce business rules like email uniqueness during validation?

Implement dedicated validator service classes that check existence and uniqueness constraints against your database. Execute these checks early in your use case logic—before Prisma transactions—to fail fast and avoid unnecessary processing while ensuring data integrity across your application.

Can I create custom validation decorators for TypeScript DTOs?

Yes. class-validator supports custom decorators for domain-specific rules beyond standard validators. Define custom decorators using @ValidatorConstraint and @Validate to enforce application-specific validation logic declaratively on DTO fields.

How do I prevent invalid data from corrupting my Prisma transactions?

Validate all inputs before initiating Prisma transactions. Run DTO validation and business rule checks at the start of your use case, ensuring only vetted data enters transaction scope. This fail-fast approach prevents rollbacks and maintains application stability.

Why does server-side validation matter if I already validate on the client?

Client validation is easily bypassed. Server-side validation with class-validator and custom rules is essential to prevent security vulnerabilities, data corruption, and application crashes by enforcing data integrity at the API boundary, regardless of client behavior.

Does class-validator work with TypeScript interfaces or only classes?

class-validator requires class-based DTOs because decorators only function on class instances. Use TypeScript classes with decorators for DTO validation; interfaces are compile-time only and cannot be introspected at runtime where validation occurs.