lib/json-schema/validator.rb in json-schema-2.6.2 vs lib/json-schema/validator.rb in json-schema-2.7.0
- old
+ new
@@ -16,19 +16,19 @@
module JSON
class Validator
@@schemas = {}
- @@cache_schemas = false
+ @@cache_schemas = true
@@default_opts = {
:list => false,
:version => nil,
:validate_schema => false,
:record_errors => false,
:errors_as_objects => false,
:insert_defaults => false,
- :clear_cache => true,
+ :clear_cache => false,
:strict => false,
:parse_data => true
}
@@validators = {}
@@default_validator = nil
@@ -46,11 +46,11 @@
@options[:schema_reader] ||= JSON::Validator.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] = false if @options[:clear_cache] == false
+ @validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]
@@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
@original_data = data
@data = initialize_data(data)
@@mutex.synchronize { build_schemas(@base_schema) }
@@ -60,12 +60,11 @@
if @base_schema.schema["$schema"]
base_validator = JSON::Validator.validator_for_name(@base_schema.schema["$schema"])
end
metaschema = base_validator ? base_validator.metaschema : validator.metaschema
# Don't clear the cache during metaschema validation!
- meta_validator = JSON::Validator.new(metaschema, @base_schema.schema, {:clear_cache => false})
- meta_validator.validate
+ self.class.validate!(metaschema, @base_schema.schema, {:clear_cache => false})
end
# If the :fragment option is set, try and validate against the fragment
if opts[:fragment]
@base_schema = schema_from_fragment(@base_schema, opts[:fragment])
@@ -108,16 +107,21 @@
base_schema
end
end
# Run a simple true/false validation of data against a schema
- def validate()
+ def validate
@base_schema.validate(@data,[],self,@validation_options)
- if @options[:errors_as_objects]
- return @errors.map{|e| e.to_hash}
+
+ if @options[:record_errors]
+ if @options[:errors_as_objects]
+ @errors.map{|e| e.to_hash}
+ else
+ @errors.map{|e| e.to_string}
+ end
else
- return @errors.map{|e| e.to_string}
+ true
end
ensure
if @validation_options[:clear_cache] == true
Validator.clear_cache
end
@@ -237,13 +241,11 @@
class << self
def validate(schema, data,opts={})
begin
- validator = JSON::Validator.new(schema, data, opts)
- validator.validate
- return true
+ validate!(schema, data, opts)
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
return false
end
end
@@ -256,11 +258,10 @@
end
def validate!(schema, data,opts={})
validator = JSON::Validator.new(schema, data, opts)
validator.validate
- return true
end
alias_method 'validate2', 'validate!'
def validate_json!(schema, data, opts={})
validate!(schema, data, opts.merge(:json => true))
@@ -269,13 +270,11 @@
def validate_uri!(schema, data, opts={})
validate!(schema, data, opts.merge(:uri => true))
end
def fully_validate(schema, data, opts={})
- opts[:record_errors] = true
- validator = JSON::Validator.new(schema, data, opts)
- validator.validate
+ 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
@@ -297,10 +296,10 @@
def schema_reader=(reader)
@@schema_reader = reader
end
def clear_cache
- @@schemas = {} if @@cache_schemas == false
+ @@schemas = {}
end
def schemas
@@schemas
end