Sha256: 6e409ead5180bb0d812661ed70a682d44fe8065793824b67c3cb17de31a1e5d0

Contents?: true

Size: 1.69 KB

Versions: 26

Compression:

Stored size: 1.69 KB

Contents

require 'json-schema/attributes/ref'

module JSON
  class Schema
    class ExtendsAttribute < Attribute
      def self.validate(current_schema, data, fragments, processor, validator, options = {})
        schemas = current_schema.schema['extends']
        schemas = [schemas] if !schemas.is_a?(Array)
        schemas.each do |s|
          uri,schema = get_extended_uri_and_schema(s, current_schema, validator)
          if schema
            schema.validate(data, fragments, processor, options)
          elsif uri
            message = "The extended schema '#{uri.to_s}' cannot be found"
            validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
          else
            message = "The property '#{build_fragment(fragments)}' was not a valid schema"
            validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
          end
        end
      end

      def self.get_extended_uri_and_schema(s, current_schema, validator)
        uri,schema = nil,nil

        s = {'$ref' => s} if s.is_a?(String)

        if s.is_a?(Hash)
          uri = current_schema.uri
          if s['$ref']
            ref_uri,ref_schema = JSON::Schema::RefAttribute.get_referenced_uri_and_schema(s, current_schema, validator)
            if ref_schema
              if s.size == 1 # Check if anything else apart from $ref
                uri,schema = ref_uri,ref_schema
              else
                s = s.dup
                s.delete '$ref'
                s = ref_schema.schema.merge(s)
              end
            end
          end
          schema ||= JSON::Schema.new(s,uri,validator)
        end

        [uri,schema]
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
json-schema-2.2.5 lib/json-schema/attributes/extends.rb
json-schema-2.2.4 lib/json-schema/attributes/extends.rb
json-schema-2.2.3 lib/json-schema/attributes/extends.rb
json-schema-2.2.2 lib/json-schema/attributes/extends.rb
json-schema-2.2.1 lib/json-schema/attributes/extends.rb
json-schema-1.2.1 lib/json-schema/attributes/extends.rb
json-schema-2.2.0 lib/json-schema/attributes/extends.rb
json-schema-1.2.0 lib/json-schema/attributes/extends.rb
json-schema-2.1.9 lib/json-schema/attributes/extends.rb
json-schema-2.1.8 lib/json-schema/attributes/extends.rb
json-schema-2.1.7 lib/json-schema/attributes/extends.rb
json-schema-2.1.6 lib/json-schema/attributes/extends.rb
json-schema-2.1.5 lib/json-schema/attributes/extends.rb
json-schema-2.1.4 lib/json-schema/attributes/extends.rb
json-schema-2.1.3 lib/json-schema/attributes/extends.rb
json-schema-2.1.2 lib/json-schema/attributes/extends.rb
json-schema-2.1.1 lib/json-schema/attributes/extends.rb
json-schema-2.1.0 lib/json-schema/attributes/extends.rb
json-schema-2.0.5 lib/json-schema/attributes/extends.rb
json-schema-2.0.4 lib/json-schema/attributes/extends.rb