Class: LogStash::Filters::SchemaValidation

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/schema_validation.rb

Overview

This filter will replace the contents of the default message field with whatever you specify in the configuration.

It is only intended to be used as an .

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/logstash/filters/schema_validation.rb', line 45

def filter(event)

  schemaFilePath = generate_filepath(event)

  if File.exists?(schemaFilePath)

    schema = get_schema(schemaFilePath)

    json = @jsonMapper.readTree(event.to_json)

    #validationErrors = JSON::Validator.fully_validate(schemaFilePath, event.to_hash, :strict => @strict, :fragment => @fragment, :parse_data => false)

    validationErrors = schema.validate(json)

    if validationErrors.isSuccess
      filter_matched(event)
    else
      tag_unsuccessful_lookup(event)

      unless @report_field.nil? || @report_field.empty?
        report = Array.new

        validationErrors.each do |message|
          report.push(message.getMessage)
        end

        event.set(@report_field, report)
      end
    end

  else

    @logger.fatal? && @logger.fatal("Schema file '" + schemaFilePath + "' does not exists!")

    tag_unsuccessful_lookup(event)

    unless @report_field.nil? || @report_field.empty?
      event.set(@report_field, ["Schema file '" + schemaFilePath + "' does not exists!"])
    end

  end

end

#registerObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/logstash/filters/schema_validation.rb', line 33

def register
  @schema = File.expand_path(@schema)

  @schemaFactory = JsonSchemaFactory.newBuilder().freeze()

  @jsonMapper = ObjectMapper.new
  @validator = JsonSchemaFactory.byDefault().getValidator()

  @cacheSchema = {}
end

#tag_unsuccessful_lookup(event) ⇒ Object



89
90
91
92
# File 'lib/logstash/filters/schema_validation.rb', line 89

def tag_unsuccessful_lookup(event)
  @logger.debug? && @logger.debug("Event data is not valide!", :event => event)
  @tag_on_failure.each{|tag| event.tag(tag)}
end