Sha256: 71c76093ee86489bd0c36baf4d50b49e6c9fdeeed689cf55d6ffbc9f1e363ae3

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'pathname'

module JSON
  class Schema

    attr_accessor :schema, :uri, :validator
    
    def initialize(schema,uri,parent_validator=nil)
      @schema = schema
      @uri = uri
      
      # If there is an ID on this schema, use it to generate the URI
      if @schema['id']
        temp_uri = URI.parse(@schema['id'])
        if temp_uri.relative?
          uri = uri.merge(@schema['id'])
          temp_uri = uri
        end
        @uri = temp_uri
      end
      @uri.fragment = ''
      
      # If there is a $schema on this schema, use it to determine which validator to use
      if @schema['$schema']
        u = URI.parse(@schema['$schema'])
        @validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"]
        if @validator.nil?
          raise SchemaError.new("This library does not have support for schemas defined by #{u.scheme}://#{u.host}#{u.path}")
        end
      elsif parent_validator
        @validator = parent_validator
      else
        @validator = JSON::Validator.default_validator
      end  
    end
    
    def validate(data, fragments, processor, options = {})
      @validator.validate(self, data, fragments, processor, options)
    end
    
    def base_uri
      parts = @uri.to_s.split('/')
      parts.pop
      parts.join('/') + '/'
    end

    def to_s
      @schema.to_json
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
json-schema-1.2.1 lib/json-schema/schema.rb
json-schema-1.2.0 lib/json-schema/schema.rb
json-schema-1.1.1 lib/json-schema/schema.rb
json-schema-1.1.0 lib/json-schema/schema.rb