Bird
0
0

You want to define a schema for a user profile where the email is required and must be a string, and age is optional but must be an integer if present. Which schema snippet correctly represents this?

hard📝 Application Q8 of 15
Rest API - API Documentation
You want to define a schema for a user profile where the email is required and must be a string, and age is optional but must be an integer if present. Which schema snippet correctly represents this?
A{ "type": "object", "properties": { "email": { "type": "string" }, "age": { "type": "integer" } }, "required": ["email", "age"] }
B{ "type": "object", "properties": { "email": { "type": "string", "required": true }, "age": { "type": "integer" } } }
C{ "type": "object", "properties": { "email": { "type": "string" }, "age": { "type": "integer", "required": false } }, "required": ["email"] }
D{ "type": "object", "properties": { "email": { "type": "string" }, "age": { "type": "integer" } }, "required": ["email"] }
Step-by-Step Solution
Solution:
  1. Step 1: Identify how to mark required properties

    In OpenAPI, required properties are listed in the "required" array at the object level.
  2. Step 2: Check each option for correct usage

    { "type": "object", "properties": { "email": { "type": "string" }, "age": { "type": "integer" } }, "required": ["email"] } correctly lists "email" as required and "age" as optional without "required" inside properties.
  3. Final Answer:

    { "type": "object", "properties": { "email": { "type": "string" }, "age": { "type": "integer" } }, "required": ["email"] } -> Option D
  4. Quick Check:

    Required properties listed in "required" array [OK]
Quick Trick: Use "required" array for mandatory fields only [OK]
Common Mistakes:
MISTAKES
  • Putting "required" inside property definitions
  • Listing optional fields as required
  • Omitting the "required" array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes