Class: LogStash::PluginMixins::ValidatorSupport::NamedValidationAdapter

Inherits:
Module
  • Object
show all
Defined in:
lib/logstash/plugin_mixins/validator_support.rb

Overview

A NamedValidationAdapter is a module that can be mixed into a Logstash plugin to ensure the named validator is present and available, whether provided by Logstash core or approximated with the provided backport implementation.

Instance Method Summary collapse

Constructor Details

#initialize(validator_name) {|value| ... } ⇒ NamedValidationAdapter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new named validation adapter, to approximate the implementation of a named validation that exists in Logstash Core.

Parameters:

  • validator_name (Symbol)

Yield Parameters:

  • value (Hash, Array)

Yield Returns:

  • (true, Object)

    : validation success returns true with coerced value (see: ValidationResult#success)

  • (false, String)

    : validation failure returns false with error message (see: ValidationResult#failure)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logstash/plugin_mixins/validator_support.rb', line 40

def initialize(validator_name, &validator_implementation)
  fail(ArgumentError, '`validator_name` must be a Symbol')         unless validator_name.kind_of?(Symbol)
  fail(ArgumentError, '`validator_implementation` block required') unless validator_implementation

  define_singleton_method(:validate, &validator_implementation)
  define_singleton_method(:name) { "#{NamedValidationAdapter}(#{validator_name})" }

  define_singleton_method(:extended) do |base|
    # Only include the interceptor if support is not natively provided.
    unless ValidatorSupport.native?(base, validator_name)
      interceptor = NamedValidationInterceptor.new(validator_name, self)
      base.extend(interceptor)
    end
  end
end