lib/json-schema/validator.rb in json-schema-2.3.0 vs lib/json-schema/validator.rb in json-schema-2.4.0

- old
+ new

@@ -68,10 +68,11 @@ @base_schema = schema_from_fragment(@base_schema, opts[:fragment]) end end def schema_from_fragment(base_schema, fragment) + schema_uri = base_schema.uri fragments = fragment.split("/") # ensure the first element was a hash, per the fragment spec if fragments.shift != "#" raise JSON::Schema::SchemaError.new("Invalid fragment syntax in :fragment option") @@ -80,21 +81,21 @@ fragments.each do |f| if base_schema.is_a?(JSON::Schema) #test if fragment is a JSON:Schema instance if !base_schema.schema.has_key?(f) raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option") end - base_schema = base_schema.schema[f] + base_schema = base_schema.schema[f] elsif base_schema.is_a?(Hash) if !base_schema.has_key?(f) raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option") end - base_schema = initialize_schema(base_schema[f]) #need to return a Schema instance for validation to work + base_schema = JSON::Schema.new(base_schema[f],schema_uri,@options[:version]) elsif base_schema.is_a?(Array) if base_schema[f.to_i].nil? raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option") end - base_schema = initialize_schema(base_schema[f.to_i]) + base_schema = JSON::Schema.new(base_schema[f.to_i],schema_uri,@options[:version]) else raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option") end end if @options[:list] #check if the schema is validating a list @@ -415,56 +416,43 @@ when 'json' JSON.parse(s) when 'yajl' json = StringIO.new(s) parser = Yajl::Parser.new - parser.parse(json) + parser.parse(json) or raise JSON::Schema::JsonParseError.new("The JSON could not be parsed by yajl") else raise JSON::Schema::JsonParseError.new("No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json") end end end if !defined?(MultiJson) - if begin - Gem::Specification::find_by_name('json') - rescue Gem::LoadError - false - rescue - Gem.available?('json') - end + if Gem::Specification::find_all_by_name('json').any? require 'json' @@available_json_backends << 'json' @@json_backend = 'json' + else + # Try force-loading json for rubies > 1.9.2 + begin + require 'json' + @@available_json_backends << 'json' + @@json_backend = 'json' + rescue LoadError + end end - # Try force-loading json for rubies > 1.9.2 - begin - require 'json' - @@available_json_backends << 'json' - @@json_backend = 'json' - rescue LoadError - end - if begin - Gem::Specification::find_by_name('yajl-ruby') - rescue Gem::LoadError - false - rescue - Gem.available?('yajl-ruby') - end + if Gem::Specification::find_all_by_name('yajl-ruby').any? require 'yajl' @@available_json_backends << 'yajl' @@json_backend = 'yajl' end if @@json_backend == 'yajl' @@serializer = lambda{|o| Yajl::Encoder.encode(o) } else - @@serializer = lambda{|o| - YAML.dump(o) - } + @@serializer = lambda{|o| YAML.dump(o) } end end private @@ -476,34 +464,28 @@ end end private - if begin - Gem::Specification::find_by_name('uuidtools') - rescue Gem::LoadError - false - rescue - Gem.available?('uuidtools') - end + if Gem::Specification::find_all_by_name('uuidtools').any? require 'uuidtools' - @@fake_uri_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s } + @@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s } else - require 'json-schema/uri/uuid' - @@fake_uri_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s } + require 'json-schema/util/uuid' + @@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s } end def serialize schema if defined?(MultiJson) MultiJson.respond_to?(:dump) ? MultiJson.dump(schema) : MultiJson.encode(schema) else @@serializer.call(schema) end end - def fake_uri schema - @@fake_uri_generator.call(schema) + def fake_uuid schema + @@fake_uuid_generator.call(schema) end def schema_to_list(schema) new_schema = {"type" => "array", "items" => schema} if !schema["$schema"].nil? @@ -515,28 +497,20 @@ def initialize_schema(schema) if schema.is_a?(String) begin # Build a fake URI for this - schema_uri = URI.parse(fake_uri(schema)) + schema_uri = URI.parse(fake_uuid(schema)) schema = JSON::Validator.parse(schema) if @options[:list] && @options[:fragment].nil? schema = schema_to_list(schema) end schema = JSON::Schema.new(schema,schema_uri,@options[:version]) Validator.add_schema(schema) rescue # Build a uri for it - schema_uri = URI.parse(schema) - if schema_uri.relative? - # Check for absolute path - if schema[0,1] == '/' - schema_uri = URI.parse("file://#{schema}") - else - schema_uri = URI.parse("file://#{Dir.pwd}/#{schema}") - end - end + schema_uri = normalized_uri(schema) if Validator.schemas[schema_uri.to_s].nil? schema = JSON::Validator.parse(open(schema_uri.to_s).read) if @options[:list] && @options[:fragment].nil? schema = schema_to_list(schema) end @@ -544,22 +518,22 @@ Validator.add_schema(schema) else schema = Validator.schemas[schema_uri.to_s] if @options[:list] && @options[:fragment].nil? schema = schema_to_list(schema.schema) - schema_uri = URI.parse(fake_uri(serialize(schema))) + schema_uri = URI.parse(fake_uuid(serialize(schema))) schema = JSON::Schema.new(schema, schema_uri, @options[:version]) Validator.add_schema(schema) end schema end end elsif schema.is_a?(Hash) if @options[:list] && @options[:fragment].nil? schema = schema_to_list(schema) end - schema_uri = URI.parse(fake_uri(serialize(schema))) + schema_uri = URI.parse(fake_uuid(serialize(schema))) schema = JSON::Schema.new(schema,schema_uri,@options[:version]) Validator.add_schema(schema) else raise "Invalid schema - must be either a string or a hash" end @@ -570,39 +544,38 @@ def initialize_data(data) if @options[:json] data = JSON::Validator.parse(data) elsif @options[:uri] - json_uri = URI.parse(data) - if json_uri.relative? - if data[0,1] == '/' - json_uri = URI.parse("file://#{data}") - else - json_uri = URI.parse("file://#{Dir.pwd}/#{data}") - end - end + json_uri = normalized_uri(data) data = JSON::Validator.parse(open(json_uri.to_s).read) elsif data.is_a?(String) begin data = JSON::Validator.parse(data) rescue begin - json_uri = URI.parse(data) - if json_uri.relative? - if data[0,1] == '/' - json_uri = URI.parse("file://#{data}") - else - json_uri = URI.parse("file://#{Dir.pwd}/#{data}") - end - end + json_uri = normalized_uri(data) data = JSON::Validator.parse(open(json_uri.to_s).read) rescue # Silently discard the error - the data will not change end end end JSON::Schema.add_indifferent_access(data) data + end + + def normalized_uri(data) + uri = URI.parse(data) + if uri.relative? + # Check for absolute path + if data[0,1] == '/' + uri = URI.parse("file://#{data}") + else + uri = URI.parse("file://#{Dir.pwd}/#{data}") + end + end + uri end end end