Skip to content

Commit 9b9dc51

Browse files
Jack Newcombekylef
authored andcommitted
feat: add support for date format
1 parent bd34ab5 commit 9b9dc51

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

Sources/Draft201909Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class Draft201909Validator: Validator {
6363
"json-pointer": validateJSONPointer,
6464
"duration": validateDuration,
6565
"time": validateTime,
66+
"date": validateDate,
6667
]
6768

6869
public required init(schema: Bool) {

Sources/Draft202012Validator.swift

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

6970
public required init(schema: Bool) {

Sources/Draft7Validator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class Draft7Validator: Validator {
5151
"json-pointer": validateJSONPointer,
5252
"regex": validateRegex,
5353
"time": validateTime,
54+
"date": validateDate,
5455
]
5556

5657
public required init(schema: Bool) {

Sources/Format/date.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Foundation
2+
3+
4+
func validateDate(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
5+
if let regularExpression = try? NSRegularExpression(pattern: "^\\d{4}-\\d{2}-\\d{2}$", options: []) {
6+
let range = NSRange(location: 0, length: value.utf16.count)
7+
let result = regularExpression.matches(in: value, options: [], range: range)
8+
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+
])
16+
}
17+
}
18+
19+
let rfc3339DateTimeFormatter = DateFormatter()
20+
21+
rfc3339DateTimeFormatter.dateFormat = "yyyy-MM-dd"
22+
if rfc3339DateTimeFormatter.date(from: value) != nil {
23+
return AnySequence(EmptyCollection())
24+
}
25+
26+
return AnySequence([
27+
ValidationError(
28+
"'\(value)' is not a valid RFC 3339 formatted date.",
29+
instanceLocation: context.instanceLocation,
30+
keywordLocation: context.keywordLocation
31+
)
32+
])
33+
}

Tests/JSONSchemaTests/JSONSchemaCases.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ class JSONSchemaCases: XCTestCase {
138138

139139
// optional, format
140140
"date-time.json",
141-
"date.json",
142141
"email.json",
143142
"hostname.json",
144143
"idn-email.json",
@@ -173,7 +172,6 @@ class JSONSchemaCases: XCTestCase {
173172
// optional, format
174173
"format.json",
175174
"date-time.json",
176-
"date.json",
177175
"email.json",
178176
"hostname.json",
179177
"idn-email.json",
@@ -207,7 +205,6 @@ class JSONSchemaCases: XCTestCase {
207205
// optional, format
208206
"format.json",
209207
"date-time.json",
210-
"date.json",
211208
"email.json",
212209
"hostname.json",
213210
"idn-email.json",

0 commit comments

Comments
 (0)