Skip to content

Commit e6a4168

Browse files
authored
Accept https:// JSON Schema URIs (#98)
The JSON Schema spec calls $schema an identifier, not a literal URL, and json-schema.org now serves both http:// and https:// forms. Tools commonly emit https://, but `schema_module/2` and `remote_schema/1` only matched the canonical http:// prefix and raised UnsupportedSchemaVersionError on any https:// input. Normalize https://json-schema.org/... to http://json-schema.org/... at the top of both functions. No behavioural change for any other input. Closes #97
1 parent 23447ac commit e6a4168

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/ex_json_schema/schema.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ defmodule ExJsonSchema.Schema do
163163
end
164164

165165
defp schema_module(schema_url, default \\ Draft7)
166+
167+
defp schema_module("https://json-schema.org/" <> rest, default),
168+
do: schema_module("http://json-schema.org/" <> rest, default)
169+
166170
defp schema_module(@draft4_schema_url <> _, _), do: Draft4
167171
defp schema_module(@draft6_schema_url <> _, _), do: Draft6
168172
defp schema_module(@draft7_schema_url <> _, _), do: Draft7
@@ -257,6 +261,9 @@ defmodule ExJsonSchema.Schema do
257261
end
258262

259263
@spec remote_schema(String.t()) :: ExJsonSchema.object()
264+
defp remote_schema("https://json-schema.org/" <> rest),
265+
do: remote_schema("http://json-schema.org/" <> rest)
266+
260267
defp remote_schema(@current_draft_schema_url <> _), do: Draft7.schema()
261268
defp remote_schema(@draft4_schema_url <> _), do: Draft4.schema()
262269
defp remote_schema(@draft6_schema_url <> _), do: Draft6.schema()

test/ex_json_schema/schema_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ defmodule ExJsonSchema.SchemaTest do
3131
assert resolve(schema) == %ExJsonSchema.Schema.Root{refs: %{}, schema: schema, version: 7}
3232
end
3333

34+
test "accepts both http:// and https:// forms of the JSON Schema URI" do
35+
http_schema = %{"$schema" => "http://json-schema.org/draft-07/schema#"}
36+
https_schema = %{"$schema" => "https://json-schema.org/draft-07/schema#"}
37+
38+
assert resolve(http_schema).version == 7
39+
assert resolve(https_schema).version == 7
40+
41+
http_draft6 = %{"$schema" => "http://json-schema.org/draft-06/schema#"}
42+
https_draft6 = %{"$schema" => "https://json-schema.org/draft-06/schema#"}
43+
44+
assert resolve(http_draft6).version == 6
45+
assert resolve(https_draft6).version == 6
46+
end
47+
3448
test "schema is validated against its meta-schema" do
3549
schema = %{"properties" => "foo"}
3650

0 commit comments

Comments
 (0)