Class: TemplateValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/template_validator.rb

Constant Summary collapse

@@template_rng =
File.join(Tim::Engine.root, "config/schemas", "tdl.rng")

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/validators/template_validator.rb', line 6

def validate(record)
  begin
    rng = Nokogiri::XML::RelaxNG(File.read(@@template_rng))
    xml = Nokogiri::XML(record.xml) { |config| config.strict }

    rng.validate(xml).each do |error|
      record.errors.add :xml, error.message
    end
   rescue Nokogiri::XML::SyntaxError => e
     record.errors.add :xml, "Syntax error on line #{e.line} at column #{e.column}: #{e.message}"
   end
end