denwa-nest-shared
Version:
Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client
241 lines (178 loc) • 5.53 kB
Markdown
---
name: schema-definition
description: >
Defining properties in DTOs and Entities using ApiValidationProperty to unify GraphQL, Swagger, and class-validation.
Includes Paginate, DeleteDto, IdsDto, and OkRequestDto for standardizing request/response shapes.
type: core
library: denwa-nest-shared
library_version: '1.0.80'
sources:
- 'Denwa799/nest-shared:src/decorators.ts'
- 'Denwa799/nest-shared:src/dto.ts'
---
# Unified Schema Definition
Use this skill to define data structures that are automatically compatible with NestJS GraphQL (Apollo), Swagger (OpenAPI), and class-validator using a single decorator.
## Setup
Define a standard DTO or Entity using `ApiValidationProperty`.
```typescript
import { InputType } from '@nestjs/graphql';
import { ApiValidationProperty } from 'denwa-nest-shared';
@InputType()
export class CreateUserDto {
@ApiValidationProperty({
type: 'string',
description: 'User display name',
example: 'John Doe',
min: 2,
})
name: string;
@ApiValidationProperty({
type: 'string',
isEmail: true,
description: 'Login email',
})
email: string;
@ApiValidationProperty({
type: 'boolean',
isOptional: true,
})
isActive?: boolean;
}
```
## Core Patterns
### Defining Enum Properties
Map TypeScript enums to GraphQL and Swagger simultaneously.
```typescript
import { ApiValidationProperty } from 'denwa-nest-shared';
enum UserRole {
ADMIN = 'admin',
USER = 'user',
}
export class UserDto {
@ApiValidationProperty({
type: 'string',
enumObject: UserRole,
swaggerType: 'UserRole',
})
role: UserRole;
}
```
### Pagination Request Shapes
Define pagination and filter arguments using standard types:
```typescript
import { ArgsType } from '@nestjs/graphql';
import { ApiValidationProperty } from 'denwa-nest-shared';
@ArgsType()
export class GetUsersArgs {
@ApiValidationProperty({ type: 'int', isOptional: true })
page?: number;
@ApiValidationProperty({ type: 'int', isOptional: true })
limit?: number;
@ApiValidationProperty({ type: 'string', isOptional: true })
search?: string;
}
```
### Numeric and Date Types
Leverage internal type mapping for correct decorator generation.
```typescript
import { ApiValidationProperty } from 'denwa-nest-shared';
export class StatsDto {
@ApiValidationProperty({ type: 'int', min: 0 })
count: number;
@ApiValidationProperty({ type: 'float', max: 100 })
score: number;
@ApiValidationProperty({ type: 'date', isOptional: true })
lastLogin?: Date;
}
```
### Nested / Complex DTOs
`type` accepts only primitives (`string | int | float | date | boolean`). For nested objects pass the DTO
class via `objectType` (preferred) or `swaggerType` (backward compatible, including `[SomeDto]`).
The decorator then emits `@Field(() => SomeDto)`, `@ValidateNested()` and `@Type(() => SomeDto)`
instead of `IsString`, so no manual `@Field` is required.
```typescript
import { InputType } from '@nestjs/graphql';
import { ApiValidationProperty } from 'denwa-nest-shared';
@InputType()
class ImageDto {
@ApiValidationProperty({ type: 'string', max: 256 })
tempName: string;
}
@InputType()
export class CreateProductDto {
// Массив вложенных DTO
@ApiValidationProperty({
type: 'object',
objectType: ImageDto,
isArray: true,
isOptional: true,
})
images?: ImageDto[];
// Одиночный вложенный DTO
@ApiValidationProperty({
type: 'object',
objectType: ImageDto,
isOptional: true,
})
cover?: ImageDto;
}
```
Built-in element types are not treated as nested: `swaggerType: [String]` still behaves as a string array.
## Common Mistakes
### CRITICAL Using `type: 'string'` for Nested DTOs
Wrong:
```typescript
@ApiValidationProperty({ type: 'string', isArray: true })
items: ItemDto[];
```
Correct:
```typescript
@ApiValidationProperty({ type: 'object', objectType: ItemDto, isArray: true })
items: ItemDto[];
```
`type` describes the primitive GraphQL scalar. Using `string` for objects emits `IsString({ each: true })`,
which rejects every object with `each value in items must be a string`. Passing the class in
`swaggerType` (e.g. `swaggerType: [ItemDto]`) is also supported for backward compatibility.
Source: Denwa799/nest-shared:src/decorators.ts
### CRITICAL Using Separate Decorators
Wrong:
```typescript
@Field()
@ApiProperty()
@IsString()
@MinLength(2)
name: string;
```
Correct:
```typescript
@ApiValidationProperty({ type: 'string', min: 2 })
name: string;
```
Using separate decorators misses the unified type mapping and default formatting provided by the library.
Source: maintainer interview
### HIGH Mismatched Validation for Type
Wrong:
```typescript
@ApiValidationProperty({ type: 'boolean', min: 10 })
isActive: boolean;
```
Correct:
```typescript
@ApiValidationProperty({ type: 'int', min: 10 })
count: number;
```
Validation parameters like `min`/`max` only apply to numeric types. Boolean types do not support these constraints.
Source: Denwa799/nest-shared:src/decorators.ts:160
### MEDIUM Using optional instead of isOptional
Wrong:
```typescript
@ApiValidationProperty({ type: 'string', optional: true })
name?: string;
```
Correct:
```typescript
@ApiValidationProperty({ type: 'string', isOptional: true })
name?: string;
```
The property name in `ApiValidationProperty` is `isOptional`, not `optional`. Using `optional` will be ignored and the field will be treated as required.
Source: Denwa799/nest-shared:src/decorators.ts:58