Skip to content

Commit 8c7ec15

Browse files
VictorAGAkylef
VictorAGA
authored andcommitted
feat: Add support for date-time format
1 parent f49a85d commit 8c7ec15

File tree

7 files changed

+52
-11
lines changed

7 files changed

+52
-11
lines changed

Sources/Draft201909Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class Draft201909Validator: Validator {
6464
"duration": validateDuration,
6565
"time": validateTime,
6666
"date": validateDate,
67+
"date-time": validateDateTime,
6768
]
6869

6970
public required init(schema: Bool) {

Sources/Draft202012Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class Draft202012Validator: Validator {
6565
"duration": validateDuration,
6666
"time": validateTime,
6767
"date": validateDate,
68+
"date-time": validateDateTime,
6869
]
6970

7071
public required init(schema: Bool) {

Sources/Draft4Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class Draft4Validator: Validator {
4545
"ipv4": validateIPv4,
4646
"ipv6": validateIPv6,
4747
"uri": validateURI,
48+
"date-time": validateDateTime,
4849
]
4950

5051
public required init(schema: Bool) {

Sources/Draft7Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class Draft7Validator: Validator {
5252
"regex": validateRegex,
5353
"time": validateTime,
5454
"date": validateDate,
55+
"date-time": validateDateTime,
5556
]
5657

5758
public required init(schema: Bool) {

Sources/Format/date-time.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
3+
func isValidDateTime(_ value: String) -> Bool {
4+
var valueArray = value.components(separatedBy: "T")
5+
if valueArray.count < 2 {
6+
valueArray = value.components(separatedBy: "t")
7+
if valueArray.count < 2 {
8+
valueArray = value.components(separatedBy: " ")
9+
if valueArray.count < 2 {
10+
return false
11+
}
12+
}
13+
}
14+
15+
let date = valueArray[0]
16+
let time = valueArray[1]
17+
18+
if isValidDate(date) && isValidTime(time) {
19+
return true
20+
} else {
21+
return false
22+
}
23+
}
24+
25+
func validateDateTime(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
26+
if isValidDateTime(value) {
27+
return AnySequence(EmptyCollection())
28+
}
29+
30+
return AnySequence([
31+
ValidationError(
32+
"'\(value)' is not a valid RFC 3339 formatted time.",
33+
instanceLocation: context.instanceLocation,
34+
keywordLocation: context.keywordLocation
35+
)
36+
])
37+
}

Sources/Format/date.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import Foundation
22

3-
4-
func validateDate(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
3+
func isValidDate(_ value: String) -> Bool {
54
if let regularExpression = try? NSRegularExpression(pattern: "^\\d{4}-\\d{2}-\\d{2}$", options: []) {
65
let range = NSRange(location: 0, length: value.utf16.count)
76
let result = regularExpression.matches(in: value, options: [], range: range)
87
if result.isEmpty {
9-
return AnySequence([
10-
ValidationError(
11-
"'\(value)' is not a valid RFC 3339 formatted date.",
12-
instanceLocation: context.instanceLocation,
13-
keywordLocation: context.keywordLocation
14-
)
15-
])
8+
return false
169
}
1710
}
1811

1912
let rfc3339DateTimeFormatter = DateFormatter()
2013

2114
rfc3339DateTimeFormatter.dateFormat = "yyyy-MM-dd"
2215
if rfc3339DateTimeFormatter.date(from: value) != nil {
16+
return true
17+
}
18+
19+
return false
20+
}
21+
22+
func validateDate(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
23+
24+
if isValidDate(value) {
2325
return AnySequence(EmptyCollection())
2426
}
2527

Tests/JSONSchemaTests/JSONSchemaCases.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public let testSchemaCases: ((ContextType) -> Void) = {
1818
"infinite-loop-detection.json",
1919

2020
// optional formats
21-
"date-time.json",
2221
"email.json",
2322
"hostname.json",
2423
] + additionalExclusions, in: context, with: draft4Validator)
@@ -125,7 +124,6 @@ public let testSchemaCases: ((ContextType) -> Void) = {
125124

126125
// optional, format
127126
"format.json",
128-
"date-time.json",
129127
"email.json",
130128
"hostname.json",
131129
"idn-email.json",

0 commit comments

Comments
 (0)