-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
archive_validator.rb
60 lines (47 loc) · 1.38 KB
/
archive_validator.rb
1
2
3
4
5
6
7
8
9
10
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
54
55
56
57
58
59
60
# frozen_string_literal: true
require "yajl"
# ArchiveValidator checks for errors in archive. It also find non-critical problems and fixes them in the archive hash
# so that the ArchiveImporter doesn't have to handle this issues. Non-critical problems found are indicated as warnings.
# Also it performs necessary data fetch where required.
class ArchiveValidator
include ArchiveImporter::ArchiveHelper
def initialize(archive)
@archive = archive
end
def validate
run_validators(CRITICAL_VALIDATORS, errors)
run_validators(NON_CRITICAL_VALIDATORS, warnings)
rescue KeyError => e
errors.push("Missing mandatory data: #{e}")
rescue Yajl::ParseError => e
errors.push("Bad JSON provided: #{e}")
end
def errors
@errors ||= []
end
def warnings
@warnings ||= []
end
def archive_hash
@archive_hash ||= Yajl::Parser.new.parse(archive)
end
CRITICAL_VALIDATORS = [
SchemaValidator,
AuthorPrivateKeyValidator
].freeze
NON_CRITICAL_VALIDATORS = [
ContactsValidator,
PostsValidator,
RelayablesValidator,
OthersRelayablesValidator
].freeze
private_constant :CRITICAL_VALIDATORS, :NON_CRITICAL_VALIDATORS
private
attr_reader :archive
def run_validators(list, messages)
list.each do |validator_class|
validator = validator_class.new(archive_hash)
messages.concat(validator.messages)
end
end
end