lib/json-schema/validator.rb in json-schema-0.9.0 vs lib/json-schema/validator.rb in json-schema-0.9.1
- old
+ new
@@ -55,11 +55,11 @@
"#{@uri.scheme}://#{uri.host}#{uri.path}"
end
def validate(current_schema, data, fragments)
current_schema.schema.each do |attr_name,attribute|
-
+
if @attributes.has_key?(attr_name.to_s)
@attributes[attr_name.to_s].validate(current_schema, data, fragments, self)
end
end
data
@@ -81,11 +81,11 @@
@@available_json_backends = []
@@json_backend = nil
def initialize(schema_data, data, opts={})
@options = @@default_opts.clone.merge(opts)
-
+
# I'm not a fan of this, but it's quick and dirty to get it working for now
if @options[:version]
@options[:version] = case @options[:version].to_s
when "draft3"
"draft-03"
@@ -300,16 +300,28 @@
@@json_backend = 'yajl'
end
private
-
+
+ if Gem.available?('uuidtools')
+ require 'uuidtools'
+ @@fake_uri_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
+ else
+ require 'uri/uuid'
+ @@fake_uri_generator = lambda{|s| UUID.create_v5(s,UUID::Nil).to_s }
+ end
+
+ def fake_uri schema
+ @@fake_uri_generator.call(schema)
+ end
+
def initialize_schema(schema)
if schema.is_a?(String)
begin
# Build a fake URI for this
- schema_uri = URI.parse(UUID.create_v5(schema,UUID::Nil).to_s)
+ schema_uri = URI.parse(fake_uri(schema))
schema = JSON::Validator.parse(schema)
if @options[:list]
schema = {"type" => "array", "items" => schema}
end
schema = JSON::Schema.new(schema,schema_uri,@options[:version])
@@ -338,10 +350,10 @@
end
elsif schema.is_a?(Hash)
if @options[:list]
schema = {"type" => "array", "items" => schema}
end
- schema_uri = URI.parse(UUID.create_v5(schema.inspect,UUID::Nil).to_s)
+ schema_uri = URI.parse(fake_uri(schema.inspect))
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
\ No newline at end of file