Class: Yes::Command::Api::Commands::BatchValidator

Inherits:
Object
  • Object
show all
Extended by:
Yes::Core::OpenTelemetry::Trackable
Defined in:
lib/yes/command/api/commands/batch_validator.rb

Overview

Validates a collection of commands using their respective validator classes. Raises if any command is invalid.

Constant Summary collapse

CommandsInvalid =
Class.new(Yes::Core::Error)

Class Method Summary collapse

Class Method Details

.call(commands) ⇒ void

This method returns an undefined value.

Validates the given commands, raises CommandsInvalid if any are invalid.

Parameters:

  • commands (Array<Yes::Core::Command>)

    commands to validate

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yes/command/api/commands/batch_validator.rb', line 20

def call(commands)
  invalid = []
  commands.each do |command|
    validator = validator_for(command)
    next unless validator

    validator.call(command)
  rescue Yes::Core::Commands::Validator::CommandInvalid => e
    invalid << {
      message: e.message,
      command: command.class.to_s,
      command_id: command.command_id,
      data: command.payload,
      metadata: command. || {},
      details: e.extra
    }.tap do
      trace_error('Command validation failed', { command: command.to_json })
    end
  end

  return unless invalid.any?

  trace_error('Commands invalid', { invalid: invalid.to_json })
  raise CommandsInvalid.new(extra: invalid)
end