Module: Kommando::CommandPlugins::Validate::ClassMethods

Defined in:
lib/kommando/command_plugins/validate.rb

Instance Method Summary collapse

Instance Method Details

#_before_execute(context) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kommando/command_plugins/validate.rb', line 8

def _before_execute(context)
  if const_defined?('Schema')
    schema_keys = const_get('Schema').schema.key_map.map(&:name).map(&:to_sym)

    if forbidden_key = reserved_parameter_keys.find { |key| schema_keys.include?(key) }
      raise Command::ReservedParameterError, "The `#{forbidden_key}` parameter is reserved and cannot be used in command schema"
    end

    reserved_parameters = context[:parameters].slice(*reserved_parameter_keys)
    validation_result = const_get('Schema').new.call(context[:parameters])

    if validation_result.success?
      super(context.merge(parameters: validation_result.to_h.merge(reserved_parameters)))
    else
      super(context.merge(halt: { error: :schema_error, data: validation_result.errors.to_h }))
    end
  else
    super(context)
  end
end

#_before_schedule(context) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kommando/command_plugins/validate.rb', line 29

def _before_schedule(context)
  if const_defined?('Schema')
    schema_keys = const_get('Schema').schema.key_map.map(&:name).map(&:to_sym)

    if forbidden_key = reserved_parameter_keys.find { |key| schema_keys.include?(key) }
      raise Command::ReservedParameterError, "The `#{forbidden_key}` parameter is reserved and cannot be used in command schema"
    end

    reserved_parameters = context[:parameters].slice(*reserved_parameter_keys)
    validation_result = const_get('Schema').new.call(context[:parameters])

    if validation_result.success?
      super(context.merge(parameters: validation_result.to_h.merge(reserved_parameters)))
    else
      super(context.merge(halt: { error: :schema_error, data: validation_result.errors.to_h }))
    end
  else
    super(context)
  end
end