0
0
NestJSframework~20 mins

class-transformer usage in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
class-transformer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this class-transformer code?
Given the following NestJS code using class-transformer, what will be the output of the console.log?
NestJS
import { plainToInstance } from 'class-transformer';

class User {
  id: number;
  name: string;
  password: string;
}

const plainUser = { id: 1, name: 'Alice', password: 'secret' };
const userInstance = plainToInstance(User, plainUser, { excludeExtraneousValues: true });
console.log(userInstance);
AUser { id: 1, name: 'Alice', password: 'secret' }
BUser { id: 1, name: 'Alice' }
C{ id: 1, name: 'Alice', password: 'secret' }
DUser { id: 1 }
Attempts:
2 left
💡 Hint
Check if any decorators like @Expose or @Exclude are used to filter properties.
🧠 Conceptual
intermediate
1:30remaining
What does the @Exclude decorator do in class-transformer?
In class-transformer, what is the effect of adding the @Exclude() decorator to a class property?
AIt makes the property required during transformation.
BIt prevents the property from being included when transforming class instances to plain objects.
CIt renames the property during transformation.
DIt automatically validates the property value.
Attempts:
2 left
💡 Hint
Think about hiding sensitive data when sending responses.
Hyperparameter
advanced
2:30remaining
Which option correctly enables transformation of nested objects with class-transformer?
You have a User class with an Address property which is another class. Which option correctly transforms nested plain objects into class instances?
NestJS
import { plainToInstance } from 'class-transformer';

class Address {
  street: string;
  city: string;
}

class User {
  name: string;
  address: Address;
}

const plainUser = {
  name: 'Bob',
  address: { street: '123 Main St', city: 'Townsville' }
};

const userInstance = plainToInstance(User, plainUser, OPTIONS_HERE);
console.log(userInstance.address instanceof Address);
A{ strategy: 'excludeAll' }
B{ excludeExtraneousValues: true }
C{ enableImplicitConversion: true }
D{ exposeDefaultValues: true }
Attempts:
2 left
💡 Hint
Look for the option that allows automatic conversion of nested objects.
Metrics
advanced
2:00remaining
What is the effect of using @Transform decorator in class-transformer?
Consider this code snippet using @Transform on a property. What does the @Transform decorator do?
NestJS
import { Transform, plainToInstance } from 'class-transformer';

class Product {
  @Transform(({ value }) => value.toUpperCase())
  name: string;
}

const plainProduct = { name: 'laptop' };
const productInstance = plainToInstance(Product, plainProduct);
console.log(productInstance.name);
AIt converts the name property to uppercase during transformation.
BIt validates the name property to be uppercase.
CIt excludes the name property from the output.
DIt renames the name property to 'NAME'.
Attempts:
2 left
💡 Hint
Check what the function inside @Transform does to the value.
🔧 Debug
expert
3:00remaining
Why does this class-transformer code fail to exclude the password property?
Given this code, why is the password property still present in the output?
NestJS
import { plainToInstance, Exclude } from 'class-transformer';

class User {
  id: number;
  name: string;
  @Exclude()
  password: string;
}

const plainUser = { id: 1, name: 'Eve', password: '1234' };
const userInstance = plainToInstance(User, plainUser, { excludeExtraneousValues: true });
console.log(userInstance);
ABecause password property is not defined in the class.
BBecause @Exclude decorator is missing parentheses.
CBecause plainToInstance does not support excludeExtraneousValues option.
DBecause excludeExtraneousValues only works if properties have @Expose decorator, not @Exclude.
Attempts:
2 left
💡 Hint
Check how excludeExtraneousValues interacts with @Expose and @Exclude decorators.