NestJS · Senior · Question 42 of 75
How do you prevent mass assignment when updating entities in Nest?
- 1Mark all entity fields as readonly in TypeScript
- 2Use
Object.assign(entity, req.body) - ✓Correct answer: Accept a DTO with only allowed fields, use whitelist validation, and copy fields explicitly instead of spreading the body into the entity
Explanation
TypeScript types don't exist at runtime; only validation and explicit mapping stop extra fields.