Challenge - 5 Problems
NestJS Schema Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS schema validation?
Given this schema and input, what will be the validation result?
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; @Schema() export class User extends Document { @Prop({ required: true }) name: string; @Prop({ default: 18 }) age: number; } const UserSchema = SchemaFactory.createForClass(User); // Input data: const input = { name: 'Alice' };
Attempts:
2 left
💡 Hint
Check the @Prop decorator options for default and required
✗ Incorrect
The @Prop({ default: 18 }) sets age to 18 if missing. Name is required, and provided, so validation passes.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a nested schema in NestJS?
You want to define a nested Address schema inside a User schema. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Nested schemas require both classes to be decorated with @Schema and use a function for type.
✗ Incorrect
Both Address and User must be decorated with @Schema(). The @Prop for nested schema uses type: () => Address to avoid circular reference issues.
🔧 Debug
advanced2:00remaining
Why does this NestJS schema throw a runtime error?
This schema code throws an error when creating a Product document. What is the cause?
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; @Schema() export class Product extends Document { @Prop() name: string; @Prop({ type: Number, required: true }) price: number; } export const ProductSchema = SchemaFactory.createForClass(Product); // Error: Missing required property 'price' when creating a Product document
Attempts:
2 left
💡 Hint
Check which properties are required and if defaults exist
✗ Incorrect
The 'price' property is marked as required but has no default. If a new Product is created without 'price', Mongoose throws an error.
❓ state_output
advanced2:00remaining
What is the value of 'createdAt' after saving this schema?
Given this schema with timestamps enabled, what will be the value of 'createdAt' after saving a new document?
NestJS
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; @Schema({ timestamps: true }) export class Article extends Document { @Prop() title: string; } const ArticleSchema = SchemaFactory.createForClass(Article); // After saving a new Article with title 'Hello', what is article.createdAt?
Attempts:
2 left
💡 Hint
Timestamps option automatically adds createdAt and updatedAt fields as Date objects
✗ Incorrect
With timestamps: true, Mongoose adds createdAt and updatedAt as Date objects automatically.
🧠 Conceptual
expert2:00remaining
Which option best explains the purpose of SchemaFactory in NestJS?
Why does NestJS use SchemaFactory.createForClass instead of directly using Mongoose schemas?
Attempts:
2 left
💡 Hint
Think about how decorators and classes relate to Mongoose schemas
✗ Incorrect
SchemaFactory.createForClass converts a TypeScript class decorated with @Schema and @Prop into a Mongoose schema, bridging NestJS decorators with Mongoose.