lib/json-schema/validator.rb in json-schema-0.9.2 vs lib/json-schema/validator.rb in json-schema-0.9.3
- old
+ new
@@ -132,13 +132,13 @@
end
if path && path[0,1] == '/'
uri.path = Pathname.new(path).cleanpath.to_s
else
- uri.path = (Pathname.new(parent_schema.uri.path).parent + path).cleanpath.to_s
+ uri.path = (Pathname.new(parent_schema.uri.path) + path).cleanpath.to_s
end
- uri.fragment = nil
+ uri.fragment = ''
end
if Validator.schemas[uri.to_s].nil?
begin
schema = JSON::Schema.new(JSON::Validator.parse(open(uri.to_s).read), uri, @options[:version])
@@ -154,11 +154,16 @@
end
end
# Build all schemas with IDs, mapping out the namespace
- def build_schemas(parent_schema)
+ def build_schemas(parent_schema)
+ # Build ref schemas if they exist
+ if parent_schema.schema["$ref"]
+ load_ref_schema(parent_schema, parent_schema.schema["$ref"])
+ end
+
# Check for schemas in union types
["type", "disallow"].each do |key|
if parent_schema.schema[key] && parent_schema.schema[key].is_a?(Array)
parent_schema.schema[key].each_with_index do |type,i|
if type.is_a?(Hash)
@@ -197,20 +202,16 @@
end
# Either load a reference schema or create a new schema
def handle_schema(parent_schema, obj)
- if obj['$ref']
- load_ref_schema(parent_schema, obj['$ref'])
- else
- schema_uri = parent_schema.uri.clone
- schema = JSON::Schema.new(obj,schema_uri,@options[:version])
- if obj['id']
- Validator.add_schema(schema)
- end
- build_schemas(schema)
- end
+ schema_uri = parent_schema.uri.clone
+ schema = JSON::Schema.new(obj,schema_uri,@options[:version])
+ if obj['id']
+ Validator.add_schema(schema)
+ end
+ build_schemas(schema)
end
class << self
def validate(schema, data,opts={})
@@ -224,10 +225,11 @@
end
def validate!(schema, data,opts={})
validator = JSON::Validator.new(schema, data, opts)
validator.validate
+ return true
end
alias_method 'validate2', 'validate!'
def clear_cache
@@schemas = {} if @@cache_schemas == false
@@ -265,10 +267,10 @@
@@json_backend
end
def json_backend=(backend)
backend = backend.to_s
- if @@available_json_backend.include?(backend)
+ if @@available_json_backends.include?(backend)
@@json_backend = backend
else
raise JSON::Schema::JsonParseError.new("The JSON backend '#{backend}' could not be found.")
end
end
\ No newline at end of file