Module: JSONSchemer

Defined in:
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

Defined Under Namespace

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

Constant Summary collapse

DRAFT_CLASS_BY_META_SCHEMA =
{
  '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#'
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?
  JSON.parse(File.read(uri.path))
end
VERSION =
'0.2.9'

Class Method Summary collapse

Class Method Details

.schema(schema, **options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/json_schemer.rb', line 45

def schema(schema, **options)
  case schema
  when String
    schema = JSON.parse(schema)
  when Pathname
    uri = URI.parse("file://#{schema.realpath}")
    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