Skip to content

Commit 17f3870

Browse files
committed
refactor(format): remove unneeded type checks
1 parent e74dc4a commit 17f3870

File tree

1 file changed

+78
-93
lines changed

1 file changed

+78
-93
lines changed

Sources/format.swift

+78-93
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,69 @@ func format(context: Context, format: Any, instance: Any, schema: [String: Any])
2424
}
2525

2626

27-
func validateIPv4(_ context: Context, _ value: Any) -> AnySequence<ValidationError> {
28-
if let ipv4 = value as? String {
29-
if let expression = try? NSRegularExpression(pattern: "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", options: NSRegularExpression.Options(rawValue: 0)) {
30-
if expression.matches(in: ipv4, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, ipv4.count)).count == 1 {
31-
return AnySequence(EmptyCollection())
32-
}
27+
func validateIPv4(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
28+
if let expression = try? NSRegularExpression(pattern: "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", options: NSRegularExpression.Options(rawValue: 0)) {
29+
if expression.matches(in: value, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, value.utf16.count)).count == 1 {
30+
return AnySequence(EmptyCollection())
3331
}
32+
}
3433

35-
return AnySequence([
36-
ValidationError(
37-
"'\(ipv4)' is not a IPv4 address.",
38-
instanceLocation: context.instanceLocation,
39-
keywordLocation: context.keywordLocation
40-
)
41-
])
34+
return AnySequence([
35+
ValidationError(
36+
"'\(value)' is not a IPv4 address.",
37+
instanceLocation: context.instanceLocation,
38+
keywordLocation: context.keywordLocation
39+
)
40+
])
41+
}
42+
43+
44+
func validateIPv6(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
45+
if !value.contains("%") {
46+
var buf = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET6_ADDRSTRLEN))
47+
if inet_pton(AF_INET6, value, &buf) == 1 {
48+
return AnySequence(EmptyCollection())
49+
}
4250
}
4351

44-
return AnySequence(EmptyCollection())
52+
return AnySequence([
53+
ValidationError(
54+
"'\(value)' is not a value address.",
55+
instanceLocation: context.instanceLocation,
56+
keywordLocation: context.keywordLocation
57+
)
58+
])
4559
}
4660

4761

62+
func validateURI(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
63+
// Using the regex from http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris
4864

49-
func validateIPv6(_ context: Context, _ value: Any) -> AnySequence<ValidationError> {
50-
if let ipv6 = value as? String {
51-
if !ipv6.contains("%") {
52-
var buf = UnsafeMutablePointer<Int8>.allocate(capacity: Int(INET6_ADDRSTRLEN))
53-
if inet_pton(AF_INET6, ipv6, &buf) == 1 {
65+
if let expression = try? NSRegularExpression(pattern: "((?<=\\()[A-Za-z][A-Za-z0-9\\+\\.\\-]*:([A-Za-z0-9\\.\\-_~:/\\?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]|%[A-Fa-f0-9]{2})+(?=\\)))|([A-Za-z][A-Za-z0-9\\+\\.\\-]*:([A-Za-z0-9\\.\\-_~:/\\?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]|%[A-Fa-f0-9]{2})+)", options: NSRegularExpression.Options(rawValue: 0)) {
66+
let result = expression.matches(in: value, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, value.utf16.count))
67+
if result.count == 1 {
68+
let foundRange = result[0].range
69+
if foundRange.location == 0 && foundRange.length == value.utf16.count {
5470
return AnySequence(EmptyCollection())
5571
}
5672
}
73+
}
74+
75+
return AnySequence([
76+
ValidationError(
77+
"'\(value)' is not a valid uri.",
78+
instanceLocation: context.instanceLocation,
79+
keywordLocation: context.keywordLocation
80+
)
81+
])
82+
}
83+
5784

85+
func validateUUID(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
86+
if UUID(uuidString: value) == nil {
5887
return AnySequence([
5988
ValidationError(
60-
"'\(ipv6)' is not a IPv6 address.",
89+
"'\(value)' is not a valid uuid.",
6190
instanceLocation: context.instanceLocation,
6291
keywordLocation: context.keywordLocation
6392
)
@@ -68,23 +97,13 @@ func validateIPv6(_ context: Context, _ value: Any) -> AnySequence<ValidationErr
6897
}
6998

7099

71-
func validateURI(_ context: Context, _ value: Any) -> AnySequence<ValidationError> {
72-
if let uri = value as? String {
73-
// Using the regex from http://blog.dieweltistgarnichtso.net/constructing-a-regular-expression-that-matches-uris
74-
75-
if let expression = try? NSRegularExpression(pattern: "((?<=\\()[A-Za-z][A-Za-z0-9\\+\\.\\-]*:([A-Za-z0-9\\.\\-_~:/\\?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]|%[A-Fa-f0-9]{2})+(?=\\)))|([A-Za-z][A-Za-z0-9\\+\\.\\-]*:([A-Za-z0-9\\.\\-_~:/\\?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=]|%[A-Fa-f0-9]{2})+)", options: NSRegularExpression.Options(rawValue: 0)) {
76-
let result = expression.matches(in: uri, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, uri.count))
77-
if result.count == 1 {
78-
let foundRange = result[0].range
79-
if foundRange.location == 0 && foundRange.length == uri.count {
80-
return AnySequence(EmptyCollection())
81-
}
82-
}
83-
}
84-
100+
func validateRegex(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
101+
do {
102+
_ = try NSRegularExpression(pattern: value)
103+
} catch {
85104
return AnySequence([
86105
ValidationError(
87-
"'\(uri)' is not a valid uri.",
106+
"'\(value)' is not a valid regex.",
88107
instanceLocation: context.instanceLocation,
89108
keywordLocation: context.keywordLocation
90109
)
@@ -95,68 +114,34 @@ func validateURI(_ context: Context, _ value: Any) -> AnySequence<ValidationErro
95114
}
96115

97116

98-
func validateUUID(_ context: Context, _ value: Any) -> AnySequence<ValidationError> {
99-
if let value = value as? String {
100-
if UUID(uuidString: value) == nil {
101-
return AnySequence([
102-
ValidationError(
103-
"'\(value)' is not a valid uuid.",
104-
instanceLocation: context.instanceLocation,
105-
keywordLocation: context.keywordLocation
106-
)
107-
])
108-
}
117+
func validateJSONPointer(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
118+
guard !value.isEmpty else {
119+
return AnySequence(EmptyCollection())
109120
}
110121

111-
return AnySequence(EmptyCollection())
112-
}
113-
114-
115-
func validateRegex(_ context: Context, _ value: Any) -> AnySequence<ValidationError> {
116-
if let value = value as? String {
117-
do {
118-
_ = try NSRegularExpression(pattern: value)
119-
} catch {
120-
return AnySequence([
121-
ValidationError(
122-
"'\(value)' is not a valid regex.",
123-
instanceLocation: context.instanceLocation,
124-
keywordLocation: context.keywordLocation
125-
)
126-
])
127-
}
122+
if !value.hasPrefix("/") {
123+
return AnySequence([
124+
ValidationError(
125+
"'\(value)' is not a valid json-pointer.",
126+
instanceLocation: context.instanceLocation,
127+
keywordLocation: context.keywordLocation
128+
)
129+
])
128130
}
129131

130-
return AnySequence(EmptyCollection())
131-
}
132-
133-
134-
func validateJSONPointer(_ context: Context, _ value: Any) -> AnySequence<ValidationError> {
135-
if let value = value as? String, !value.isEmpty {
136-
if !value.hasPrefix("/") {
137-
return AnySequence([
138-
ValidationError(
139-
"'\(value)' is not a valid json-pointer.",
140-
instanceLocation: context.instanceLocation,
141-
keywordLocation: context.keywordLocation
142-
)
143-
])
144-
}
145-
146-
if value
147-
.replacingOccurrences(of: "~0", with: "")
148-
.replacingOccurrences(of: "~1", with: "")
149-
.contains("~")
150-
{
151-
// unescaped ~
152-
return AnySequence([
153-
ValidationError(
154-
"'\(value)' is not a valid json-pointer.",
155-
instanceLocation: context.instanceLocation,
156-
keywordLocation: context.keywordLocation
157-
)
158-
])
159-
}
132+
if value
133+
.replacingOccurrences(of: "~0", with: "")
134+
.replacingOccurrences(of: "~1", with: "")
135+
.contains("~")
136+
{
137+
// unescaped ~
138+
return AnySequence([
139+
ValidationError(
140+
"'\(value)' is not a valid json-pointer.",
141+
instanceLocation: context.instanceLocation,
142+
keywordLocation: context.keywordLocation
143+
)
144+
])
160145
}
161146

162147
return AnySequence(EmptyCollection())

0 commit comments

Comments
 (0)