0
0
NestJSframework~10 mins

class-transformer usage in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to transform a plain object into a class instance using class-transformer.

NestJS
import { plainToInstance } from 'class-transformer';

class User {
  name: string;
  age: number;
}

const plainUser = { name: 'Alice', age: 25 };
const user = plainToInstance(User, [1]);
console.log(user instanceof User);
Drag options to blanks, or click blank then click option'
AplainUser
Bnew User()
C{ name: 'Bob', age: 30 }
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name instead of the object.
Passing a new instance instead of the plain object.
2fill in blank
medium

Complete the code to exclude the password field when transforming a User object to plain using class-transformer.

NestJS
import { Exclude, plainToInstance, instanceToPlain } from 'class-transformer';

class User {
  name: string;
  @Exclude()
  [1]: string;
}

const user = plainToInstance(User, { name: 'Bob', password: 'secret' });
const plainUser = instanceToPlain(user);
console.log(plainUser);
Drag options to blanks, or click blank then click option'
Asecret
Bname
Cage
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Excluding the wrong property like name or age.
Trying to exclude a value instead of a property name.
3fill in blank
hard

Fix the error in the code to properly transform nested objects using class-transformer.

NestJS
import { Type, plainToInstance } from 'class-transformer';

class Photo {
  url: string;
}

class User {
  name: string;
  @Type(() => [1])
  photos: Photo[];
}

const plainUser = {
  name: 'Alice',
  photos: [{ url: 'photo1.jpg' }, { url: 'photo2.jpg' }]
};

const user = plainToInstance(User, plainUser);
console.log(user.photos[0] instanceof Photo);
Drag options to blanks, or click blank then click option'
AString
BPhoto
CUser
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class in @Type decorator.
Omitting the @Type decorator for nested objects.
4fill in blank
hard

Fill both blanks to create a class-transformer transformer that converts a string to a number during transformation.

NestJS
import { Transform, plainToInstance } from 'class-transformer';

class Product {
  @Transform(({ value }) => [1])
  price: number;
}

const plainProduct = { price: '100' };
const product = plainToInstance(Product, plainProduct);
console.log(typeof product.price === [2]);
Drag options to blanks, or click blank then click option'
ANumber(value)
B'number'
C'string'
DparseInt(value)
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseInt without returning the value.
Checking typeof against 'string' instead of 'number'.
5fill in blank
hard

Fill all three blanks to create a class-transformer setup that excludes a field, transforms a nested object, and converts a string to a boolean.

NestJS
import { Exclude, Type, Transform, plainToInstance } from 'class-transformer';

class Settings {
  @Transform(({ value }) => [1])
  darkMode: boolean;
}

class User {
  name: string;
  @Exclude()
  [2]: string;
  @Type(() => [3])
  settings: Settings;
}

const plainUser = {
  name: 'Eve',
  password: '1234',
  settings: { darkMode: 'true' }
};

const user = plainToInstance(User, plainUser);
console.log(user.password === undefined);
console.log(typeof user.settings.darkMode === 'boolean');
Drag options to blanks, or click blank then click option'
Avalue === 'true'
Bpassword
CSettings
DdarkMode
Attempts:
3 left
💡 Hint
Common Mistakes
Not excluding the password field properly.
Not converting the darkMode string to boolean.
Not using the correct class in @Type decorator.