Class: J2119::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/j2119.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assertions_source) ⇒ Validator

Returns a new instance of Validator.



35
36
37
38
# File 'lib/j2119.rb', line 35

def initialize assertions_source
  assertions = File.open(assertions_source, "r")
  @parser = Parser.new assertions
end

Instance Attribute Details

#parsedObject (readonly)

Returns the value of attribute parsed.



33
34
35
# File 'lib/j2119.rb', line 33

def parsed
  @parsed
end

Instance Method Details

#rootObject



40
41
42
# File 'lib/j2119.rb', line 40

def root
  @parser.root
end

#to_sObject



72
73
74
# File 'lib/j2119.rb', line 72

def to_s
  "J2119 validator for instances of \"#{@parser.root}\""
end

#validate(json_source) ⇒ Object



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
# File 'lib/j2119.rb', line 44

def validate json_source
  # already parsed?
  if json_source.is_a?(Hash)
    @parsed = json_source
  else
    if json_source.respond_to?(:read)
      text = json_source.read
    elsif File.readable? json_source
      text = File.read json_source
    else
      text = json_source
    end
    begin
      @parsed = JSON.parse text
    rescue Exception => e
      return [ "Problem reading/parsing JSON: #{e.to_s}" ]
    end
  end
  
  problems = []
  validator = NodeValidator.new(@parser)
  validator.validate_node(@parsed,
                          @parser.root,
                          [ @parser.root ],
                          problems)
  problems
end