Class: RailsDrips::DefinitionSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_drips/definition_schema.rb

Constant Summary collapse

SCHEMA_VERSION =
1

Class Method Summary collapse

Class Method Details

.validate(definition) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails_drips/definition_schema.rb', line 7

def self.validate(definition)
  errors = []
  return ["definition must be a mapping"] unless definition.is_a?(Hash)

  errors << "schema_version must be #{SCHEMA_VERSION}" unless definition["schema_version"] == SCHEMA_VERSION
  campaign = definition["campaign"]
  return errors << "campaign must be a mapping" unless campaign.is_a?(Hash)

  errors << "campaign.key cannot be blank" if campaign["key"].to_s.strip.empty?
  errors << "campaign.name cannot be blank" if campaign["name"].to_s.strip.empty?
  steps = campaign["steps"]
  return errors << "campaign.steps must be a non-empty list" unless steps.is_a?(Array) && steps.any?

  positions = steps.filter_map { |step| step["position"] if step.is_a?(Hash) }
  errors << "campaign.steps positions must be unique" if positions.uniq.length != positions.length
  steps.each_with_index { |step, index| validate_step(step, index, errors) }
  errors
end