Skip to content

Commit

Permalink
chore: add reusable validation methods (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua authored Nov 16, 2023
1 parent ef0558e commit 3d67278
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
51 changes: 27 additions & 24 deletions Sources/momento/TopicClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ public class TopicClient: TopicClientProtocol {
topicName: String,
value: String
) async -> TopicPublishResponse {
if cacheName.count < 1 {
return TopicPublishError(
error: InvalidArgumentError(message: "Must provide a cache name")
)
}
if topicName.count < 1 {
return TopicPublishError(
error: InvalidArgumentError(message: "Must provide a topic name")
do {
try validateCacheName(cacheName: cacheName)
try validateTopicName(topicName: topicName)
} catch let err as SdkError {
return TopicPublishError(error: err)
} catch {
return TopicPublishError(error: UnknownError(
message: "unexpected error: \(error)")
)
}

do {
let result = try await self.pubsubClient.publish(
cacheName: cacheName,
Expand All @@ -75,16 +76,17 @@ public class TopicClient: TopicClientProtocol {
topicName: String,
value: Data
) async -> TopicPublishResponse {
if cacheName.count < 1 {
return TopicPublishError(
error: InvalidArgumentError(message: "Must provide a cache name")
)
}
if topicName.count < 1 {
return TopicPublishError(
error: InvalidArgumentError(message: "Must provide a topic name")
do {
try validateCacheName(cacheName: cacheName)
try validateTopicName(topicName: topicName)
} catch let err as SdkError {
return TopicPublishError(error: err)
} catch {
return TopicPublishError(error: UnknownError(
message: "unexpected error: \(error)")
)
}

do {
let result = try await self.pubsubClient.publish(
cacheName: cacheName,
Expand All @@ -103,16 +105,17 @@ public class TopicClient: TopicClientProtocol {
}

public func subscribe(cacheName: String, topicName: String) async -> TopicSubscribeResponse {
if cacheName.count < 1 {
return TopicSubscribeError(
error: InvalidArgumentError(message: "Must provide a cache name")
)
}
if topicName.count < 1 {
return TopicSubscribeError(
error: InvalidArgumentError(message: "Must provide a topic name")
do {
try validateCacheName(cacheName: cacheName)
try validateTopicName(topicName: topicName)
} catch let err as SdkError {
return TopicSubscribeError(error: err)
} catch {
return TopicSubscribeError(error: UnknownError(
message: "unexpected error: \(error)")
)
}

do {
let result = try await self.pubsubClient.subscribe(
cacheName: cacheName,
Expand Down
19 changes: 19 additions & 0 deletions Sources/momento/internal/utils/validators.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extension String {
var isBlank: Bool {
return allSatisfy({ $0.isWhitespace })
}
}

private func validateString(str: String, errorMessage: String) throws {
if str.isBlank || str.isEmpty {
throw InvalidArgumentError(message: errorMessage)
}
}

internal func validateCacheName(cacheName: String) throws {
try validateString(str: cacheName, errorMessage: "Invalid cache name")
}

internal func validateTopicName(topicName: String) throws {
try validateString(str: topicName, errorMessage: "Invalid topic name")
}

0 comments on commit 3d67278

Please sign in to comment.