Skip to content

Commit 3d5cd2c

Browse files
committed
feat: support dependentRequired
1 parent 99365e4 commit 3d5cd2c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Sources/Draft201909Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class Draft201909Validator: Validator {
1010
"$ref": ref,
1111
"type": type,
1212
"required": required,
13+
"dependentRequired": dependentRequired,
1314
"propertyNames": propertyNames,
1415
"not": not,
1516
"pattern": pattern,

Sources/Validators.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,25 @@ func required(validator: Validator, required: Any, instance: Any, schema: [Strin
542542
return .invalid(["Required properties are missing '\(required)'"])
543543
}
544544

545+
func dependentRequired(validator: Validator, dependentRequired: Any, instance: Any, schema: [String: Any]) -> ValidationResult {
546+
guard let instance = instance as? [String: Any] else {
547+
return .valid
548+
}
549+
550+
guard let dependentRequired = dependentRequired as? [String: [String]] else {
551+
return .valid
552+
}
553+
554+
return flatten(dependentRequired.compactMap({ (key, required) -> ValidationResult? in
555+
if instance.keys.contains(key) {
556+
return JSONSchema.required(validator: validator, required: required, instance: instance, schema: schema)
557+
}
558+
559+
return nil
560+
}))
561+
}
562+
563+
545564
func propertyNames(validator: Validator, propertyNames: Any, instance: Any, schema: [String: Any]) -> ValidationResult {
546565
guard let instance = instance as? [String: Any] else {
547566
return .valid

Tests/JSONSchemaTests/JSONSchemaCases.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ class JSONSchemaCases: XCTestCase {
123123
"ref.json",
124124
"refRemote.json",
125125
"anchor.json",
126-
"dependentRequired.json",
127126
"dependentSchemas.json",
128127
"unevaluatedProperties.json",
129128
"unevaluatedItems.json",

0 commit comments

Comments
 (0)