Skip to content

Commit 78cee2a

Browse files
committed
feat: match metaschema with or without empty fragment
1 parent 519db4c commit 78cee2a

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
error will now be thrown instead of the schema being silently treated as a
1313
draft 4 schema.
1414

15+
### Enhancements
16+
17+
- `$schema` matching will now operate on normalise URIs and thus will handle
18+
cases when the meta spec or the `$schema` URI both contain and don't contain
19+
empty trailing fragments as the same meta schema.
20+
1521
## 0.6.0
1622

1723
### Breaking Changes

Sources/JSONSchema.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ func validator(for schema: [String: Any]) throws -> Validator {
7373
throw ReferenceError.notFound
7474
}
7575

76-
if let id = DRAFT_2020_12_META_SCHEMA["$id"] as? String, schemaURI == id {
76+
if let id = DRAFT_2020_12_META_SCHEMA["$id"] as? String, urlEqual(schemaURI, id) {
7777
return Draft202012Validator(schema: schema)
7878
}
7979

80-
if let id = DRAFT_2019_09_META_SCHEMA["$id"] as? String, schemaURI == id {
80+
if let id = DRAFT_2019_09_META_SCHEMA["$id"] as? String, urlEqual(schemaURI, id) {
8181
return Draft201909Validator(schema: schema)
8282
}
8383

84-
if let id = DRAFT_07_META_SCHEMA["$id"] as? String, schemaURI == id {
84+
if let id = DRAFT_07_META_SCHEMA["$id"] as? String, urlEqual(schemaURI, id) {
8585
return Draft7Validator(schema: schema)
8686
}
8787

88-
if let id = DRAFT_06_META_SCHEMA["$id"] as? String, schemaURI == id {
88+
if let id = DRAFT_06_META_SCHEMA["$id"] as? String, urlEqual(schemaURI, id) {
8989
return Draft6Validator(schema: schema)
9090
}
9191

92-
if let id = DRAFT_04_META_SCHEMA["id"] as? String, schemaURI == id {
92+
if let id = DRAFT_04_META_SCHEMA["id"] as? String, urlEqual(schemaURI, id) {
9393
return Draft4Validator(schema: schema)
9494
}
9595

Sources/RefResolver.swift

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func urlNormalise(_ value: String) -> String {
3232
}
3333

3434

35+
func urlEqual(_ lhs: String, _ rhs: String) -> Bool {
36+
return urlNormalise(lhs) == urlNormalise(rhs)
37+
}
38+
39+
3540
class RefResolver {
3641
let referrer: [String: Any]
3742
var store: [String: Any]

Tests/JSONSchemaTests/JSONSchemaTests.swift

+25
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,51 @@ class ValidateTests: XCTestCase {
118118
XCTAssertTrue(result is Draft4Validator, "Unexpected type of validator \(result)")
119119
}
120120

121+
func testDraft4ValidatorIsAvailableWithoutFragment() throws {
122+
let result = try validator(for: ["$schema": "http://json-schema.org/draft-04/schema"])
123+
XCTAssertTrue(result is Draft4Validator, "Unexpected type of validator \(result)")
124+
}
125+
121126
func testDraft6ValidatorIsAvailable() throws {
122127
let result = try validator(for: ["$schema": "http://json-schema.org/draft-06/schema#"])
123128
XCTAssertTrue(result is Draft6Validator, "Unexpected type of validator \(result)")
124129
}
125130

131+
func testDraft6ValidatorIsAvailableWithoutFragment() throws {
132+
let result = try validator(for: ["$schema": "http://json-schema.org/draft-06/schema"])
133+
XCTAssertTrue(result is Draft6Validator, "Unexpected type of validator \(result)")
134+
}
135+
126136
func testDraft7ValidatorIsAvailable() throws {
127137
let result = try validator(for: ["$schema": "http://json-schema.org/draft-07/schema#"])
128138
XCTAssertTrue(result is Draft7Validator, "Unexpected type of validator \(result)")
129139
}
130140

141+
func testDraft7ValidatorIsAvailableWithoutFragment() throws {
142+
let result = try validator(for: ["$schema": "http://json-schema.org/draft-07/schema"])
143+
XCTAssertTrue(result is Draft7Validator, "Unexpected type of validator \(result)")
144+
}
145+
131146
func testDraft201909ValidatorIsAvailable() throws {
132147
let result = try validator(for: ["$schema": "https://json-schema.org/draft/2019-09/schema"])
133148
XCTAssertTrue(result is Draft201909Validator, "Unexpected type of validator \(result)")
134149
}
135150

151+
func testDraft201909ValidatorIsAvailableWithTrailingFragment() throws {
152+
let result = try validator(for: ["$schema": "https://json-schema.org/draft/2019-09/schema#"])
153+
XCTAssertTrue(result is Draft201909Validator, "Unexpected type of validator \(result)")
154+
}
155+
136156
func testDraft202012ValidatorIsAvailable() throws {
137157
let result = try validator(for: ["$schema": "https://json-schema.org/draft/2020-12/schema"])
138158
XCTAssertTrue(result is Draft202012Validator, "Unexpected type of validator \(result)")
139159
}
140160

161+
func testDraft202012ValidatorIsAvailableWithTrailingFragment() throws {
162+
let result = try validator(for: ["$schema": "https://json-schema.org/draft/2020-12/schema#"])
163+
XCTAssertTrue(result is Draft202012Validator, "Unexpected type of validator \(result)")
164+
}
165+
141166
func testUnknownValidator() throws {
142167
XCTAssertThrowsError(
143168
try validator(for: ["$schema": "https://example.com/schema"])

0 commit comments

Comments
 (0)