Skip to content

Commit 5c3dcc8

Browse files
committed
Use current draft as default for child schemas
This will keep using the same draft for any remote `$ref` schemas that don't explicitly set `$schema`. It's necessary so that we know which keywords to use for things that have changed between drafts (eg, `id` vs. `$id`). Exposed by: json-schema-org/JSON-Schema-Test-Suite#454
1 parent eafb7dc commit 5c3dcc8

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

lib/json_schemer.rb

+9-11
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,13 @@ class InvalidRegexpResolution < StandardError; end
3333
class InvalidFileURI < StandardError; end
3434
class InvalidSymbolKey < StandardError; end
3535

36-
DRAFT_CLASS_BY_META_SCHEMA = {
36+
SCHEMA_CLASS_BY_META_SCHEMA = {
3737
'http://json-schema.org/schema#' => Schema::Draft4, # Version-less $schema deprecated after Draft 4
3838
'http://json-schema.org/draft-04/schema#' => Schema::Draft4,
3939
'http://json-schema.org/draft-06/schema#' => Schema::Draft6,
4040
'http://json-schema.org/draft-07/schema#' => Schema::Draft7
4141
}.freeze
4242

43-
DEFAULT_META_SCHEMA = 'http://json-schema.org/draft-07/schema#'
44-
4543
WINDOWS_URI_PATH_REGEX = /\A\/[a-z]:/i
4644

4745
FILE_URI_REF_RESOLVER = proc do |uri|
@@ -53,7 +51,7 @@ class InvalidSymbolKey < StandardError; end
5351
end
5452

5553
class << self
56-
def schema(schema, **options)
54+
def schema(schema, default_schema_class: Schema::Draft7, **options)
5755
case schema
5856
when String
5957
schema = JSON.parse(schema)
@@ -66,16 +64,16 @@ def schema(schema, **options)
6664
schema = ref_resolver.call(uri)
6765
options[:ref_resolver] = ref_resolver
6866
end
69-
schema[draft_class(schema)::ID_KEYWORD] ||= uri.to_s
7067
end
71-
draft_class(schema).new(schema, **options)
72-
end
7368

74-
private
69+
schema_class = if schema.is_a?(Hash) && schema.key?('$schema')
70+
meta_schema = schema.fetch('$schema')
71+
SCHEMA_CLASS_BY_META_SCHEMA[meta_schema] || raise(UnsupportedMetaSchema, meta_schema)
72+
else
73+
default_schema_class
74+
end
7575

76-
def draft_class(schema)
77-
meta_schema = schema.is_a?(Hash) && schema.key?('$schema') ? schema['$schema'] : DEFAULT_META_SCHEMA
78-
DRAFT_CLASS_BY_META_SCHEMA[meta_schema] || raise(UnsupportedMetaSchema, meta_schema)
76+
schema_class.new(schema, **options)
7977
end
8078
end
8179
end

lib/json_schemer/schema/base.rb

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ def spec_format?(format)
249249
def child(schema)
250250
JSONSchemer.schema(
251251
schema,
252+
default_schema_class: self.class,
252253
format: format?,
253254
formats: formats,
254255
keywords: keywords,

0 commit comments

Comments
 (0)