Class: Etna::Clients::Magma::AddModelValidator

Inherits:
ValidatorBase show all
Defined in:
lib/etna/clients/magma/workflows/json_validators.rb

Instance Attribute Summary collapse

Attributes inherited from ValidatorBase

#errors

Instance Method Summary collapse

Methods inherited from ValidatorBase

#check_in_set, #check_key, #check_key_empty, #check_valid_name_with_numbers, #format_errors, #model_exists_in_project?, #name_regex_no_numbers, #name_regex_with_numbers, #nil_or_empty?, #valid?, #validate!

Constructor Details

#initialize(models = Models.new, model_name = 'project') ⇒ AddModelValidator

Returns a new instance of AddModelValidator.



109
110
111
112
113
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 109

def initialize(models = Models.new, model_name = 'project')
  super()
  @model = models.build_model(model_name)
  @models = models
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



107
108
109
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 107

def model
  @model
end

Instance Method Details

#is_project?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 131

def is_project?
  name == 'project'
end


190
191
192
193
194
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 190

def link_attributes
  @model.template.attributes.all.select do |attribute|
    attribute.link_model_name
  end
end

#nameObject



123
124
125
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 123

def name
  model&.name
end

#parent_reciprocal_attributeObject



115
116
117
118
119
120
121
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 115

def parent_reciprocal_attribute
  @models.find_reciprocal(
    model: @model,
    link_attribute_name: @model.name,
    link_model: @models.model(@model.template.parent)
  )
end

#rawObject



127
128
129
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 127

def raw
  model&.template&.raw
end

#validateObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 135

def validate
  @errors << "Model name #{name} must be snake_case and can only consist of letters and \"_\"." unless name =~ name_regex_no_numbers

  if parent_reciprocal_attribute&.attribute_type != Etna::Clients::Magma::ParentLinkType::TABLE
    check_key("model #{name}", raw, 'identifier')
  end

  validate_links
  validate_attributes
end

#validate_attributesObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 146

def validate_attributes
  model.template.attributes.attribute_keys.each do |attribute_name|
    attribute = model.template.attributes.attribute(attribute_name)

    reciprocal = @models.find_reciprocal(model: model, attribute: attribute)
    if attribute_name == model.template.identifier && reciprocal&.attribute_type != AttributeType::TABLE
      attribute_types = [AttributeType::IDENTIFIER]
    elsif attribute_name == model.template.parent
      attribute_types = [AttributeType::PARENT]
    elsif reciprocal&.attribute_type == AttributeType::PARENT
      attribute_types = AttributeValidator.valid_parent_link_attribute_types
    else
      attribute_types = AttributeValidator.valid_add_row_attribute_types
    end

    attribute_validator = AttributeValidator.new(attribute, attribute_types, @models)
    attribute_validator.validate
    @errors += attribute_validator.errors
  end
end


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 167

def validate_links
  if !is_project? && !@models.model_keys.include?(@model.template.parent)
    @errors << "Parent model \"#{@model.template.parent}\" for #{name} does not exist in project."
  end

  if !is_project? && parent_reciprocal_attribute.nil?
    @errors << "Parent link attributes not defined for model #{name}."
  end

  link_attributes.each do |attribute|
    check_key("attribute #{attribute.attribute_name}", attribute.raw, 'link_model_name')

    unless @models.model_keys.include?(attribute.link_model_name)
      @errors << "Linked model, \"#{attribute.link_model_name}\", on attribute #{attribute.attribute_name} does not exist!"
    end

    reciprocal = @models.find_reciprocal(model: @model, attribute: attribute)
    if reciprocal.nil?
      @errors << "Linked model, \"#{attribute.link_model_name}\", on attribute #{attribute.attribute_name} does not have a reciprocal link defined."
    end
  end
end