Skip to content

project rulesBackend and APIsnodetypescript

NestJS Rules

Project rules for NestJS: feature modules, injectable providers, class-validator DTOs, guards, typed ConfigService and supertest e2e tests.

npx workspacetemplates add nestjs-rules --tool claude-code

Writes 1 file to your project root

CLAUDE.mdadd to your file26 lines · 1.5 KBFile
# Project rules: NestJS

## Modules
- One module per feature under `src/<feature>/`: module, controller, service, `dto/`, `entities/`. Export from a module only what others need and import the module, never the provider file.
- Avoid `forwardRef`; a circular dependency means the design needs a third module or an event.

## Controllers and DTOs
- Controllers parse and delegate; services hold logic. Return plain objects or serialized DTOs, never persistence entities with secrets.
- Define input as classes with `class-validator` decorators. Enable a global `ValidationPipe` with `whitelist`, `forbidNonWhitelisted` and `transform`.
- Use `ParseUUIDPipe` and friends for path parameters.

## Auth and errors
- Protect routes with guards (`@UseGuards`) and read roles with a custom decorator and `Reflector`. Keep global guards for authentication.
- Throw built-in HTTP exceptions from services only when meaningful; map domain errors in an exception filter that returns one JSON shape.

## Config
- Use `@nestjs/config` with a validated schema and inject typed config through `ConfigService`. Do not read `process.env` in providers.

## Data
- Inject repositories (TypeORM or Prisma service) into services. Use migrations, not `synchronize: true`.

## Tests
- Unit-test services with `Test.createTestingModule` and mocked providers. Write e2e tests under `test/` with supertest against `createNestApplication`.

## Before finishing
Run `npm run lint`, `npm run build`, `npm test` and `npm run test:e2e`.

What it does

NestJS brings a module system and decorators, and an agent can easily bypass both by importing services across the tree or skipping validation. These rules keep the dependency graph honest.

They explain where each concern belongs: pipes validate, guards authorize, interceptors shape output, filters format errors.

Example

you › Create a tags module with list and create endpoints

agent › The agent scaffolds a tags module with a DTO, validation pipe coverage, a service on the repository, an exported provider only where needed, and an e2e test for both routes.

Questions

Does it choose an ORM?
No. The data rules say how to inject and migrate, so TypeORM, Prisma or MikroORM can all follow them.
Why forbid synchronize: true?
It alters tables from entity changes at startup, which can drop columns in production. Migrations make the change reviewable.
  • ASP.NET Core Web API Rules

    Project rulesdotnet

    Free
  • Django Rules

    Project rulespython

    Free
  • Flask Rules

    Project rulespython

    Free
  • Go Service Rules

    Project rulesgo

    Free