Module: LogStash::PluginMixins::ValidatorSupport

Defined in:
lib/logstash/plugin_mixins/validator_support.rb,
lib/logstash/plugin_mixins/validator_support/field_reference_validation_adapter.rb,
lib/logstash/plugin_mixins/validator_support/required_host_optional_port_validation_adapter.rb

Defined Under Namespace

Modules: ValidationResult Classes: NamedValidationAdapter, NamedValidationInterceptor

Constant Summary collapse

FieldReferenceValidationAdapter =
NamedValidationAdapter.new(:field_reference) do |value|
  break ValidationResult.failure("Expected exactly one field reference, got `#{value.inspect}`") unless value.kind_of?(Array) && value.size <= 1

  candidate = value.first

  break ValidationResult.success(nil) if value.empty? || candidate.nil? || candidate.empty?

  break ValidationResult.failure("Expected a valid field reference, got `#{candidate.inspect}`") unless field_reference_pattern =~ candidate

  break ValidationResult.success(candidate)
end
RequiredHostOptionalPortValidationAdapter =

When included into a Logstash plugin, adds a valiador named required_host_optional_port, which expects exactly one string matching a required host, optionally followed by a colon and a port number, and coerces valid values into a value with host and port attributes. When specifying an IPv6 address, it MUST be enclosed in square-brackets, and the address itself (without brackets) will be made available in the coerced value's host attribute

NamedValidationAdapter.new(:required_host_optional_port) do |value|
  break ValidationResult.failure("Expected exactly one #{expectation_desc}, got `#{value.inspect}`") unless value.kind_of?(Array) && value.size <= 1

  candidate = value.first

  break ValidationResult.failure("Expected a valid #{expectation_desc}, got `#{candidate.inspect}`") unless candidate.kind_of?(String) && !candidate.empty?

  # optional port
  candidate_host, candidate_port = candidate.split(%r{\:(?=\d{1,5}\z)},2)
  port = candidate_port&.to_i

  break ValidationResult.failure("Expected a port in #{port_range.inspect}, got `#{port}`") if port && !port_range.include?(port)

  # bracket-wrapped ipv6
  if candidate_host.start_with?('[') && candidate_host.end_with?(']')
    candidate_host = candidate_host[1...-1]
    break ValidationResult.success(host_port_pair.new(candidate_host, port)) if is_valid_ipv6[candidate_host]
  else
    # ipv4 or hostname
    break ValidationResult.success(host_port_pair.new(candidate_host, port)) if is_valid_ipv4[candidate_host]
    break ValidationResult.success(host_port_pair.new(candidate_host, port)) if is_valid_hostname[candidate_host]
  end

  break ValidationResult.failure("Expected a valid #{expectation_desc}, got `#{candidate.inspect}`")
end

Class Method Summary collapse

Class Method Details

.native?(base, validator_name) ⇒ Boolean



15
16
17
18
19
# File 'lib/logstash/plugin_mixins/validator_support.rb', line 15

def self.native?(base, validator_name)
  native_is_valid, native_coerced_or_error = base.validate_value(nil, validator_name)

  native_is_valid || !native_coerced_or_error.start_with?('Unknown validator')
end