# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true module Contrast module Agent module Telemetry module TelemetryException # This class will hold the all the mutual information for the Telemetry Exception class Base def to_controlled_hash; end protected # Validate required and option fields # # @param validations [Hash] Validation hash to use # @return [nil] def validate validations validations.each do |k, v| next if v[:required] == false validate_field(validations[k], k) end nil end # This method will validate every single field passed from validate # # @param validation_pair [Hash] Validation hash to use # @param key[String] The key to check in VALIDATIONS # @raise [ArgumentError] def validate_field validation_pair, key value_to_validate = send(key.to_sym) validate_class(value_to_validate, validation_pair[:class], key) if validation_pair.key?(:class) value_length = if value_to_validate.cs__is_a?(String) || value_to_validate.cs__is_a?(Array) value_to_validate.length else value_to_validate.entries.length end unless validation_pair[:range].include?(value_length) raise(ArgumentError, "The provided value for #{ key } is invalid: #{ value_to_validate }") end nil end # With the all nested classes, we still need to double check if everything passed along the way # is right # @param message [Object] The message we want to check the class of # @param klass [Class] The klass we want to check the message with # @param field [Object] The field with the error # @raise [ArgumentError] def validate_class message, klass, field message = message[0] if message.cs__is_a?(Array) raise(ArgumentError, "The provided value for #{ field } is of wrong class") unless message.cs__is_a?(klass) end end end end end end