lib/json-schema/validator.rb in json-schema-2.7.0 vs lib/json-schema/validator.rb in json-schema-2.8.0
- old
+ new
@@ -39,13 +39,13 @@
def initialize(schema_data, data, opts={})
@options = @@default_opts.clone.merge(opts)
@errors = []
- validator = JSON::Validator.validator_for_name(@options[:version])
+ validator = self.class.validator_for_name(@options[:version])
@options[:version] = validator
- @options[:schema_reader] ||= JSON::Validator.schema_reader
+ @options[:schema_reader] ||= self.class.schema_reader
@validation_options = @options[:record_errors] ? {:record_errors => true} : {}
@validation_options[:insert_defaults] = true if @options[:insert_defaults]
@validation_options[:strict] = true if @options[:strict] == true
@validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]
@@ -56,11 +56,11 @@
@@mutex.synchronize { build_schemas(@base_schema) }
# validate the schema, if requested
if @options[:validate_schema]
if @base_schema.schema["$schema"]
- base_validator = JSON::Validator.validator_for_name(@base_schema.schema["$schema"])
+ base_validator = self.class.validator_for_name(@base_schema.schema["$schema"])
end
metaschema = base_validator ? base_validator.metaschema : validator.metaschema
# Don't clear the cache during metaschema validation!
self.class.validate!(metaschema, @base_schema.schema, {:clear_cache => false})
end
@@ -121,37 +121,29 @@
else
true
end
ensure
if @validation_options[:clear_cache] == true
- Validator.clear_cache
+ self.class.clear_cache
end
if @validation_options[:insert_defaults]
- JSON::Validator.merge_missing_values(@data, @original_data)
+ self.class.merge_missing_values(@data, @original_data)
end
end
def load_ref_schema(parent_schema, ref)
- schema_uri = absolutize_ref_uri(ref, parent_schema.uri)
+ schema_uri = JSON::Util::URI.absolutize_ref(ref, parent_schema.uri)
return true if self.class.schema_loaded?(schema_uri)
+ validator = self.class.validator_for_uri(schema_uri, false)
+ schema_uri = JSON::Util::URI.file_uri(validator.metaschema) if validator
+
schema = @options[:schema_reader].read(schema_uri)
self.class.add_schema(schema)
build_schemas(schema)
end
- def absolutize_ref_uri(ref, parent_schema_uri)
- ref_uri = JSON::Util::URI.strip_fragment(ref)
-
- return ref_uri if ref_uri.absolute?
- # This is a self reference and thus the schema does not need to be re-loaded
- return parent_schema_uri if ref_uri.path.empty?
-
- uri = JSON::Util::URI.strip_fragment(parent_schema_uri.dup)
- Util::URI.normalized_uri(uri.join(ref_uri.path))
- end
-
# Build all schemas with IDs, mapping out the namespace
def build_schemas(parent_schema)
schema = parent_schema.schema
# Build ref schemas if they exist
@@ -223,11 +215,11 @@
def handle_schema(parent_schema, obj)
if obj.is_a?(Hash)
schema_uri = parent_schema.uri.dup
schema = JSON::Schema.new(obj, schema_uri, parent_schema.validator)
if obj['id']
- Validator.add_schema(schema)
+ self.class.add_schema(schema)
end
build_schemas(schema)
end
end
@@ -256,15 +248,19 @@
def validate_uri(schema, data, opts={})
validate(schema, data, opts.merge(:uri => true))
end
def validate!(schema, data,opts={})
- validator = JSON::Validator.new(schema, data, opts)
+ validator = new(schema, data, opts)
validator.validate
end
- alias_method 'validate2', 'validate!'
+ def validate2(schema, data, opts={})
+ warn "[DEPRECATION NOTICE] JSON::Validator#validate2 has been replaced by JSON::Validator#validate! and will be removed in version >= 3. Please use the #validate! method instead."
+ validate!(schema, data, opts)
+ end
+
def validate_json!(schema, data, opts={})
validate!(schema, data, opts.merge(:json => true))
end
def validate_uri!(schema, data, opts={})
@@ -275,11 +271,11 @@
validate!(schema, data, opts.merge(:record_errors => true))
end
def fully_validate_schema(schema, opts={})
data = schema
- schema = JSON::Validator.validator_for_name(opts[:version]).metaschema
+ schema = validator_for_name(opts[:version]).metaschema
fully_validate(schema, data, opts)
end
def fully_validate_json(schema, data, opts={})
fully_validate(schema, data, opts.merge(:json => true))
@@ -297,10 +293,11 @@
@@schema_reader = reader
end
def clear_cache
@@schemas = {}
+ JSON::Util::URI.clear_cache
end
def schemas
@@schemas
end
@@ -335,56 +332,65 @@
def default_validator
@@default_validator
end
- def validator_for_uri(schema_uri)
+ def validator_for_uri(schema_uri, raise_not_found=true)
return default_validator unless schema_uri
u = JSON::Util::URI.parse(schema_uri)
validator = validators["#{u.scheme}://#{u.host}#{u.path}"]
- if validator.nil?
+ if validator.nil? && raise_not_found
raise JSON::Schema::SchemaError.new("Schema not found: #{schema_uri}")
else
validator
end
end
- def validator_for_name(schema_name)
+ def validator_for_name(schema_name, raise_not_found=true)
return default_validator unless schema_name
- validator = validators_for_names([schema_name]).first
- if validator.nil?
+ schema_name = schema_name.to_s
+ validator = validators.values.detect do |v|
+ Array(v.names).include?(schema_name)
+ end
+ if validator.nil? && raise_not_found
raise JSON::Schema::SchemaError.new("The requested JSON schema version is not supported")
else
validator
end
end
- alias_method :validator_for, :validator_for_uri
+ def validator_for(schema_uri)
+ warn "[DEPRECATION NOTICE] JSON::Validator#validator_for has been replaced by JSON::Validator#validator_for_uri and will be removed in version >= 3. Please use the #validator_for_uri method instead."
+ validator_for_uri(schema_uri)
+ end
def register_validator(v)
@@validators["#{v.uri.scheme}://#{v.uri.host}#{v.uri.path}"] = v
end
def register_default_validator(v)
@@default_validator = v
end
- def register_format_validator(format, validation_proc, versions = ["draft1", "draft2", "draft3", "draft4", nil])
+ def register_format_validator(format, validation_proc, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
custom_format_validator = JSON::Schema::CustomFormat.new(validation_proc)
- validators_for_names(versions).each do |validator|
+ versions.each do |version|
+ validator = validator_for_name(version)
validator.formats[format.to_s] = custom_format_validator
end
end
- def deregister_format_validator(format, versions = ["draft1", "draft2", "draft3", "draft4", nil])
- validators_for_names(versions).each do |validator|
+ def deregister_format_validator(format, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
+ versions.each do |version|
+ validator = validator_for_name(version)
validator.formats[format.to_s] = validator.default_formats[format.to_s]
end
end
- def restore_default_formats(versions = ["draft1", "draft2", "draft3", "draft4", nil])
- validators_for_names(versions).each do |validator|
+ def restore_default_formats(versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
+ versions.each do |version|
+ validator = validator_for_name(version)
validator.formats = validator.default_formats.clone
end
end
def json_backend
@@ -485,26 +491,10 @@
@@serializer = lambda{|o| JSON.dump(o) }
else
@@serializer = lambda{|o| YAML.dump(o) }
end
end
-
- private
-
- def validators_for_names(names)
- names = names.map { |name| name.to_s }
- [].tap do |memo|
- validators.each do |_, validator|
- if (validator.names & names).any?
- memo << validator
- end
- end
- if names.include?('')
- memo << default_validator
- end
- end
- end
end
private
if Gem::Specification::find_all_by_name('uuidtools').any?
@@ -530,15 +520,15 @@
def initialize_schema(schema)
if schema.is_a?(String)
begin
# Build a fake URI for this
schema_uri = JSON::Util::URI.parse(fake_uuid(schema))
- schema = JSON::Schema.new(JSON::Validator.parse(schema), schema_uri, @options[:version])
+ schema = JSON::Schema.new(self.class.parse(schema), schema_uri, @options[:version])
if @options[:list] && @options[:fragment].nil?
schema = schema.to_array_schema
end
- Validator.add_schema(schema)
+ self.class.add_schema(schema)
rescue JSON::Schema::JsonParseError
# Build a uri for it
schema_uri = Util::URI.normalized_uri(schema)
if !self.class.schema_loaded?(schema_uri)
schema = @options[:schema_reader].read(schema_uri)
@@ -546,17 +536,17 @@
if @options[:list] && @options[:fragment].nil?
schema = schema.to_array_schema
end
- Validator.add_schema(schema)
+ self.class.add_schema(schema)
else
schema = self.class.schema_for_uri(schema_uri)
if @options[:list] && @options[:fragment].nil?
schema = schema.to_array_schema
schema.uri = JSON::Util::URI.parse(fake_uuid(serialize(schema.schema)))
- Validator.add_schema(schema)
+ self.class.add_schema(schema)
end
schema
end
end
elsif schema.is_a?(Hash)
@@ -564,31 +554,31 @@
schema = JSON::Schema.stringify(schema)
schema = JSON::Schema.new(schema, schema_uri, @options[:version])
if @options[:list] && @options[:fragment].nil?
schema = schema.to_array_schema
end
- Validator.add_schema(schema)
+ self.class.add_schema(schema)
else
raise JSON::Schema::SchemaParseError, "Invalid schema - must be either a string or a hash"
end
schema
end
def initialize_data(data)
if @options[:parse_data]
if @options[:json]
- data = JSON::Validator.parse(data)
+ data = self.class.parse(data)
elsif @options[:uri]
json_uri = Util::URI.normalized_uri(data)
- data = JSON::Validator.parse(custom_open(json_uri))
+ data = self.class.parse(custom_open(json_uri))
elsif data.is_a?(String)
begin
- data = JSON::Validator.parse(data)
+ data = self.class.parse(data)
rescue JSON::Schema::JsonParseError
begin
json_uri = Util::URI.normalized_uri(data)
- data = JSON::Validator.parse(custom_open(json_uri))
+ data = self.class.parse(custom_open(json_uri))
rescue JSON::Schema::JsonLoadError
# Silently discard the error - use the data as-is
end
end
end