lib/verifier.rb in inqlude-0.0.8 vs lib/verifier.rb in inqlude-0.7.0

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright (C) 2011 Cornelius Schumacher <schumacher@kde.org> +# Copyright (C) 2011-2013 Cornelius Schumacher <schumacher@kde.org> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. @@ -42,36 +42,48 @@ end def initialize settings @settings = settings - @allowed_keys = [ "schema_version", "name", "version", "release_date", + @allowed_keys = [ "$schema", "name", "version", "release_date", "summary", "urls", "licenses", "description", "authors", "maturity", "platforms", "packages", "keywords", "dependencies", "filename", - "libraryname" ] - @mandatory_keys = [ "schema_version", "name", "version", "release_date", - "summary", "urls", "licenses", "description", "maturity", - "platforms", "packages" ] + "libraryname", "display_name", "schema_type", "schema_version", "group" ] end def verify manifest @result = Result.new if !manifest["filename"] - @result.errors = "Unable to determine filename" + @result.errors.push "Unable to determine filename" @result.name = "<unknown>" else @result.name = manifest["filename"] end if !manifest["libraryname"] - @result.errors = "Unable to determine libraryname" + @result.errors.push "Unable to determine libraryname" end + if manifest["$schema"] + schema_type = manifest["schema_type"] + if schema_type != "generic" && schema_type != "release" && + schema_type != "proprietary-release" + @result.errors.push "Unknown schema type '#{schema_type}'" + end + else + @result.errors.push "Unable to find $schema attribute" + end if @result.errors.empty? filename = manifest["filename"] - expected_filename = "#{manifest["libraryname"]}.#{manifest["release_date"]}.manifest" - + expected_filename = "" + schema_type = manifest["schema_type"] + if schema_type == "generic" + expected_filename = "#{manifest["libraryname"]}.manifest" + elsif schema_type == "release" || schema_type == "proprietary-release" + expected_filename = "#{manifest["libraryname"]}.#{manifest["release_date"]}.manifest" + end + if filename != expected_filename @result.errors.push "Expected file name: #{expected_filename}" end if manifest["release_date"] == "1970-01-01" @@ -82,14 +94,16 @@ if !@allowed_keys.include? key @result.errors.push "Illegal entry: #{key}" end end - @mandatory_keys.each do |key| - if !manifest.keys.include? key - @result.errors.push "Mandatory attribute is missing: #{key}" - end + schema_name = "#{manifest["schema_type"]}-manifest-v#{manifest["schema_version"]}" + schema_file = File.expand_path("../../schema/#{schema_name}", __FILE__) + + errors = JSON::Validator.fully_validate(schema_file, manifest) + errors.each do |error| + @result.errors.push "Schema validation error: #{error}" end end if @result.errors.empty? @result.valid = true @@ -99,13 +113,10 @@ return @result end end def verify_file filename - manifest = JSON File.read filename - manifest["filename"] = filename - filename =~ /^(.*?)\./ - manifest["libraryname"] = $1 + manifest = Manifest.parse_file filename verify manifest end end