Module: JSONSchemer

Defined in:
lib/json_schemer/errors.rb,
lib/json_schemer.rb,
lib/json_schemer/format.rb,
lib/json_schemer/version.rb,
lib/json_schemer/schema/base.rb,
lib/json_schemer/schema/draft4.rb,
lib/json_schemer/schema/draft6.rb,
lib/json_schemer/schema/draft7.rb,
lib/json_schemer/cached_ref_resolver.rb

Overview

Based on code from @robacarp found in issue 48: github.com/davishmcclurg/json_schemer/issues/48

Defined Under Namespace

Modules: Errors, Format, Schema Classes: CachedRefResolver, InvalidFileURI, InvalidRefResolution, InvalidSymbolKey, UnknownRef, UnsupportedMetaSchema

Constant Summary collapse

DRAFT_CLASS_BY_META_SCHEMA =
{
  'http://json-schema.org/schema#' => Schema::Draft4, # Version-less $schema deprecated after Draft 4
  'http://json-schema.org/draft-04/schema#' => Schema::Draft4,
  'http://json-schema.org/draft-06/schema#' => Schema::Draft6,
  'http://json-schema.org/draft-07/schema#' => Schema::Draft7
}.freeze
DEFAULT_META_SCHEMA =
'http://json-schema.org/draft-07/schema#'
WINDOWS_URI_PATH_REGEX =
/\A\/[a-z]:/i
FILE_URI_REF_RESOLVER =
proc do |uri|
  raise InvalidFileURI, 'must use `file` scheme' unless uri.scheme == 'file'
  raise InvalidFileURI, 'cannot have a host (use `file:///`)' if uri.host && !uri.host.empty?
  path = uri.path
  path = path[1..-1] if path.match?(WINDOWS_URI_PATH_REGEX)
  JSON.parse(File.read(URI::DEFAULT_PARSER.unescape(path)))
end
VERSION =
'0.2.21'

Class Method Summary collapse

Class Method Details

.schema(schema, **options) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/json_schemer.rb', line 53

def schema(schema, **options)
  case schema
  when String
    schema = JSON.parse(schema)
  when Pathname
    uri = URI.parse(File.join('file:', URI::DEFAULT_PARSER.escape(schema.realpath.to_s)))
    if options.key?(:ref_resolver)
      schema = FILE_URI_REF_RESOLVER.call(uri)
    else
      ref_resolver = CachedRefResolver.new(&FILE_URI_REF_RESOLVER)
      schema = ref_resolver.call(uri)
      options[:ref_resolver] = ref_resolver
    end
    schema[draft_class(schema)::ID_KEYWORD] ||= uri.to_s
  end
  draft_class(schema).new(schema, **options)
end