mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-11 13:54:06 -06:00
[PM-26060] Consolidate InputValidator to BitwardenKit (#2068)
This commit is contained in:
parent
bc7817b674
commit
b51ba8de14
@ -1,3 +1,4 @@
|
||||
import BitwardenKit
|
||||
import BitwardenResources
|
||||
import Foundation
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import BitwardenKit
|
||||
import BitwardenResources
|
||||
import XCTest
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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")
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
@ -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.
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
import BitwardenKit
|
||||
import XCTest
|
||||
|
||||
@testable import BitwardenShared
|
||||
|
||||
class EmptyInputValidatorTests: BitwardenTestCase {
|
||||
// MARK: Tests
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import BitwardenKit
|
||||
import BitwardenKitMocks
|
||||
import BitwardenResources
|
||||
import BitwardenSdk
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import BitwardenKit
|
||||
import BitwardenKitMocks
|
||||
import BitwardenResources
|
||||
import BitwardenSdk
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import BitwardenKit
|
||||
import BitwardenResources
|
||||
import Foundation
|
||||
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import BitwardenKit
|
||||
import BitwardenResources
|
||||
import XCTest
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user