Skip to content

Commit 3334f97

Browse files
committed
feat: throw for reference not found
This change makes the distinction between schema problems and validation of the instance errors.
1 parent 9c0c863 commit 3334f97

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Sources/Core/ref.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
enum ReferenceError: Error {
2+
case notFound
3+
}
4+
15
func ref(context: Context, reference: Any, instance: Any, schema: [String: Any]) throws -> AnySequence<ValidationError> {
26
guard let reference = reference as? String else {
37
return AnySequence(EmptyCollection())
48
}
59

610
guard let document = context.resolve(ref: reference) else {
7-
return AnySequence([
8-
ValidationError("Reference not found '\(reference)'", instanceLocation: nil),
9-
])
11+
throw ReferenceError.notFound
1012
}
1113

1214
let id: String?

Tests/JSONSchemaTests/JSONSchemaTests.swift

+14
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,18 @@ class ValidateTests: XCTestCase {
167167
XCTAssertEqual(error.instanceLocation?.path, "/0")
168168
}
169169
}
170+
171+
func testReferenceNotFound() {
172+
let schema: [String: Any] = [
173+
"$ref": "#/unknown",
174+
]
175+
176+
do {
177+
_ = try validate("anything", schema: schema)
178+
} catch {
179+
return
180+
}
181+
182+
XCTFail("Validation did not throw error")
183+
}
170184
}

0 commit comments

Comments
 (0)