Skip to content

Commit ed6041e

Browse files
committed
feat: error out when encountering unknown meta schemas
BREAKING CHANGE: Unknown $schema have previously have been treated as draft 4
1 parent a3a33cf commit ed6041e

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# JSONSchema Changelog
22

3+
## TBD
4+
5+
### Breaking Changes
6+
7+
- When a schema references an unknown meta schema via `$schema`, a reference
8+
error will now be thrown instead of the schema being silently treated as a
9+
draft 4 schema.
10+
311
## 0.6.0
412

513
### Breaking Changes

Sources/JSONSchema.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,27 @@ public struct Schema {
5252
}
5353

5454
public func validate(_ data: Any) throws -> ValidationResult {
55-
let validator = JSONSchema.validator(for: schema)
55+
let validator = try JSONSchema.validator(for: schema)
5656
return try validator.validate(instance: data)
5757
}
5858

5959
public func validate(_ data: Any) throws -> AnySequence<ValidationError> {
60-
let validator = JSONSchema.validator(for: schema)
60+
let validator = try JSONSchema.validator(for: schema)
6161
return try validator.validate(instance: data)
6262
}
6363
}
6464

6565

66-
func validator(for schema: [String: Any]) -> Validator {
67-
guard let schemaURI = schema["$schema"] as? String else {
66+
func validator(for schema: [String: Any]) throws -> Validator {
67+
guard schema.keys.contains("$schema") else {
68+
// Default schema
6869
return Draft4Validator(schema: schema)
6970
}
7071

72+
guard let schemaURI = schema["$schema"] as? String else {
73+
throw ReferenceError.notFound
74+
}
75+
7176
if let id = DRAFT_2020_12_META_SCHEMA["$id"] as? String, schemaURI == id {
7277
return Draft202012Validator(schema: schema)
7378
}
@@ -84,11 +89,11 @@ func validator(for schema: [String: Any]) -> Validator {
8489
return Draft6Validator(schema: schema)
8590
}
8691

87-
if let id = DRAFT_04_META_SCHEMA["$id"] as? String, schemaURI == id {
92+
if let id = DRAFT_04_META_SCHEMA["id"] as? String, schemaURI == id {
8893
return Draft4Validator(schema: schema)
8994
}
9095

91-
return Draft4Validator(schema: schema)
96+
throw ReferenceError.notFound
9297
}
9398

9499

Tests/JSONSchemaTests/JSONSchemaTests.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,42 @@ class ValidateTests: XCTestCase {
108108
try XCTAssertFalse(validate(["price": 34.99], schema: schema).valid)
109109
}
110110

111-
func testDraft6ValidatorIsAvailable() {
112-
let result = validator(for: ["$schema": "http://json-schema.org/draft-06/schema#"])
111+
func testDefaultValidator() throws {
112+
let result = try validator(for: [:])
113+
XCTAssertTrue(result is Draft4Validator, "Unexpected type of validator \(result)")
114+
}
115+
116+
func testDraft4ValidatorIsAvailable() throws {
117+
let result = try validator(for: ["$schema": "http://json-schema.org/draft-04/schema#"])
118+
XCTAssertTrue(result is Draft4Validator, "Unexpected type of validator \(result)")
119+
}
120+
121+
func testDraft6ValidatorIsAvailable() throws {
122+
let result = try validator(for: ["$schema": "http://json-schema.org/draft-06/schema#"])
113123
XCTAssertTrue(result is Draft6Validator, "Unexpected type of validator \(result)")
114124
}
115125

126+
func testDraft7ValidatorIsAvailable() throws {
127+
let result = try validator(for: ["$schema": "http://json-schema.org/draft-07/schema#"])
128+
XCTAssertTrue(result is Draft7Validator, "Unexpected type of validator \(result)")
129+
}
130+
131+
func testDraft201909ValidatorIsAvailable() throws {
132+
let result = try validator(for: ["$schema": "https://json-schema.org/draft/2019-09/schema"])
133+
XCTAssertTrue(result is Draft201909Validator, "Unexpected type of validator \(result)")
134+
}
135+
136+
func testDraft202012ValidatorIsAvailable() throws {
137+
let result = try validator(for: ["$schema": "https://json-schema.org/draft/2020-12/schema"])
138+
XCTAssertTrue(result is Draft202012Validator, "Unexpected type of validator \(result)")
139+
}
140+
141+
func testUnknownValidator() throws {
142+
XCTAssertThrowsError(
143+
try validator(for: ["$schema": "https://example.com/schema"])
144+
)
145+
}
146+
116147
func testValidateDraft7() throws {
117148
let schema: [String: Any] = [
118149
"$schema": "http://json-schema.org/draft-07/schema#",

0 commit comments

Comments
 (0)