Class: Etna::Clients::Magma::ValidatorBase

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidatorBase

Returns a new instance of ValidatorBase.



10
11
12
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 10

def initialize
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 8

def errors
  @errors
end

Instance Method Details

#check_in_set(label, raw, key, valid_values) ⇒ Object



31
32
33
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 31

def check_in_set(label, raw, key, valid_values)
  @errors << "Invalid #{key} for #{label}: \"#{raw[key]}\".\nShould be one of #{valid_values}." if raw.dig(key) && !valid_values.include?(raw[key])
end

#check_key(label, raw, key) ⇒ Object



22
23
24
25
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 22

def check_key(label, raw, key)
  @errors << "Missing required key for #{label}: \"#{key}\"." if !raw.dig(key)
  @errors << "Invalid empty #{key} for #{label}: \"#{raw[key]}\"." if raw.dig(key) && nil_or_empty?(raw[key])
end

#check_key_empty(label, raw, key) ⇒ Object



27
28
29
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 27

def check_key_empty(label, raw, key)
  @errors << "Invalid key for #{label}: \"#{key}\"." if raw.dig(key)
end

#check_valid_name_with_numbers(label, proposed_name) ⇒ Object



39
40
41
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 39

def check_valid_name_with_numbers(label, proposed_name)
  @errors << "#{label} name \"#{proposed_name}\" must be snake_case and can only consist of letters, numbers, and \"_\"." unless proposed_name =~ name_regex_with_numbers
end

#format_errorsObject



57
58
59
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 57

def format_errors
  errors.map { |e| e.gsub("\n", "\n\t") }.join("\n  * ")
end

#model_exists_in_project?(project_magma_models, model_name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 47

def model_exists_in_project?(project_magma_models, model_name)
  !!project_magma_models.model(model_name)
end

#name_regex_no_numbersObject



43
44
45
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 43

def name_regex_no_numbers
  /\A[a-z]*(_[a-z]+)*\Z/
end

#name_regex_with_numbersObject



35
36
37
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 35

def name_regex_with_numbers
  /\A[a-z][a-z0-9]*(_[a-z0-9]+)*\Z/
end

#nil_or_empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 18

def nil_or_empty?(value)
  value.nil? || value.empty?
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 14

def valid?
  @errors.length == 0
end

#validate!(err_message) ⇒ Object



51
52
53
54
55
# File 'lib/etna/clients/magma/workflows/json_validators.rb', line 51

def validate!(err_message)
  @errors = []
  self.validate
  raise "#{err_message}\n#{format_errors}" unless valid?
end