[PM-26060] Consolidate InputValidator to BitwardenKit (#2068)

This commit is contained in:
Katherine Bertelsen 2025-10-27 08:56:04 -05:00 committed by GitHub
parent bc7817b674
commit b51ba8de14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 60 additions and 103 deletions

View File

@ -1,3 +1,4 @@
import BitwardenKit
import BitwardenResources
import Foundation

View File

@ -1,3 +1,4 @@
import BitwardenKit
import BitwardenResources
import XCTest

View File

@ -1,8 +0,0 @@
/// An error thrown by an `InputValidator` for invalid input in a field.
///
struct InputValidationError: Error, Equatable {
// MARK: Properties
/// A localized error message describing the validation error.
let message: String
}

View File

@ -1,18 +0,0 @@
import BitwardenResources
/// Validates that the input for a field is not empty.
///
struct EmptyInputValidator: InputValidator {
// MARK: Properties
/// The name of the field that is being validated.
let fieldName: String
// MARK: InputValidator
func validate(input: String?) throws {
guard input?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == false else {
throw InputValidationError(message: Localizations.validationFieldRequired(fieldName))
}
}
}

View File

@ -1,39 +0,0 @@
import XCTest
@testable import AuthenticatorShared
class EmptyInputValidatorTests: BitwardenTestCase {
// MARK: Tests
/// `validate(input:)` doesn't throw an error if the input is valid.
func test_validate_success() {
let subject = EmptyInputValidator(fieldName: "Email")
XCTAssertNoThrow(try subject.validate(input: "a"))
XCTAssertNoThrow(try subject.validate(input: "user@bitwarden.com"))
}
/// `validate(input:)` throw an `InputValidationError` if the input is invalid.
func test_validate_error() {
let subject = EmptyInputValidator(fieldName: "Email")
func assertThrowsInputValidationError(
input: String?,
file: StaticString = #filePath,
line: UInt = #line,
) {
XCTAssertThrowsError(try subject.validate(input: input)) { error in
XCTAssertTrue(error is InputValidationError)
XCTAssertEqual(
error as? InputValidationError,
InputValidationError(message: "The Email field is required."),
)
}
}
assertThrowsInputValidationError(input: nil)
assertThrowsInputValidationError(input: " ")
assertThrowsInputValidationError(input: " ")
assertThrowsInputValidationError(input: "\n")
}
}

View File

@ -0,0 +1,20 @@
// MARK: - InputValidationError
/// An error thrown by an `InputValidator` for invalid input in a field.
///
public struct InputValidationError: Error, Equatable {
// MARK: Properties
/// A localized error message describing the validation error.
public let message: String
// MARK: Initialization
/// Initialize an input validation error.
///
/// - Parameters:
/// - message: A localized error message describing the validation error.
public init(message: String) {
self.message = message
}
}

View File

@ -1,6 +1,8 @@
// - MARK: Input Validator
/// A protocol for an object that handles validating input for a field.
///
protocol InputValidator {
public protocol InputValidator {
/// Validates that the specified input matches the requirements for the field.
///
/// - Parameter input: The input to validate.

View File

@ -0,0 +1,30 @@
import BitwardenResources
// MARK: - EmptyInputValidator
/// Validates that the input for a field is not empty.
///
public struct EmptyInputValidator: InputValidator {
// MARK: Properties
/// The name of the field that is being validated.
public let fieldName: String
// MARK: Initializer
/// Initializes an `EmptyInputValidator`.
///
/// - Parameters:
/// - fieldName: The name of the field that is being validated.
public init(fieldName: String) {
self.fieldName = fieldName
}
// MARK: InputValidator
public func validate(input: String?) throws {
guard input?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == false else {
throw InputValidationError(message: Localizations.validationFieldRequired(fieldName))
}
}
}

View File

@ -1,7 +1,6 @@
import BitwardenKit
import XCTest
@testable import BitwardenShared
class EmptyInputValidatorTests: BitwardenTestCase {
// MARK: Tests

View File

@ -1,3 +1,4 @@
import BitwardenKit
import BitwardenKitMocks
import BitwardenResources
import BitwardenSdk

View File

@ -1,3 +1,4 @@
import BitwardenKit
import BitwardenKitMocks
import BitwardenResources
import BitwardenSdk

View File

@ -1,3 +1,4 @@
import BitwardenKit
import BitwardenResources
import Foundation

View File

@ -1,3 +1,4 @@
import BitwardenKit
import BitwardenResources
import XCTest

View File

@ -1,8 +0,0 @@
/// An error thrown by an `InputValidator` for invalid input in a field.
///
struct InputValidationError: Error, Equatable {
// MARK: Properties
/// A localized error message describing the validation error.
let message: String
}

View File

@ -1,9 +0,0 @@
/// A protocol for an object that handles validating input for a field.
///
protocol InputValidator {
/// Validates that the specified input matches the requirements for the field.
///
/// - Parameter input: The input to validate.
///
func validate(input: String?) throws
}

View File

@ -1,18 +0,0 @@
import BitwardenResources
/// Validates that the input for a field is not empty.
///
struct EmptyInputValidator: InputValidator {
// MARK: Properties
/// The name of the field that is being validated.
let fieldName: String
// MARK: InputValidator
func validate(input: String?) throws {
guard input?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty == false else {
throw InputValidationError(message: Localizations.validationFieldRequired(fieldName))
}
}
}