Class: Textus::Store::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/store/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(reader:, manifest:, audit_log:, schema_for:) ⇒ Validator

Returns a new instance of Validator.



4
5
6
7
8
9
# File 'lib/textus/store/validator.rb', line 4

def initialize(reader:, manifest:, audit_log:, schema_for:)
  @reader = reader
  @manifest = manifest
  @audit_log = audit_log
  @schema_for = schema_for
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/textus/store/validator.rb', line 11

def call
  violations = []
  @manifest.enumerate.each do |row|
    begin
      @reader.get(row[:key])
    rescue Textus::Error => e
      violations << { "key" => row[:key], "code" => e.code, "message" => e.message }
    end
  end

  @manifest.enumerate.each do |row|
    mentry = row[:manifest_entry]
    next unless mentry.schema

    schema = @schema_for.call(mentry.schema)
    next unless schema

    env = begin
      @reader.get(row[:key])
    rescue StandardError
      next
    end
    last_writer = @audit_log.last_writer_for(row[:key])
    next if last_writer.nil?

    env["_meta"].each_key do |field|
      owner = schema.maintained_by(field)
      next if owner.nil?
      next if last_writer == owner
      next if last_writer == "human"

      violations << {
        "key" => row[:key],
        "code" => "role_authority",
        "field" => field,
        "expected" => owner,
        "last_writer" => last_writer,
      }
    end
  end

  { "protocol" => PROTOCOL, "ok" => violations.empty?, "violations" => violations }
end