Data Schemas & Contracts
Define the structure and validation rules for your API responses
What is a Schema?
A schema (or data contract) is a blueprint that defines the structure, data types, and validation rules for your API responses. It ensures consistency and predictability in your mock data.
Why Schemas Matter
- ✓ Type Safety: Frontend knows what data types to expect
- ✓ Consistency: All records follow the same structure
- ✓ Validation: Catch errors before they reach production
- ✓ Documentation: Self-documenting API contracts
- ✓ Auto-generation: Smart mock data based on field types
Field Types
Mock API Builder supports various field types for realistic data:
Text data of any length
"name": "John Doe"Use for: names, descriptions, addresses
Integer or decimal values
"price": 29.99Use for: prices, quantities, IDs
True/false values
"isActive": trueUse for: flags, toggles, status
Email address format
"email": "user@example.com"Auto-validates email format
ISO 8601 date strings
"createdAt": "2024-01-15T10:30:00Z"Use for: timestamps, birthdays
Unique identifier
"id": "550e8400-e29b-41d4-a716-..."Use for: unique IDs
Web address format
"website": "https://example.com"Use for: links, avatar URLs
List of items
"tags": ["javascript", "react"]Use for: lists, collections
Nested data structure
"address": { "city": "NYC" }Use for: nested data
Limited set of values
"status": "active" | "pending"Use for: status, role, category
Creating Schemas
You have three ways to define your data schema:
Build your schema field-by-field using the visual interface:
Example: User Schema
Best for:
• Simple schemas
• Quick prototyping
• Learning and experimentation
Write your schema directly in JSON format:
{
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"isActive": true,
"createdAt": "2024-01-15T10:30:00Z"
}Best for:
• Complex nested structures
• Copy/paste from existing APIs
• Developers comfortable with JSON
Import complete schemas from OpenAPI/Swagger specifications:
components:
schemas:
User:
type: object
properties:
id:
type: integer
name:
type: string
email:
type: string
format: emailBest for:
• Existing API specs
• Team standardization
• Large-scale projects
Nested Objects & Arrays
Nested Objects
Create complex data structures with nested properties:
{
"id": 1,
"name": "John Doe",
"address": {
"street": "123 Main St",
"city": "New York",
"zipCode": "10001",
"coordinates": {
"lat": 40.7128,
"lng": -74.0060
}
}
}Arrays of Objects
Define arrays containing complex objects:
{
"id": 1,
"name": "John Doe",
"orders": [
{
"orderId": "ORD-001",
"total": 99.99,
"status": "completed"
},
{
"orderId": "ORD-002",
"total": 149.99,
"status": "pending"
}
]
}Field Constraints & Validation
Mark fields that must always have a value
required: ["id", "name", "email"]Set minimum and maximum character limits
minLength: 3, maxLength: 50Define acceptable numeric ranges
minimum: 0, maximum: 100Use regex for custom validation
pattern: "^[A-Z]{2}-[0-9]{4}$"Best Practices
Begin with basic fields, then add complexity as needed. Don't over-engineer from the start.
✓ firstName, emailAddress
✗ fn, em
Choose a convention and stick to it:
- • camelCase:
firstName - • snake_case:
first_name - • PascalCase:
FirstName
Always include: id,createdAt,updatedAt for better tracking
Keep your mock schema identical to your real API to avoid integration issues later.
Common Patterns
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"email": "john@example.com",
"avatar": "https://...",
"role": "user",
"isActive": true,
"createdAt": "2024-01-15T10:30:00Z"
}{
"id": 1,
"name": "Product Name",
"description": "Product description...",
"price": 29.99,
"currency": "USD",
"stock": 100,
"category": "electronics",
"images": ["url1", "url2"],
"tags": ["new", "featured"]
}{
"data": [...],
"pagination": {
"currentPage": 1,
"perPage": 25,
"totalPages": 1,
"totalItems": 5,
"hasNextPage": false,
"hasPrevPage": false
}
}