Class: JsonValidator

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

Overview

Copyright © 2013-2015 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Method Summary collapse

Constructor Details

#initialize(json_hash) ⇒ JsonValidator

Returns a new instance of JsonValidator.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/json_validator.rb', line 19

def initialize(json_hash)
  @json_hash = json_hash

  format_version = @json_hash["meta"]["format_version"] if @json_hash["meta"]

  if !format_version
    raise Machinery::Errors::SystemDescriptionValidationFailed.new(
      ["Could not determine format version"]
    )
  end

  @global_schema = global_schema(format_version)
  @scope_schemas = scope_schemas(format_version)
end

Instance Method Details

#validateObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/json_validator.rb', line 34

def validate
  errors = JSON::Validator.fully_validate(@global_schema, @json_hash)

  scopes = @json_hash.keys
  scopes.delete("meta")

  errors += scopes.flat_map do |scope|
    validate_scope(@json_hash[scope], scope)
  end

  errors
end