Class: Kontena::Cli::Apps::YAML::Validations::CustomValidators::HooksValidator

Inherits:
HashValidator::Validator::Base
  • Object
show all
Defined in:
lib/kontena/cli/apps/yaml/custom_validators/hooks_validator.rb

Instance Method Summary collapse

Constructor Details

#initializeHooksValidator

Returns a new instance of HooksValidator.



3
4
5
# File 'lib/kontena/cli/apps/yaml/custom_validators/hooks_validator.rb', line 3

def initialize
  super('valid_hooks')
end

Instance Method Details

#validate(key, value, validations, errors) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kontena/cli/apps/yaml/custom_validators/hooks_validator.rb', line 7

def validate(key, value, validations, errors)
  unless value.is_a?(Hash)
    errors[key] = 'hooks must be array'
    return
  end

  if value['pre_build']
    validate_pre_build_hooks(key, value['pre_build'], errors)
  end

  if value['post_start']
    validate_post_start_hooks(key, value['post_start'], errors)
  end
end

#validate_post_start_hooks(key, post_start_hooks, errors) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kontena/cli/apps/yaml/custom_validators/hooks_validator.rb', line 37

def validate_post_start_hooks(key, post_start_hooks, errors)
  unless post_start_hooks.is_a?(Array)
    errors[key] = 'post_start must be array'
    return
  end
  post_start_validation = {
    'name' => 'string',
    'instances' => (-> (value) { value.is_a?(Integer) || value == '*' }),
    'cmd' => 'string',
    'oneshot' => HashValidator.optional('boolean')
  }
  validator = HashValidator.validator_for(post_start_validation)
  post_start_hooks.each do |post_start|
    validator.validate('hooks.post_start', post_start, post_start_validation, errors)
  end
end

#validate_pre_build_hooks(key, pre_build_hooks, errors) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kontena/cli/apps/yaml/custom_validators/hooks_validator.rb', line 22

def validate_pre_build_hooks(key, pre_build_hooks, errors)
  unless pre_build_hooks.is_a?(Array)
    errors[key] = 'pre_build must be array'
    return
  end
  pre_build_validation = {
    'name' => 'string',
    'cmd' => 'string'
  }
  validator = HashValidator.validator_for(pre_build_validation)
  pre_build_hooks.each do |pre_build|
    validator.validate('hooks.pre_build', pre_build, pre_build_validation, errors)
  end
end