lib/json-schema/validator.rb in json-schema-0.1.9 vs lib/json-schema/validator.rb in json-schema-0.1.10
- old
+ new
@@ -13,10 +13,13 @@
@fragments = fragments
@schema = schema
super(message)
end
end
+
+ class SchemaError < Exception
+ end
class Validator
@@schemas = {}
@@cache_schemas = false
@@ -195,20 +198,20 @@
end
# Validate the minimum number of items in an array
def validate_minItems(current_schema, data, fragments)
- if data.is_a?(Array) && (data.nitems < current_schema.schema['minItems'])
+ if data.is_a?(Array) && (data.compact.size < current_schema.schema['minItems'])
message = "The property '#{build_fragment(fragments)}' did not contain a minimum number of items #{current_schema.schema['minItems']}"
raise ValidationError.new(message, fragments, current_schema)
end
end
# Validate the maximum number of items in an array
def validate_maxItems(current_schema, data, fragments)
- if data.is_a?(Array) && (data.nitems > current_schema.schema['maxItems'])
+ if data.is_a?(Array) && (data.compact.size > current_schema.schema['maxItems'])
message = "The property '#{build_fragment(fragments)}' did not contain a minimum number of items #{current_schema.schema['minItems']}"
raise ValidationError.new(message, fragments, current_schema)
end
end
@@ -454,13 +457,13 @@
if temp_uri.relative?
temp_uri = current_schema.uri.clone
# Check for absolute path
path = current_schema.schema['$ref'].split("#")[0]
if path[0,1] == "/"
- temp_uri.path = Pathname.new(path).cleanpath
+ temp_uri.path = Pathname.new(path).cleanpath.to_s
else
- temp_uri.path = (Pathname.new(current_schema.uri.path).parent + path).cleanpath
+ temp_uri.path = (Pathname.new(current_schema.uri.path).parent + path).cleanpath.to_s
end
temp_uri.fragment = current_schema.schema['$ref'].split("#")[1]
end
temp_uri.fragment = "" if temp_uri.fragment.nil?
@@ -470,17 +473,22 @@
if ref_schema
# Perform fragment resolution to retrieve the appropriate level for the schema
target_schema = ref_schema.schema
fragments = temp_uri.fragment.split("/")
+ fragment_path = ''
fragments.each do |fragment|
if fragment && fragment != ''
if target_schema.is_a?(Array)
target_schema = target_schema[fragment.to_i]
else
target_schema = target_schema[fragment]
end
+ fragment_path = fragment_path + "/#{fragment}"
+ if target_schema.nil?
+ raise SchemaError.new("The fragment '#{fragment_path}' does not exist on schema #{ref_schema.uri.to_s}")
+ end
end
end
# We have the schema finally, build it and validate!
schema = JSON::Schema.new(target_schema,temp_uri)
@@ -501,12 +509,12 @@
if uri.relative?
uri = parent_schema.uri.clone
# Check for absolute path
path = ref.split("#")[0]
if path[0,1] == '/'
- uri.path = Pathname.new(path).cleanpath
+ uri.path = Pathname.new(path).cleanpath.to_s
else
- uri.path = (Pathname.new(parent_schema.uri.path).parent + path).cleanpath
+ uri.path = (Pathname.new(parent_schema.uri.path).parent + path).cleanpath.to_s
end
uri.fragment = nil
end
if Validator.schemas[uri.to_s].nil?
\ No newline at end of file