Implemented the User Agent middleware (#6)

This PR contains the work done to implement the `UserAgentMiddleware` middleware that includes user agent information into a header of the requests sent by the `Client` type, as defined in the [Discogs documentation](https://www.discogs.com/developers/#page:home,header:home-general-information). For this purpose, the `CamelCaseValidationRule`, `SemanticVersionValidationRule` and `URLValidationRule` types were implemented and integrated into the existing `ValidateInputUseCase` type.

Reviewed-on: #6
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #6.
This commit is contained in:
2025-10-13 00:54:17 +00:00
committed by Javier Cicchelli
parent 24d703b967
commit 791ebf4f78
1988 changed files with 2882 additions and 2010 deletions
@@ -77,32 +77,6 @@ private extension SecureValidationRule {
// MARK: Functions
/// Checks if a given input is valid,
/// - Parameter input: An input to validate.
/// - Returns: A flag that indicates whether a given input is valid or not.
func isValid(_ input: String) -> Bool {
let regexPattern = String(format: .Pattern.securityInput, inputType.rawValue)
do {
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 6.0, *) {
let securityInput = try Regex(regexPattern)
let matches = input.matches(of: securityInput)
return !matches.isEmpty
} else {
let securityInput = try NSRegularExpression(pattern: regexPattern)
let matches = securityInput.matches(
in: input,
range: .init(location: 0, length: input.count)
)
return !matches.isEmpty
}
} catch {
return false
}
}
/// Validates a given input.
///
/// > note: This helper function would not be necessary when support for *Swift 5.10* is discontinued.
@@ -114,7 +88,10 @@ private extension SecureValidationRule {
guard let input else {
return false
}
guard isValid(input) else {
guard input.fullyMatch(
pattern: .init(format: .Pattern.securityInput, inputType.rawValue)
) else {
switch inputType {
case .consumerKey: throw InputValidationError.inputNotConsumerKey
case .consumerSecret: throw InputValidationError.inputNotConsumerSecret
@@ -130,6 +107,6 @@ private extension SecureValidationRule {
// MARK: - Constants
private extension String.Pattern {
/// A regular expression pattern to match the security inputs against.
/// A regular expression pattern that represents security inputs.
static let securityInput = "^([a-z]|[A-Z]){%d}$"
}