Free & open source — no account required
Free & open source — no account required
This technical guide provides an in-depth analysis of the json to nestjs dto engine, best practices for implementation, and data security standards.
NestJS relies on Data Transfer Objects (DTOs) backed by class-validator and class-transformer to validate incoming requests. Hand-writing these classes for every endpoint is tedious and error-prone—a single mistyped decorator can let bad data reach your service layer. TypeMorph infers the shape from a real JSON sample and generates a complete, decorator-annotated DTO in seconds.
// Input JSON (POST /orders body)
{
"order_id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"customer_email": "buyer@example.com",
"total_amount": 12800.50,
"item_count": 3,
"is_gift": false,
"note": null,
"items": [
{ "product_id": "p001", "name": "Widget", "quantity": 2 }
]
}
// Generated NestJS DTO
import { IsUUID, IsEmail, IsNumber, IsInt, IsBoolean, IsOptional,
IsNotEmpty, IsArray, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';
export class OrderItemDto {
@IsNotEmpty()
product_id!: string;
@IsNotEmpty()
name!: string;
@IsInt()
quantity!: number;
}
export class OrderDto {
@IsUUID()
order_id!: string;
@IsEmail()
customer_email!: string;
@IsNumber()
total_amount!: number;
@IsInt()
item_count!: number;
@IsBoolean()
is_gift!: boolean;
@IsOptional()
note?: string | null;
@IsArray()
@ValidateNested({ each: true })
@Type(() => OrderItemDto)
items!: OrderItemDto[];
}
1. Install dependencies: npm install class-validator class-transformer
2. Enable decorators in tsconfig.json: Set "experimentalDecorators": true and "emitDecoratorMetadata": true.
3. Enable the ValidationPipe globally in your main.ts: app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true })).
4. Paste your JSON sample into TypeMorph, select NestJS DTO, and copy the output.
5. Review and refine: Add @Min()/@Max() bounds for numbers, @Length() for strings, or @IsEnum() for known literal fields.
TypeMorph analyses the JSON value, not just the key name, to pick the right decorator. A string that matches a UUID v4 pattern gets @IsUUID(). An RFC-5322 email address gets @IsEmail(). An ISO 8601 timestamp gets @IsISO8601(). Integer values get @IsInt() while floats get @IsNumber(). Nested objects become separate DTO classes linked with @ValidateNested({ each: true }) and @Type()—the combination required for class-transformer to recursively instantiate and validate them.
class-validator shines in decorator-heavy NestJS projects where DTOs double as OpenAPI schema sources via @nestjs/swagger. Zod is more portable and requires no tsconfig flags, making it easier to share between frontend and backend. tRPC eliminates the DTO layer entirely if you control both ends of the API. Choose DTOs when you need @nestjs/swagger auto-documentation or when your team is already invested in the class-based NestJS style.
whitelist: true: The ValidationPipe strips unknown properties, preventing mass-assignment vulnerabilities.CreateOrderDto for input and a OrderResponseDto for output keeps your surface area explicit.@Transform() for coercion: Query parameters arrive as strings; @Transform(({ value }) => Number(value)) handles the conversion before validation runs.@ApiProperty() alongside validators: @nestjs/swagger reads decorator metadata to generate OpenAPI docs automatically—document constraints like minimum and example in the same decorator.Q: Why does the generated DTO use !: string (definite assignment assertion)?
A: NestJS DTOs are instantiated by class-transformer which bypasses TypeScript constructors. The ! tells the TypeScript compiler the property is always assigned at runtime, avoiding false "possibly undefined" errors.
Q: Can I use this with @nestjs/swagger?
A: Yes. Add @ApiProperty() from @nestjs/swagger above each property. The validation decorators and the Swagger decorator coexist without conflict.
Q: How are nullable fields handled?
A: Fields that were null in the JSON sample become @IsOptional() with a type of string | null. If a field can be either absent or null, both @IsOptional() and @ValidateIf(o => o.field !== null) may be appropriate.
Is the processing local-only?
Absolutely. TypeMorph operates entirely within your browser's sandbox. We use Web Workers for high-performance computation without ever transmitting your JSON, SQL, or API data to a remote server.
Can I use this for enterprise projects?
Yes. The tool is designed for professional software engineers who require GDPR compliance and data privacy. It is trusted by developers at top-tier startups and financial institutions.