Class: Scaptimony::DataStreamValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/scaptimony/scap_content.rb

Instance Method Summary collapse

Instance Method Details

#validate(scap_content) ⇒ Object



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
# File 'app/models/scaptimony/scap_content.rb', line 10

def validate(scap_content)
  if !scap_content.new_record?
    return true if scap_content.scap_file.nil?
    scap_content.errors[:base] << _("Cannot change uploaded file while editing content.")
    return false
  end
  if scap_content.scap_file.nil?
    scap_content.errors[:base] << _("Please select file for upload.")
    return false
  end

  existing = ScapContent.where(:digest => scap_content.digest).first
  if !existing.nil?
    scap_content.errors[:base] << _("This file has been already uploaded as '#{existing.title}'.")
    return false
  end

  allowed_type = 'SCAP Source Datastream'
  if scap_content.source.type != allowed_type
    scap_content.errors[:base] << _("Uploaded file is not #{allowed_type}.")
    return false
  end

  begin
    scap_content.source.validate!
  rescue OpenSCAP::OpenSCAPError => e
    scap_content.errors[:base] << e.message
  end
end