lib/json-schema/validator.rb in json-schema-1.2.1 vs lib/json-schema/validator.rb in json-schema-2.0.0

- old
+ new

@@ -109,24 +109,25 @@ :list => false, :version => nil, :validate_schema => false, :record_errors => false, :errors_as_objects => false, - :insert_defaults => false, - :strict => false + :insert_defaults => false } @@validators = {} @@default_validator = nil @@available_json_backends = [] @@json_backend = nil @@serializer = nil @@mutex = Mutex.new def self.version_string_for(version) # I'm not a fan of this, but it's quick and dirty to get it working for now - return "draft-03" unless version + return "draft-04" unless version case version.to_s + when "draft4" + "draft-04" when "draft3" "draft-03" when "draft2" "draft-02" when "draft1" @@ -143,21 +144,20 @@ def initialize(schema_data, data, opts={}) @options = @@default_opts.clone.merge(opts) @errors = [] # I'm not a fan of this, but it's quick and dirty to get it working for now - version_string = "draft-03" + version_string = "draft-04" if @options[:version] version_string = @options[:version] = self.class.version_string_for(@options[:version]) u = URI.parse("http://json-schema.org/#{@options[:version]}/schema#") validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"] @options[:version] = validator end @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 # validate the schema, if requested if @options[:validate_schema] begin meta_validator = JSON::Validator.new(self.class.metaschema_for(version_string), schema_data) @@ -246,10 +246,17 @@ end end end end + # "definitions" are schemas in V4 + if parent_schema.schema["definitions"] + parent_schema.schema["definitions"].each do |k,v| + handle_schema(parent_schema, v) + end + end + # All properties are schemas if parent_schema.schema["properties"] parent_schema.schema["properties"].each do |k,v| handle_schema(parent_schema, v) end @@ -284,11 +291,11 @@ # Either load a reference schema or create a new schema def handle_schema(parent_schema, obj) if obj.is_a?(Hash) schema_uri = parent_schema.uri.clone - schema = JSON::Schema.new(obj,schema_uri,@options[:version]) + schema = JSON::Schema.new(obj,schema_uri,parent_schema.validator) if obj['id'] Validator.add_schema(schema) end build_schemas(schema) end @@ -502,11 +509,15 @@ begin # Build a fake URI for this schema_uri = URI.parse(fake_uri(schema)) schema = JSON::Validator.parse(schema) if @options[:list] - schema = {"type" => "array", "items" => schema} + new_schema = {"type" => "array", "items" => schema} + if !schema["$schema"].nil? + new_schema["$schema"] = schema["$schema"] + end + schema = new_schema end schema = JSON::Schema.new(schema,schema_uri,@options[:version]) Validator.add_schema(schema) rescue # Build a uri for it @@ -520,20 +531,28 @@ end end if Validator.schemas[schema_uri.to_s].nil? schema = JSON::Validator.parse(open(schema_uri.to_s).read) if @options[:list] - schema = {"type" => "array", "items" => schema} + new_schema = {"type" => "array", "items" => schema} + if !schema["$schema"].nil? + new_schema["$schema"] = schema["$schema"] + end + schema = new_schema end schema = JSON::Schema.new(schema,schema_uri,@options[:version]) Validator.add_schema(schema) else schema = Validator.schemas[schema_uri.to_s] end end elsif schema.is_a?(Hash) if @options[:list] - schema = {"type" => "array", "items" => schema} + new_schema = {"type" => "array", "items" => schema} + if !schema["$schema"].nil? + new_schema["$schema"] = schema["$schema"] + end + schema = new_schema end schema_uri = URI.parse(fake_uri(serialize(schema))) schema = JSON::Schema.new(schema,schema_uri,@options[:version]) Validator.add_schema(schema) else