-
Notifications
You must be signed in to change notification settings - Fork 0
/
dark_validator.rb
81 lines (64 loc) · 2.16 KB
/
dark_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
module Cocina
module Models
module Validators
# Validates that shelve and publish file attributes are set to false for dark DRO objects.
class DarkValidator
def self.validate(clazz, attributes)
new(clazz, attributes).validate
end
def initialize(clazz, attributes)
@clazz = clazz
@attributes = attributes
end
def validate
return unless meets_preconditions?
return if invalid_files.empty?
raise ValidationError, 'Not all files have dark access and/or are unshelved ' \
"when object access is dark: #{invalid_filenames}"
end
private
attr_reader :clazz, :attributes
def meets_preconditions?
# Checking for nil to account for default being dark.
dro? && ['dark', nil].include?(attributes.dig(:access, :view))
end
def dro?
(clazz::TYPES & DRO::TYPES).any?
rescue NameError
false
end
def invalid_files
@invalid_files ||=
[].tap do |invalid_files|
files.each do |file|
invalid_files << file if invalid?(file)
end
end
end
def invalid_filenames
invalid_files.map { |invalid_file| invalid_file[:filename] || invalid_file[:label] }
end
def invalid?(file)
# Ignore if a WARC
return false if file[:hasMimeType] == 'application/warc'
return true if file.dig(:administrative, :shelve)
# Checking for nil to account for default being dark.
return true if ['dark', nil].exclude?(file.dig(:access, :view))
false
end
def files
[].tap do |files|
next if attributes.dig(:structural, :contains).nil?
attributes[:structural][:contains].each do |fileset|
next if fileset.dig(:structural, :contains).nil?
fileset[:structural][:contains].each do |file|
files << file
end
end
end
end
end
end
end
end