Class: Moku6::Rules::SchemaRule

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/moku6/rules/schema_rule.rb

Constant Summary collapse

SCHEMA_PATH =

: String

File.expand_path("../../../schemas/audit-event.schema.json", __dir__.to_s)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRule

#initialize

Constructor Details

This class inherits a constructor from Moku6::Rules::BaseRule

Class Method Details

.schemerObject

: () -> untyped



12
13
14
# File 'lib/moku6/rules/schema_rule.rb', line 12

def self.schemer
  @schemer ||= Senko.compile(JSON.parse(File.read(SCHEMA_PATH)))
end

Instance Method Details

#check(event) ⇒ Object

: (Event event) -> Array



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moku6/rules/schema_rule.rb', line 17

def check(event)
  result = self.class.schemer.validate(event.to_h)
  return [] if result.valid?

  result.errors.map do |err|
    h = err.to_h
    location = h["instanceLocation"].to_s
    pointer = location.empty? ? "(root)" : location
    keyword = h["keywordLocation"].to_s.split("/").last
    offense(event, :error, "schema violation #{pointer}: #{keyword} (#{h["error"] || "invalid"})")
  end
end