Best Practices

Tips and recommendations for using FakeDataDSL effectively.

Schema Organization

Use Descriptive Names

# Good
User:
  id: uuid
  email: email

# Bad
U:
  i: uuid
  e: email
# schemas/users.dsl
User:
  id: uuid
  name: name

# schemas/orders.dsl
Order:
  id: uuid
  user_id: reference(User, id)

Use Comments

User:
  # Primary identifier
  id: uuid

  # User's email address (must be unique)
  email: email unique

  # Optional phone number
  phone: phone optional

Type Selection

Choose Appropriate Types

# Good - specific types
User:
  email: email
  age: number(18..65)
  created_at: past_date(days: 365)

# Bad - generic types
User:
  email: text
  age: text
  created_at: text

Use Ranges for Numbers

# Good - realistic ranges
age: number(18..65)
price: number(min: 10, max: 1000)

# Bad - too wide
age: number(0..1000)
price: number

Performance

Use Streaming for Large Datasets

# Good - memory efficient
schema.generate_many_stream(10000).each do |record|
  process(record)
end

# Bad - loads all into memory
records = schema.generate_many(10000)
records.each { |r| process(r) }

Limit Resource Usage

# Configure limits
FakeDataDSL.configure do |config|
  config.max_array_size = 100
  config.max_text_length = 1000
end

Testing

Use Deterministic Seeds

# In tests
FakeDataDSL.configure { |c| c.seed = 12345 }

# Generate same data every time
record1 = schema.generate
record2 = schema.generate
# record1 == record2 (with same seed)

Test Edge Cases

# Generate edge cases
edge_records = schema.generate_many(100, mode: :edge)

# Test validation
edge_records.each do |record|
  expect { validate(record) }.not_to raise_error
end

Test Invalid Data

# Generate invalid data
invalid_records = schema.generate_many(100, mode: :invalid)

# Test error handling
invalid_records.each do |record|
  expect { create(record) }.to raise_error(ValidationError)
end

Security

Validate Input

# Always validate generated data
record = schema.generate
validate_record(record)  # Your validation logic

Use Limits

# Prevent DoS
FakeDataDSL.configure do |config|
  config.max_recursion = 10
  config.max_array_size = 1000
end

Maintainability

Version Control Schemas

# Keep schemas in version control
# schemas/v1/user.dsl
# schemas/v2/user.dsl

Document Complex Schemas

# Complex schema with explanation
Order:
  # Calculated from quantity and unit_price
  total: formula(quantity * unit_price)

  # Tax is 8% of total
  tax: formula(total * 0.08)

  # Grand total includes tax
  grand_total: formula(total + tax)

See Also