Class: LcpRuby::Workflow::ContractValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/workflow/contract_validator.rb

Constant Summary collapse

REQUIRED_FIELDS =
{
  "name" => "string",
  "model_name" => "string",
  "field_name" => "string",
  "states" => "json",
  "transitions" => "json",
  "version" => "integer",
  "active" => "boolean"
}.freeze

Class Method Summary collapse

Class Method Details

.validate(model_def, field_mapping = nil) ⇒ Metadata::ContractResult

Validates that the given model definition satisfies the workflow model contract.

Parameters:

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lcp_ruby/workflow/contract_validator.rb', line 18

def self.validate(model_def, field_mapping = nil)
  field_mapping = (field_mapping || LcpRuby.configuration.workflow_model_fields).transform_keys(&:to_s)
  errors = []
  warnings = []

  REQUIRED_FIELDS.each do |logical_name, expected_type|
    mapped_name = field_mapping[logical_name] || logical_name
    field = model_def.fields.find { |f| f.name == mapped_name.to_s }

    if field.nil?
      errors << "Workflow model '#{model_def.name}' must have a '#{mapped_name}' field (mapped as #{logical_name})"
      next
    end

    actual_type = field.type
    expected_types = expected_type == "json" ? %w[json jsonb] : [ expected_type ]
    unless expected_types.include?(actual_type)
      errors << "Workflow model '#{model_def.name}': '#{mapped_name}' field must be type " \
                 "'#{expected_type}' (got '#{actual_type}')"
    end
  end

  Metadata::ContractResult.new(errors: errors, warnings: warnings)
end