Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Examples/Core/Content.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import ValidatorCore

// 1. LengthValidationRule - validates string length
/// 1. LengthValidationRule - validates string length
let lengthRule = LengthValidationRule(min: 3, max: 20, error: "Length must be 3-20 characters")

let shortString = "ab"
Expand All @@ -18,7 +18,7 @@ print("'\(validString)' is valid: \(lengthRule.validate(input: validString))") /
print("'\(longString)' is valid: \(lengthRule.validate(input: longString))") // false
print()

// 2. NonEmptyValidationRule - checks if string is not empty
/// 2. NonEmptyValidationRule - checks if string is not empty
let nonEmptyRule = NonEmptyValidationRule(error: "Field is required")

let emptyString = ""
Expand All @@ -31,7 +31,7 @@ print("'\(whitespaceString)' is valid: \(nonEmptyRule.validate(input: whitespace
print("'\(filledString)' is valid: \(nonEmptyRule.validate(input: filledString))") // true
print()

// 3. PrefixValidationRule - validates string prefix
/// 3. PrefixValidationRule - validates string prefix
let prefixRule = PrefixValidationRule(prefix: "https://", error: "URL must start with https://")

let httpURL = "http://example.com"
Expand All @@ -44,7 +44,7 @@ print("'\(httpsURL)' is valid: \(prefixRule.validate(input: httpsURL))") // true
print("'\(noProtocol)' is valid: \(prefixRule.validate(input: noProtocol))") // false
print()

// 4. SuffixValidationRule - validates string suffix
/// 4. SuffixValidationRule - validates string suffix
let suffixRule = SuffixValidationRule(suffix: ".com", error: "Domain must end with .com")

let comDomain = "example.com"
Expand All @@ -57,7 +57,7 @@ print("'\(orgDomain)' is valid: \(suffixRule.validate(input: orgDomain))") // fa
print("'\(noDomain)' is valid: \(suffixRule.validate(input: noDomain))") // false
print()

// 5. RegexValidationRule - validates using regular expression
/// 5. RegexValidationRule - validates using regular expression
let phoneRule = RegexValidationRule(pattern: "^\\d{3}-\\d{4}$", error: "Invalid phone format")

let validPhone = "123-4567"
Expand All @@ -70,7 +70,7 @@ print("'\(invalidPhone1)' is valid: \(phoneRule.validate(input: invalidPhone1))"
print("'\(invalidPhone2)' is valid: \(phoneRule.validate(input: invalidPhone2))") // false
print()

// 6. URLValidationRule - validates URL format
/// 6. URLValidationRule - validates URL format
let urlRule = URLValidationRule(error: "Please enter a valid URL")

let validURL = "https://www.apple.com"
Expand All @@ -83,7 +83,7 @@ print("'\(invalidURL)' is valid: \(urlRule.validate(input: invalidURL))") // fal
print("'\(localURL)' is valid: \(urlRule.validate(input: localURL))") // true
print()

// 7. CreditCardValidationRule - validates credit card number (Luhn algorithm)
/// 7. CreditCardValidationRule - validates credit card number (Luhn algorithm)
let cardRule = CreditCardValidationRule(error: "Invalid card number")

let validCard = "4532015112830366" // Valid Visa test number
Expand All @@ -96,7 +96,7 @@ print("'\(invalidCard)' is valid: \(cardRule.validate(input: invalidCard))") //
print("'\(shortCard)' is valid: \(cardRule.validate(input: shortCard))") // false
print()

// 8. EmailValidationRule - validates email format
/// 8. EmailValidationRule - validates email format
let emailRule = EmailValidationRule(error: "Please enter a valid email")

let validEmail = "user@example.com"
Expand All @@ -109,7 +109,7 @@ print("'\(invalidEmail1)' is valid: \(emailRule.validate(input: invalidEmail1))"
print("'\(invalidEmail2)' is valid: \(emailRule.validate(input: invalidEmail2))") // false
print()

// 9. CharactersValidationRule - validates allowed characters
/// 9. CharactersValidationRule - validates allowed characters
let lettersRule = CharactersValidationRule(characterSet: .letters, error: "Invalid characters")

let onlyLetters = "HelloWorld"
Expand All @@ -122,7 +122,7 @@ print("'\(withNumbers)' is valid: \(lettersRule.validate(input: withNumbers))")
print("'\(withSpaces)' is valid: \(lettersRule.validate(input: withSpaces))") // false
print()

// 10. NilValidationRule - validates that value is nil
/// 10. NilValidationRule - validates that value is nil
let nilRule = NilValidationRule<String>(error: "Value must be nil")

let nilValue: String? = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@testable import PlaygroundDependencies
import Testing

@Test func example() async throws {
@Test func example() {
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ValidatorUI
final class LoginTextFieldExampleViewController: UIViewController {
// MARK: - Properties

// UI
/// UI
private let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -67,7 +67,7 @@ final class LoginTextFieldExampleViewController: UIViewController {
return stackView
}()

// Private properties
/// Private properties
private var isValid: Bool {
[firstNameTextField, lastNameTextField, emailTextField]
.allSatisfy { $0.validationResult == .valid }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import Foundation

// MARK: - IValidationError

/// `IValidationError` is the protocol representing a validation error in `ValidatorCore`.
/// Any type conforming to `IValidationError` can be returned when validation fails.
///
Expand All @@ -14,7 +16,13 @@ import Foundation
/// var message: String { "Invalid input" }
/// }
/// ```
public protocol IValidationError: Error {
public protocol IValidationError: LocalizedError {
/// A human-readable error message describing why validation failed.
var message: String { get }
}

public extension IValidationError {
var errorDescription: String? {
message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ import Foundation
/// ```
extension String: IValidationError {
/// Returns the string itself as the error message.
public var message: String { self }
public var message: String {
self
}
}
16 changes: 8 additions & 8 deletions Sources/ValidatorCore/Classes/Rules/URLValidationRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import Foundation

/// Validates that a string represents a valid URL.
///
/// # Example:
/// ```swift
/// let rule = URLValidationRule(error: "Invalid URL")
/// rule.validate(input: "https://example.com") // true
/// rule.validate(input: "not_a_url") // false
/// ```
// Validates that a string represents a valid URL.
//
// # Example:
// ```swift
// let rule = URLValidationRule(error: "Invalid URL")
// rule.validate(input: "https://example.com") // true
// rule.validate(input: "not_a_url") // false
// ```

public struct URLValidationRule: IValidationRule {
// MARK: Types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
extension NSTextField: IUIValidatable {
/// The value of the text field to validate.
/// Returns an empty string if `text` is nil.
public var inputValue: String { stringValue }
public var inputValue: String {
stringValue
}

/// The type of input for validation.
public typealias Input = String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
extension UITextField: IUIValidatable {
/// The value of the text field to validate.
/// Returns an empty string if `text` is nil.
public var inputValue: String { text ?? "" }
public var inputValue: String {
text ?? ""
}

/// The type of input for validation.
public typealias Input = String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
extension UITextView: IUIValidatable {
/// The value of the text view to validate.
/// Returns an empty string if `text` is nil.
public var inputValue: String { text ?? "" }
public var inputValue: String {
text ?? ""
}

/// The type of input for validation.
public typealias Input = String
Expand Down
Loading