Bird
0
0

How would you define a schema for a Product with a required name (string), optional price (number), and a default inStock (boolean) set to true?

hard📝 Application Q15 of 15
NestJS - Database with Prisma
How would you define a schema for a Product with a required name (string), optional price (number), and a default inStock (boolean) set to true?
A@Schema() export class Product { @Prop({ required: true }) name: string; @Prop() price?: number; @Prop({ default: true }) inStock: boolean; }
B@Schema() export class Product { @Prop() name: string; @Prop({ required: true }) price: number; @Prop({ default: false }) inStock: boolean; }
C@Schema() export class Product { @Prop({ required: true }) name: number; @Prop() price: string; @Prop({ default: true }) inStock: boolean; }
D@Schema() export class Product { @Prop({ required: true }) name: string; @Prop({ default: 0 }) price: number; @Prop() inStock: boolean; }
Step-by-Step Solution
Solution:
  1. Step 1: Define required name as string

    Use @Prop({ required: true }) name: string; to make name mandatory.
  2. Step 2: Define optional price as number

    Use @Prop() price?: number; to allow price to be optional.
  3. Step 3: Define inStock with default true

    Use @Prop({ default: true }) inStock: boolean; to set default value.
  4. Final Answer:

    Schema with required name (string), optional price (number), and inStock default true -> Option A
  5. Quick Check:

    Required, optional, default props set properly [OK]
Quick Trick: Use required, optional ?, and default in @Prop() [OK]
Common Mistakes:
  • Mixing types for properties
  • Forgetting to mark required props
  • Setting wrong default values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes