Skip to content

Commit 7cd81b5

Browse files
committed
feat: support keywordLocation for $ref relative
1 parent 2d0f17f commit 7cd81b5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Sources/Validator.swift

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Context {
3434
if validator is Draft4Validator || validator is Draft6Validator {
3535
// Older versions of JSON Schema, $ref ignores any alongside keywords
3636
if let ref = schema["$ref"] as? String {
37+
keywordLocation.push("$ref")
38+
defer { keywordLocation.pop() }
3739
let validation = validator.validations["$ref"]!
3840
return try validation(self, ref, instance, schema)
3941
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import XCTest
2+
@testable import JSONSchema
3+
4+
5+
class RefTests: XCTestCase {
6+
func testRefWithErrorInSubschema() throws {
7+
let schema: [String: Any] = [
8+
"$schema": "http://json-schema.org/draft-07/schema#",
9+
"items": [
10+
"$ref": "#/definitions/name",
11+
],
12+
"definitions": [
13+
"name": [
14+
"type": "string",
15+
],
16+
],
17+
]
18+
19+
let result = try validate([true], schema: schema)
20+
21+
switch result {
22+
case .valid:
23+
XCTFail("Validation should fail")
24+
case .invalid(let errors):
25+
XCTAssertEqual(errors.count, 1)
26+
let error = errors[0]
27+
28+
XCTAssertEqual(error.description, "'true' is not of type 'string'")
29+
XCTAssertEqual(error.instanceLocation.path, "/0")
30+
XCTAssertEqual(error.keywordLocation.path, "#/items/$ref/type")
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)