Class: Skadi::DashboardValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/skadi/dashboard_validator.rb

Overview

Validates that a Dashboard's configuration matches the DashboardConfig type in app/frontend/types.d.ts

Defined Under Namespace

Classes: ArrayOf, Context, Filter, OneOf

Constant Summary collapse

FILTER_TYPES =
{
  boolean: {
    operators: [ "=", "!=", "empty", "not empty" ],
  },
  date: {
    operators: [ "=", ">", ">=", "<=", "<", "!=", "empty", "not empty" ],
  },
  number: {
    operators: [ "=", ">", ">=", "<=", "<", "!=", "empty", "not empty" ],
  },
  one_of: {
    operators: [ "=", "!=", "empty", "not empty" ],
  },
  string: {
    operators: [ "=", "!=", "like", "not like", "empty", "not empty" ],
  },
}
COMMON_DATASET_FIELDS =
{
  id: :string,
  label: :string,
  visible: :boolean?,
  axis: OneOf.new(allowed_values: %w[left right].freeze, allow_missing: true),
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.typesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/skadi/dashboard_validator.rb', line 33

def self.types
  return @types if defined?(@types)

  @types = {
    DashboardTabConfig: {
      id: :string,
      title: :string,
      description: :string?,
      date_from: :date?,
      date_to: :date?,
      children: ArrayOf.new(:ChartConfig),
    },
    ChartConfig: {
      id: :string,
      type: OneOf.new(allowed_values: %w[bar line table].freeze).freeze,
      title: :string,
      description: :string?,
      time_series: OneOf.new(allowed_values: %w[daily weekly monthly].freeze, allow_missing: true).freeze,
      date_from: :date?,
      date_to: :date?,
      verified_visits: :boolean?,
      unique_by: OneOf.new(allowed_values: %w[visit visitor].freeze, allow_missing: true).freeze,
      visit_tracking: OneOf.new(allowed_values: %w[any anonymity_set cookie].freeze, allow_missing: true).freeze,
      datasets: ArrayOf.new(:Dataset, 1),
    },
    percentageDataset: {
      **COMMON_DATASET_FIELDS,
      type: "percentage",

      numerator: :dataset_id,
      denominator: :dataset_id,
    },
    sqlDataset: {
      **COMMON_DATASET_FIELDS,
      type: "sql",

      sql: :sql,
    },
  }

  Schema.database_schema.each do |dataset_name, schema|
    dataset_type = COMMON_DATASET_FIELDS.dup
    dataset_type[:type] = dataset_name.to_s

    split_by_fields = []
    filters = {}

    schema[:fields].each do |field, field_config|
      split_by_fields << field.to_s if field_config[:split]
      next unless field_config[:filter]

      if field_config[:type] == :one_of
        filters[field] = OneOf.new(allowed_values: field_config[:options], allow_missing: true)
      else
        filters[field] = field_config[:type] || :string
      end
    end

    dataset_type[:split_by] = ArrayOf.new(OneOf.new(split_by_fields, false)) if split_by_fields.any?
    dataset_type[:filters] = ArrayOf.new(Filter.new(filters))

    @types[:"#{dataset_name}Dataset"] = dataset_type
  end

  return @types
end

Instance Method Details

#validate(record) ⇒ Object



102
103
104
105
106
# File 'app/models/skadi/dashboard_validator.rb', line 102

def validate(record)
  context = Context.new(record, nil, [])

  validate_type(ArrayOf.new(:DashboardTabConfig, 1), record.configuration, "configuration", context:)
end