Class: Etna::Clients::Magma::AttributeValidator

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

Instance Attribute Summary collapse

Attributes inherited from ValidatorBase

#errors

Class Method Summary collapse

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(attribute, valid_attribute_types, project_models) ⇒ AttributeValidator

Returns a new instance of AttributeValidator.



200
201
202
203
204
205
206
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 200

def initialize(attribute, valid_attribute_types, project_models)
  super()
  @attribute = attribute
  @valid_attribute_types = valid_attribute_types
  @valid_validation_types = self.class.valid_validation_types
  @project_models = project_models
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



198
199
200
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 198

def attribute
  @attribute
end

Class Method Details

.valid_add_row_attribute_typesObject



208
209
210
211
212
213
214
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 208

def self.valid_add_row_attribute_types
  Etna::Clients::Magma::AttributeType.entries.reject { |a|
    a == Etna::Clients::Magma::AttributeType::CHILD ||
        a == Etna::Clients::Magma::AttributeType::IDENTIFIER ||
        a == Etna::Clients::Magma::AttributeType::PARENT
  }.sort
end


216
217
218
219
220
221
222
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 216

def self.valid_parent_link_attribute_types
  [
      AttributeType::COLLECTION,
      AttributeType::TABLE,
      AttributeType::CHILD,
  ]
end

.valid_update_attribute_typesObject



224
225
226
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 224

def self.valid_update_attribute_types
  Etna::Clients::Magma::AttributeType.entries.sort
end

.valid_validation_typesObject



228
229
230
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 228

def self.valid_validation_types
  Etna::Clients::Magma::AttributeValidationType.entries.sort
end

Instance Method Details

#is_identifier_attribute?Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 263

def is_identifier_attribute?
  attribute.attribute_type == Etna::Clients::Magma::AttributeType::IDENTIFIER
end

Returns:

  • (Boolean)


259
260
261
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 259

def is_link_attribute?
  attribute.attribute_type == Etna::Clients::Magma::AttributeType::LINK
end

#validateObject



232
233
234
235
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 232

def validate
  validate_basic_attribute_data
  validate_attribute_validation
end

#validate_attribute_validationObject



252
253
254
255
256
257
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 252

def validate_attribute_validation
  return unless attribute.validation
  check_key("attribute #{attribute.attribute_name}, validation", attribute.validation, 'type')
  check_key("attribute #{attribute.attribute_name}, validation", attribute.validation, 'value')
  check_in_set("attribute #{attribute.attribute_name}, validation", attribute.validation, 'type', @valid_validation_types)
end

#validate_basic_attribute_dataObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 237

def validate_basic_attribute_data
  check_valid_name_with_numbers('Attribute', attribute.attribute_name)
  check_key("attribute #{attribute.attribute_name}", attribute.raw, 'attribute_type')

  if attribute.link_model_name && ![AttributeType::TABLE, AttributeType::LINK, AttributeType::COLLECTION, AttributeType::PARENT, AttributeType::CHILD].include?(attribute.attribute_type)
    @errors << "attribute #{attribute.attribute_name} has link_model_name set, but has attribute_type #{attribute.attribute_type}"
  end

  if attribute.link_model_name && !@project_models.model_keys.include?(attribute.link_model_name)
    @errors << "attribute #{attribute.attribute_name} has link_model_name value of #{attribute.link_model_name}, but a model by that name does not exist."
  end

  check_in_set("attribute #{attribute.attribute_name}", attribute.raw, 'attribute_type', @valid_attribute_types)
end