From 4a10ce9d52261800a0d14734bab7c5a0481a0897 Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Thu, 27 Nov 2025 15:35:22 +0400 Subject: [PATCH 1/2] chore: add usage examples to the examples directory --- .../Core/Content.playground/Contents.swift | 152 +++++++ .../Content.playground/contents.xcplayground | 4 + .../Core/PlaygroundDependencies/.gitignore | 8 + .../Core/PlaygroundDependencies/Package.swift | 33 ++ .../PlaygroundDependencies.swift | 7 + .../PlaygroundDependenciesTests.swift | 11 + .../SwiftUIExample.xcodeproj/project.pbxproj | 377 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../ApplicationRoot/SwiftUIExampleApp.swift | 15 + .../Registration/RegistrationForm.swift | 43 ++ .../Registration/RegistrationView.swift | 63 +++ .../Registration/Subviews/ErrorView.swift | 23 ++ .../AccentColor.colorset/Contents.json | 11 + .../AppIcon.appiconset/Contents.json | 35 ++ .../Resources/Assets.xcassets/Contents.json | 6 + .../UIKitExample.xcodeproj/project.pbxproj | 376 +++++++++++++++++ .../contents.xcworkspacedata | 7 + Examples/UIKit/UIKitExample/AppDelegate.swift | 33 ++ .../AccentColor.colorset/Contents.json | 11 + .../AppIcon.appiconset/Contents.json | 35 ++ .../Assets.xcassets/Contents.json | 6 + .../Base.lproj/LaunchScreen.storyboard | 25 ++ .../UIKitExample/Base.lproj/Main.storyboard | 24 ++ Examples/UIKit/UIKitExample/Info.plist | 25 ++ .../UIKit/UIKitExample/SceneDelegate.swift | 46 +++ .../UIKit/UIKitExample/ViewController.swift | 244 ++++++++++++ 26 files changed, 1627 insertions(+) create mode 100644 Examples/Core/Content.playground/Contents.swift create mode 100644 Examples/Core/Content.playground/contents.xcplayground create mode 100644 Examples/Core/PlaygroundDependencies/.gitignore create mode 100644 Examples/Core/PlaygroundDependencies/Package.swift create mode 100644 Examples/Core/PlaygroundDependencies/Sources/PlaygroundDependencies/PlaygroundDependencies.swift create mode 100644 Examples/Core/PlaygroundDependencies/Tests/PlaygroundDependenciesTests/PlaygroundDependenciesTests.swift create mode 100644 Examples/SwiftUI/SwiftUIExample.xcodeproj/project.pbxproj create mode 100644 Examples/SwiftUI/SwiftUIExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Examples/SwiftUI/SwiftUIExample/Classes/ApplicationRoot/SwiftUIExampleApp.swift create mode 100644 Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationForm.swift create mode 100644 Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationView.swift create mode 100644 Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/Subviews/ErrorView.swift create mode 100644 Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AccentColor.colorset/Contents.json create mode 100644 Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/Contents.json create mode 100644 Examples/UIKit/UIKitExample.xcodeproj/project.pbxproj create mode 100644 Examples/UIKit/UIKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Examples/UIKit/UIKitExample/AppDelegate.swift create mode 100644 Examples/UIKit/UIKitExample/Assets.xcassets/AccentColor.colorset/Contents.json create mode 100644 Examples/UIKit/UIKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json create mode 100644 Examples/UIKit/UIKitExample/Assets.xcassets/Contents.json create mode 100644 Examples/UIKit/UIKitExample/Base.lproj/LaunchScreen.storyboard create mode 100644 Examples/UIKit/UIKitExample/Base.lproj/Main.storyboard create mode 100644 Examples/UIKit/UIKitExample/Info.plist create mode 100644 Examples/UIKit/UIKitExample/SceneDelegate.swift create mode 100644 Examples/UIKit/UIKitExample/ViewController.swift diff --git a/Examples/Core/Content.playground/Contents.swift b/Examples/Core/Content.playground/Contents.swift new file mode 100644 index 0000000..ccbd049 --- /dev/null +++ b/Examples/Core/Content.playground/Contents.swift @@ -0,0 +1,152 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import ValidatorCore + +// 1. LengthValidationRule - validates string length +let lengthRule = LengthValidationRule(min: 3, max: 20, error: "Length must be 3-20 characters") + +let shortString = "ab" +let validString = "Hello" +let longString = "This is a very long string that exceeds the limit" + +print("LengthValidationRule:") +print("'\(shortString)' is valid: \(lengthRule.validate(input: shortString))") // false +print("'\(validString)' is valid: \(lengthRule.validate(input: validString))") // true +print("'\(longString)' is valid: \(lengthRule.validate(input: longString))") // false +print() + +// 2. NonEmptyValidationRule - checks if string is not empty +let nonEmptyRule = NonEmptyValidationRule(error: "Field is required") + +let emptyString = "" +let whitespaceString = " " +let filledString = "Content" + +print("NonEmptyValidationRule:") +print("'\(emptyString)' is valid: \(nonEmptyRule.validate(input: emptyString))") // false +print("'\(whitespaceString)' is valid: \(nonEmptyRule.validate(input: whitespaceString))") // false +print("'\(filledString)' is valid: \(nonEmptyRule.validate(input: filledString))") // true +print() + +// 3. PrefixValidationRule - validates string prefix +let prefixRule = PrefixValidationRule(prefix: "https://", error: "URL must start with https://") + +let httpURL = "http://example.com" +let httpsURL = "https://example.com" +let noProtocol = "example.com" + +print("PrefixValidationRule:") +print("'\(httpURL)' is valid: \(prefixRule.validate(input: httpURL))") // false +print("'\(httpsURL)' is valid: \(prefixRule.validate(input: httpsURL))") // true +print("'\(noProtocol)' is valid: \(prefixRule.validate(input: noProtocol))") // false +print() + +// 4. SuffixValidationRule - validates string suffix +let suffixRule = SuffixValidationRule(suffix: ".com", error: "Domain must end with .com") + +let comDomain = "example.com" +let orgDomain = "example.org" +let noDomain = "example" + +print("SuffixValidationRule:") +print("'\(comDomain)' is valid: \(suffixRule.validate(input: comDomain))") // true +print("'\(orgDomain)' is valid: \(suffixRule.validate(input: orgDomain))") // false +print("'\(noDomain)' is valid: \(suffixRule.validate(input: noDomain))") // false +print() + +// 5. RegexValidationRule - validates using regular expression +let phoneRule = RegexValidationRule(pattern: "^\\d{3}-\\d{4}$", error: "Invalid phone format") + +let validPhone = "123-4567" +let invalidPhone1 = "1234567" +let invalidPhone2 = "123-456" + +print("RegexValidationRule:") +print("'\(validPhone)' is valid: \(phoneRule.validate(input: validPhone))") // true +print("'\(invalidPhone1)' is valid: \(phoneRule.validate(input: invalidPhone1))") // false +print("'\(invalidPhone2)' is valid: \(phoneRule.validate(input: invalidPhone2))") // false +print() + +// 6. URLValidationRule - validates URL format +let urlRule = URLValidationRule(error: "Please enter a valid URL") + +let validURL = "https://www.apple.com" +let invalidURL = "not a url" +let localURL = "file:///path/to/file" + +print("URLValidationRule:") +print("'\(validURL)' is valid: \(urlRule.validate(input: validURL))") // true +print("'\(invalidURL)' is valid: \(urlRule.validate(input: invalidURL))") // false +print("'\(localURL)' is valid: \(urlRule.validate(input: localURL))") // true +print() + +// 7. CreditCardValidationRule - validates credit card number (Luhn algorithm) +let cardRule = CreditCardValidationRule(error: "Invalid card number") + +let validCard = "4532015112830366" // Valid Visa test number +let invalidCard = "1234567890123456" +let shortCard = "4532" + +print("CreditCardValidationRule:") +print("'\(validCard)' is valid: \(cardRule.validate(input: validCard))") // true +print("'\(invalidCard)' is valid: \(cardRule.validate(input: invalidCard))") // false +print("'\(shortCard)' is valid: \(cardRule.validate(input: shortCard))") // false +print() + +// 8. EmailValidationRule - validates email format +let emailRule = EmailValidationRule(error: "Please enter a valid email") + +let validEmail = "user@example.com" +let invalidEmail1 = "user@" +let invalidEmail2 = "user.example.com" + +print("EmailValidationRule:") +print("'\(validEmail)' is valid: \(emailRule.validate(input: validEmail))") // true +print("'\(invalidEmail1)' is valid: \(emailRule.validate(input: invalidEmail1))") // false +print("'\(invalidEmail2)' is valid: \(emailRule.validate(input: invalidEmail2))") // false +print() + +// 9. CharactersValidationRule - validates allowed characters +let lettersRule = CharactersValidationRule(characterSet: .letters, error: "Invalid characters") + +let onlyLetters = "HelloWorld" +let withNumbers = "Hello123" +let withSpaces = "Hello World" + +print("CharactersValidationRule:") +print("'\(onlyLetters)' is valid: \(lettersRule.validate(input: onlyLetters))") // true +print("'\(withNumbers)' is valid: \(lettersRule.validate(input: withNumbers))") // false +print("'\(withSpaces)' is valid: \(lettersRule.validate(input: withSpaces))") // false +print() + +// 10. NilValidationRule - validates that value is nil +let nilRule = NilValidationRule(error: "Value must be nil") + +let nilValue: String? = nil +let nonNilValue: String? = "Something" + +print("NilValidationRule:") +print("nil value is valid: \(nilRule.validate(input: nilValue))") // true +print("non-nil value is valid: \(nilRule.validate(input: nonNilValue))") // false +print() + +// MARK: - Combining Rules + +print("=== Combining Multiple Rules ===") + +// Example: password validation with multiple rules +let passwordLengthRule = LengthValidationRule(min: 8, max: 32, error: "Password must be 8-32 characters") +let passwordNotEmptyRule = NonEmptyValidationRule(error: "Password is required") + +let passwords = ["", "123", "ValidPass123", "VeryLongPasswordThatExceedsMaximumLength"] + +for password in passwords { + let isLengthValid = passwordLengthRule.validate(input: password) + let isNotEmpty = passwordNotEmptyRule.validate(input: password) + let isValid = isLengthValid && isNotEmpty + + print("Password '\(password)': \(isValid ? "✅" : "❌")") +} diff --git a/Examples/Core/Content.playground/contents.xcplayground b/Examples/Core/Content.playground/contents.xcplayground new file mode 100644 index 0000000..a8211e5 --- /dev/null +++ b/Examples/Core/Content.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Examples/Core/PlaygroundDependencies/.gitignore b/Examples/Core/PlaygroundDependencies/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/Examples/Core/PlaygroundDependencies/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Examples/Core/PlaygroundDependencies/Package.swift b/Examples/Core/PlaygroundDependencies/Package.swift new file mode 100644 index 0000000..bb9a21a --- /dev/null +++ b/Examples/Core/PlaygroundDependencies/Package.swift @@ -0,0 +1,33 @@ +// swift-tools-version: 6.2 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "PlaygroundDependencies", + products: [ + .library(name: "PlaygroundDependencies", targets: ["PlaygroundDependencies"]), + ], + targets: [ + .target(name: "PlaygroundDependencies"), + .testTarget(name: "PlaygroundDependenciesTests", dependencies: ["PlaygroundDependencies"]), + ] +) + +package.dependencies = [ + .package(path: "../../../"), +] +package.targets = [ + .target( + name: "PlaygroundDependencies", + dependencies: [ + .product(name: "ValidatorCore", package: "validator"), + ] + ), +] +package.platforms = [ + .iOS("16.0"), + .macOS("13.0"), + .tvOS("16.0"), + .watchOS("9.0"), +] diff --git a/Examples/Core/PlaygroundDependencies/Sources/PlaygroundDependencies/PlaygroundDependencies.swift b/Examples/Core/PlaygroundDependencies/Sources/PlaygroundDependencies/PlaygroundDependencies.swift new file mode 100644 index 0000000..c0217c0 --- /dev/null +++ b/Examples/Core/PlaygroundDependencies/Sources/PlaygroundDependencies/PlaygroundDependencies.swift @@ -0,0 +1,7 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +// The Swift Programming Language +// https://docs.swift.org/swift-book diff --git a/Examples/Core/PlaygroundDependencies/Tests/PlaygroundDependenciesTests/PlaygroundDependenciesTests.swift b/Examples/Core/PlaygroundDependencies/Tests/PlaygroundDependenciesTests/PlaygroundDependenciesTests.swift new file mode 100644 index 0000000..df2b80e --- /dev/null +++ b/Examples/Core/PlaygroundDependencies/Tests/PlaygroundDependenciesTests/PlaygroundDependenciesTests.swift @@ -0,0 +1,11 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +@testable import PlaygroundDependencies +import Testing + +@Test func example() async throws { + // Write your test here and use APIs like `#expect(...)` to check expected conditions. +} diff --git a/Examples/SwiftUI/SwiftUIExample.xcodeproj/project.pbxproj b/Examples/SwiftUI/SwiftUIExample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..0a4cd39 --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample.xcodeproj/project.pbxproj @@ -0,0 +1,377 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXBuildFile section */ + 5CC205F02ED85F16006BB4AF /* ValidatorCore in Frameworks */ = {isa = PBXBuildFile; productRef = 5CC205EF2ED85F16006BB4AF /* ValidatorCore */; }; + 5CC205F22ED85F16006BB4AF /* ValidatorUI in Frameworks */ = {isa = PBXBuildFile; productRef = 5CC205F12ED85F16006BB4AF /* ValidatorUI */; }; + 5CC206212ED85F9D006BB4AF /* ValidatorCore in Frameworks */ = {isa = PBXBuildFile; productRef = 5CC206202ED85F9D006BB4AF /* ValidatorCore */; }; + 5CC206232ED85F9D006BB4AF /* ValidatorUI in Frameworks */ = {isa = PBXBuildFile; productRef = 5CC206222ED85F9D006BB4AF /* ValidatorUI */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 5CC205E02ED85EEE006BB4AF /* SwiftUIExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUIExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + 5CC205E22ED85EEE006BB4AF /* SwiftUIExample */ = { + isa = PBXFileSystemSynchronizedRootGroup; + path = SwiftUIExample; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5CC205DD2ED85EEE006BB4AF /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 5CC205F02ED85F16006BB4AF /* ValidatorCore in Frameworks */, + 5CC206212ED85F9D006BB4AF /* ValidatorCore in Frameworks */, + 5CC205F22ED85F16006BB4AF /* ValidatorUI in Frameworks */, + 5CC206232ED85F9D006BB4AF /* ValidatorUI in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 5CC205D72ED85EEE006BB4AF = { + isa = PBXGroup; + children = ( + 5CC205E22ED85EEE006BB4AF /* SwiftUIExample */, + 5CC205E12ED85EEE006BB4AF /* Products */, + ); + sourceTree = ""; + }; + 5CC205E12ED85EEE006BB4AF /* Products */ = { + isa = PBXGroup; + children = ( + 5CC205E02ED85EEE006BB4AF /* SwiftUIExample.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 5CC205DF2ED85EEE006BB4AF /* SwiftUIExample */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5CC205EB2ED85EF0006BB4AF /* Build configuration list for PBXNativeTarget "SwiftUIExample" */; + buildPhases = ( + 5CC205DC2ED85EEE006BB4AF /* Sources */, + 5CC205DD2ED85EEE006BB4AF /* Frameworks */, + 5CC205DE2ED85EEE006BB4AF /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + 5CC205E22ED85EEE006BB4AF /* SwiftUIExample */, + ); + name = SwiftUIExample; + packageProductDependencies = ( + 5CC205EF2ED85F16006BB4AF /* ValidatorCore */, + 5CC205F12ED85F16006BB4AF /* ValidatorUI */, + 5CC206202ED85F9D006BB4AF /* ValidatorCore */, + 5CC206222ED85F9D006BB4AF /* ValidatorUI */, + ); + productName = SwiftUIExample; + productReference = 5CC205E02ED85EEE006BB4AF /* SwiftUIExample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 5CC205D82ED85EEE006BB4AF /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 2600; + LastUpgradeCheck = 2600; + TargetAttributes = { + 5CC205DF2ED85EEE006BB4AF = { + CreatedOnToolsVersion = 26.0; + }; + }; + }; + buildConfigurationList = 5CC205DB2ED85EEE006BB4AF /* Build configuration list for PBXProject "SwiftUIExample" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 5CC205D72ED85EEE006BB4AF; + minimizedProjectReferenceProxies = 1; + packageReferences = ( + 5CC2061F2ED85F9D006BB4AF /* XCLocalSwiftPackageReference "../../../validator" */, + ); + preferredProjectObjectVersion = 77; + productRefGroup = 5CC205E12ED85EEE006BB4AF /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 5CC205DF2ED85EEE006BB4AF /* SwiftUIExample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 5CC205DE2ED85EEE006BB4AF /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 5CC205DC2ED85EEE006BB4AF /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 5CC205E92ED85EF0006BB4AF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.0; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 5CC205EA2ED85EF0006BB4AF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.0; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 5CC205EC2ED85EF0006BB4AF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.space-code.validatorui.SwiftUIExample"; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 5CC205ED2ED85EF0006BB4AF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + ENABLE_PREVIEWS = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchScreen_Generation = YES; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.space-code.validatorui.SwiftUIExample"; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 5CC205DB2ED85EEE006BB4AF /* Build configuration list for PBXProject "SwiftUIExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5CC205E92ED85EF0006BB4AF /* Debug */, + 5CC205EA2ED85EF0006BB4AF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 5CC205EB2ED85EF0006BB4AF /* Build configuration list for PBXNativeTarget "SwiftUIExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5CC205EC2ED85EF0006BB4AF /* Debug */, + 5CC205ED2ED85EF0006BB4AF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCLocalSwiftPackageReference section */ + 5CC2061F2ED85F9D006BB4AF /* XCLocalSwiftPackageReference "../../../validator" */ = { + isa = XCLocalSwiftPackageReference; + relativePath = ../../../validator; + }; +/* End XCLocalSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 5CC205EF2ED85F16006BB4AF /* ValidatorCore */ = { + isa = XCSwiftPackageProductDependency; + productName = ValidatorCore; + }; + 5CC205F12ED85F16006BB4AF /* ValidatorUI */ = { + isa = XCSwiftPackageProductDependency; + productName = ValidatorUI; + }; + 5CC206202ED85F9D006BB4AF /* ValidatorCore */ = { + isa = XCSwiftPackageProductDependency; + productName = ValidatorCore; + }; + 5CC206222ED85F9D006BB4AF /* ValidatorUI */ = { + isa = XCSwiftPackageProductDependency; + productName = ValidatorUI; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 5CC205D82ED85EEE006BB4AF /* Project object */; +} diff --git a/Examples/SwiftUI/SwiftUIExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Examples/SwiftUI/SwiftUIExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Examples/SwiftUI/SwiftUIExample/Classes/ApplicationRoot/SwiftUIExampleApp.swift b/Examples/SwiftUI/SwiftUIExample/Classes/ApplicationRoot/SwiftUIExampleApp.swift new file mode 100644 index 0000000..ea0d90b --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Classes/ApplicationRoot/SwiftUIExampleApp.swift @@ -0,0 +1,15 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import SwiftUI + +@main +struct SwiftUIExampleApp: App { + var body: some Scene { + WindowGroup { + RegistrationView() + } + } +} diff --git a/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationForm.swift b/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationForm.swift new file mode 100644 index 0000000..dc13963 --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationForm.swift @@ -0,0 +1,43 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import Combine +import SwiftUI +import ValidatorCore +import ValidatorUI + +final class RegistrationForm: ObservableObject { + // MARK: Properties + + @Published var manager = FormFieldManager() + + @FormField( + rules: [ + LengthValidationRule(min: 2, max: 50, error: "Invalid name length"), + ], + debounce: 0.8 + ) + var firstName = "" + + @FormField( + rules: [ + LengthValidationRule(min: 2, max: 50, error: "Invalid name length"), + ], + debounce: 0.8 + ) + var lastName = "" + + @FormField( + rules: [ + EmailValidationRule(error: "Invalid email"), + ], + debounce: 0.8 + ) + var email = "" + + lazy var firstNameContainer = _firstName.validate(manager: manager) + lazy var lastNameContainer = _lastName.validate(manager: manager) + lazy var emailContainer = _email.validate(manager: manager) +} diff --git a/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationView.swift b/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationView.swift new file mode 100644 index 0000000..e5f8a15 --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/RegistrationView.swift @@ -0,0 +1,63 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import Combine +import SwiftUI +import ValidatorCore +import ValidatorUI + +struct RegistrationView: View { + // MARK: Properties + + @StateObject private var form = RegistrationForm() + @State private var isFormValid = false + + // MARK: View + + var body: some View { + Form { + Section("Personal Information") { + TextField("First Name", text: $form.firstName) + .validate(validationContainer: form.firstNameContainer) { errors in + ErrorView(errors: errors) + } + + TextField("Last Name", text: $form.lastName) + .validate(validationContainer: form.lastNameContainer) { errors in + ErrorView(errors: errors) + } + } + + Section("Contact") { + TextField("Email", text: $form.email) + .validate(validationContainer: form.emailContainer) { errors in + ErrorView(errors: errors) + } + } + + Section { + Button("Submit") { + form.manager.validate() + } + .disabled(!isFormValid) + } + } + .onReceive(form.manager.$isValid) { newValue in + isFormValid = newValue + } + } + + // MARK: Private + + private func submitForm() { + print("✅ Form is valid, submitting...") + } +} + +// MARK: Preview + +#Preview { + RegistrationView() +} diff --git a/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/Subviews/ErrorView.swift b/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/Subviews/ErrorView.swift new file mode 100644 index 0000000..80adf9e --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Classes/Presentation/UserStories/Registration/Subviews/ErrorView.swift @@ -0,0 +1,23 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import SwiftUI +import ValidatorCore + +struct ErrorView: View { + // MARK: Properties + + let errors: [any IValidationError] + + // MARK: View + + var body: some View { + errors.first.map { + Text($0.message) + .font(.caption) + .foregroundStyle(.red) + } + } +} diff --git a/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AccentColor.colorset/Contents.json b/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2305880 --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,35 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "tinted" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/Contents.json b/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Examples/SwiftUI/SwiftUIExample/Resources/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/UIKit/UIKitExample.xcodeproj/project.pbxproj b/Examples/UIKit/UIKitExample.xcodeproj/project.pbxproj new file mode 100644 index 0000000..2252e79 --- /dev/null +++ b/Examples/UIKit/UIKitExample.xcodeproj/project.pbxproj @@ -0,0 +1,376 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXBuildFile section */ + 5CC206162ED85F6F006BB4AF /* ValidatorCore in Frameworks */ = {isa = PBXBuildFile; productRef = 5CC206152ED85F6F006BB4AF /* ValidatorCore */; }; + 5CC206182ED85F6F006BB4AF /* ValidatorUI in Frameworks */ = {isa = PBXBuildFile; productRef = 5CC206172ED85F6F006BB4AF /* ValidatorUI */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 5CC205FC2ED85F5E006BB4AF /* UIKitExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UIKitExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFileSystemSynchronizedBuildFileExceptionSet section */ + 5CC2060E2ED85F63006BB4AF /* Exceptions for "UIKitExample" folder in "UIKitExample" target */ = { + isa = PBXFileSystemSynchronizedBuildFileExceptionSet; + membershipExceptions = ( + Info.plist, + ); + target = 5CC205FB2ED85F5E006BB4AF /* UIKitExample */; + }; +/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */ + +/* Begin PBXFileSystemSynchronizedRootGroup section */ + 5CC205FE2ED85F5E006BB4AF /* UIKitExample */ = { + isa = PBXFileSystemSynchronizedRootGroup; + exceptions = ( + 5CC2060E2ED85F63006BB4AF /* Exceptions for "UIKitExample" folder in "UIKitExample" target */, + ); + path = UIKitExample; + sourceTree = ""; + }; +/* End PBXFileSystemSynchronizedRootGroup section */ + +/* Begin PBXFrameworksBuildPhase section */ + 5CC205F92ED85F5E006BB4AF /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 5CC206162ED85F6F006BB4AF /* ValidatorCore in Frameworks */, + 5CC206182ED85F6F006BB4AF /* ValidatorUI in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 5CC205F32ED85F5E006BB4AF = { + isa = PBXGroup; + children = ( + 5CC205FE2ED85F5E006BB4AF /* UIKitExample */, + 5CC205FD2ED85F5E006BB4AF /* Products */, + ); + sourceTree = ""; + }; + 5CC205FD2ED85F5E006BB4AF /* Products */ = { + isa = PBXGroup; + children = ( + 5CC205FC2ED85F5E006BB4AF /* UIKitExample.app */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 5CC205FB2ED85F5E006BB4AF /* UIKitExample */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5CC2060F2ED85F63006BB4AF /* Build configuration list for PBXNativeTarget "UIKitExample" */; + buildPhases = ( + 5CC205F82ED85F5E006BB4AF /* Sources */, + 5CC205F92ED85F5E006BB4AF /* Frameworks */, + 5CC205FA2ED85F5E006BB4AF /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + fileSystemSynchronizedGroups = ( + 5CC205FE2ED85F5E006BB4AF /* UIKitExample */, + ); + name = UIKitExample; + packageProductDependencies = ( + 5CC206152ED85F6F006BB4AF /* ValidatorCore */, + 5CC206172ED85F6F006BB4AF /* ValidatorUI */, + ); + productName = UIKitExample; + productReference = 5CC205FC2ED85F5E006BB4AF /* UIKitExample.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 5CC205F42ED85F5E006BB4AF /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 2600; + LastUpgradeCheck = 2600; + TargetAttributes = { + 5CC205FB2ED85F5E006BB4AF = { + CreatedOnToolsVersion = 26.0; + }; + }; + }; + buildConfigurationList = 5CC205F72ED85F5E006BB4AF /* Build configuration list for PBXProject "UIKitExample" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 5CC205F32ED85F5E006BB4AF; + minimizedProjectReferenceProxies = 1; + packageReferences = ( + 5CC206142ED85F6F006BB4AF /* XCLocalSwiftPackageReference "../../../validator" */, + ); + preferredProjectObjectVersion = 77; + productRefGroup = 5CC205FD2ED85F5E006BB4AF /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 5CC205FB2ED85F5E006BB4AF /* UIKitExample */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 5CC205FA2ED85F5E006BB4AF /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 5CC205F82ED85F5E006BB4AF /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 5CC206102ED85F63006BB4AF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = UIKitExample/Info.plist; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; + INFOPLIST_KEY_UIMainStoryboardFile = Main; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.space-code.validatorui.UIKitExample"; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 5CC206112ED85F63006BB4AF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = UIKitExample/Info.plist; + INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; + INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; + INFOPLIST_KEY_UIMainStoryboardFile = Main; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + PRODUCT_BUNDLE_IDENTIFIER = "com.space-code.validatorui.UIKitExample"; + PRODUCT_NAME = "$(TARGET_NAME)"; + STRING_CATALOG_GENERATE_SYMBOLS = YES; + SWIFT_APPROACHABLE_CONCURRENCY = YES; + SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; + 5CC206122ED85F63006BB4AF /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.0; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 5CC206132ED85F63006BB4AF /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 26.0; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 5CC205F72ED85F5E006BB4AF /* Build configuration list for PBXProject "UIKitExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5CC206122ED85F63006BB4AF /* Debug */, + 5CC206132ED85F63006BB4AF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 5CC2060F2ED85F63006BB4AF /* Build configuration list for PBXNativeTarget "UIKitExample" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5CC206102ED85F63006BB4AF /* Debug */, + 5CC206112ED85F63006BB4AF /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + +/* Begin XCLocalSwiftPackageReference section */ + 5CC206142ED85F6F006BB4AF /* XCLocalSwiftPackageReference "../../../validator" */ = { + isa = XCLocalSwiftPackageReference; + relativePath = ../../../validator; + }; +/* End XCLocalSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 5CC206152ED85F6F006BB4AF /* ValidatorCore */ = { + isa = XCSwiftPackageProductDependency; + productName = ValidatorCore; + }; + 5CC206172ED85F6F006BB4AF /* ValidatorUI */ = { + isa = XCSwiftPackageProductDependency; + productName = ValidatorUI; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = 5CC205F42ED85F5E006BB4AF /* Project object */; +} diff --git a/Examples/UIKit/UIKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Examples/UIKit/UIKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Examples/UIKit/UIKitExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Examples/UIKit/UIKitExample/AppDelegate.swift b/Examples/UIKit/UIKitExample/AppDelegate.swift new file mode 100644 index 0000000..0b4d57e --- /dev/null +++ b/Examples/UIKit/UIKitExample/AppDelegate.swift @@ -0,0 +1,33 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import UIKit + +@main +class AppDelegate: UIResponder, UIApplicationDelegate { + func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { + // Override point for customization after application launch. + true + } + + // MARK: UISceneSession Lifecycle + + func application( + _: UIApplication, + configurationForConnecting connectingSceneSession: UISceneSession, + options _: UIScene.ConnectionOptions + ) -> UISceneConfiguration { + // Called when a new scene session is being created. + // Use this method to select a configuration to create the new scene with. + UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) + } + + func application(_: UIApplication, didDiscardSceneSessions _: Set) { + // Called when the user discards a scene session. + // If any sessions were discarded while the application was not running, this will be called shortly after + // application:didFinishLaunchingWithOptions. + // Use this method to release any resources that were specific to the discarded scenes, as they will not return. + } +} diff --git a/Examples/UIKit/UIKitExample/Assets.xcassets/AccentColor.colorset/Contents.json b/Examples/UIKit/UIKitExample/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/Examples/UIKit/UIKitExample/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/UIKit/UIKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json b/Examples/UIKit/UIKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2305880 --- /dev/null +++ b/Examples/UIKit/UIKitExample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,35 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "tinted" + } + ], + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/UIKit/UIKitExample/Assets.xcassets/Contents.json b/Examples/UIKit/UIKitExample/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Examples/UIKit/UIKitExample/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Examples/UIKit/UIKitExample/Base.lproj/LaunchScreen.storyboard b/Examples/UIKit/UIKitExample/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..865e932 --- /dev/null +++ b/Examples/UIKit/UIKitExample/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/UIKit/UIKitExample/Base.lproj/Main.storyboard b/Examples/UIKit/UIKitExample/Base.lproj/Main.storyboard new file mode 100644 index 0000000..25a7638 --- /dev/null +++ b/Examples/UIKit/UIKitExample/Base.lproj/Main.storyboard @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/UIKit/UIKitExample/Info.plist b/Examples/UIKit/UIKitExample/Info.plist new file mode 100644 index 0000000..dd3c9af --- /dev/null +++ b/Examples/UIKit/UIKitExample/Info.plist @@ -0,0 +1,25 @@ + + + + + UIApplicationSceneManifest + + UIApplicationSupportsMultipleScenes + + UISceneConfigurations + + UIWindowSceneSessionRoleApplication + + + UISceneConfigurationName + Default Configuration + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).SceneDelegate + UISceneStoryboardFile + Main + + + + + + diff --git a/Examples/UIKit/UIKitExample/SceneDelegate.swift b/Examples/UIKit/UIKitExample/SceneDelegate.swift new file mode 100644 index 0000000..fbbb044 --- /dev/null +++ b/Examples/UIKit/UIKitExample/SceneDelegate.swift @@ -0,0 +1,46 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import UIKit + +class SceneDelegate: UIResponder, UIWindowSceneDelegate { + var window: UIWindow? + + func scene(_ scene: UIScene, willConnectTo _: UISceneSession, options _: UIScene.ConnectionOptions) { + // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. + // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. + // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` + // instead). + guard let _ = (scene as? UIWindowScene) else { return } + } + + func sceneDidDisconnect(_: UIScene) { + // Called as the scene is being released by the system. + // This occurs shortly after the scene enters the background, or when its session is discarded. + // Release any resources associated with this scene that can be re-created the next time the scene connects. + // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). + } + + func sceneDidBecomeActive(_: UIScene) { + // Called when the scene has moved from an inactive state to an active state. + // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. + } + + func sceneWillResignActive(_: UIScene) { + // Called when the scene will move from an active state to an inactive state. + // This may occur due to temporary interruptions (ex. an incoming phone call). + } + + func sceneWillEnterForeground(_: UIScene) { + // Called as the scene transitions from the background to the foreground. + // Use this method to undo the changes made on entering the background. + } + + func sceneDidEnterBackground(_: UIScene) { + // Called as the scene transitions from the foreground to the background. + // Use this method to save data, release shared resources, and store enough scene-specific state information + // to restore the scene back to its current state. + } +} diff --git a/Examples/UIKit/UIKitExample/ViewController.swift b/Examples/UIKit/UIKitExample/ViewController.swift new file mode 100644 index 0000000..478b50e --- /dev/null +++ b/Examples/UIKit/UIKitExample/ViewController.swift @@ -0,0 +1,244 @@ +// +// Validator +// Copyright © 2025 Space Code. All rights reserved. +// + +import UIKit +import ValidatorCore +import ValidatorUI + +// MARK: - ViewController + +final class ViewController: UIViewController { + // MARK: - Properties + + // UI + private let scrollView: UIScrollView = { + let scrollView = UIScrollView() + scrollView.translatesAutoresizingMaskIntoConstraints = false + return scrollView + }() + + private let contentView: UIView = { + let view = UIView() + view.translatesAutoresizingMaskIntoConstraints = false + return view + }() + + private lazy var firstNameTextField: UITextField = { + let textField = makeField("First Name") + textField.validationRules = [ + LengthValidationRule(min: 2, max: 50, error: "First name should be 2–50 characters long"), + ] + return textField + }() + + private lazy var lastNameTextField: UITextField = { + let textField = makeField("Last Name") + textField.validationRules = [ + LengthValidationRule(min: 2, max: 50, error: "Last name should be 2–50 characters long"), + ] + return textField + }() + + private lazy var emailTextField: UITextField = { + let textField = makeField("Email") + textField.keyboardType = .emailAddress + textField.validationRules = [ + EmailValidationRule(error: "Please enter a valid email address"), + ] + return textField + }() + + private lazy var submitButton: UIButton = { + let button = UIButton(type: .system) + button.setTitle("Sign Up", for: .normal) + button.layer.cornerRadius = 10 + button.backgroundColor = .systemGray4 + button.setTitleColor(.white, for: .normal) + button.translatesAutoresizingMaskIntoConstraints = false + button.addTarget(self, action: #selector(submit), for: .touchUpInside) + return button + }() + + private let stackView: UIStackView = { + let stackView = UIStackView() + stackView.axis = .vertical + stackView.spacing = 16 + stackView.translatesAutoresizingMaskIntoConstraints = false + return stackView + }() + + // Private properties + private var isValid: Bool { + [firstNameTextField, lastNameTextField, emailTextField] + .allSatisfy { $0.validationResult == .valid } + } + + // MARK: - Lifecycle + + override func viewDidLoad() { + super.viewDidLoad() + view.backgroundColor = .systemBackground + configure() + } + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + registerKeyboardNotifications() + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + unregisterKeyboardNotifications() + } + + // MARK: - UI Setup + + private func configure() { + for item in [firstNameTextField, lastNameTextField, emailTextField] { + item.validationHandler = { [weak self] _ in + guard let self else { return } + updateSubmitButtonState() + } + } + + [firstNameTextField, lastNameTextField, emailTextField, submitButton] + .forEach(stackView.addArrangedSubview) + + view.addSubview(scrollView) + scrollView.addSubview(contentView) + contentView.addSubview(stackView) + + NSLayoutConstraint.activate([ + scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor), + scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor), + scrollView.topAnchor.constraint(equalTo: view.topAnchor), + scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor), + + contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor), + contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor), + contentView.topAnchor.constraint(equalTo: scrollView.topAnchor), + contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor), + contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor), + contentView.heightAnchor.constraint(equalTo: scrollView.heightAnchor), + + stackView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), + stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 24), + stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -24), + + submitButton.heightAnchor.constraint(equalToConstant: 48), + ]) + } + + private func updateSubmitButtonState() { + submitButton.isEnabled = isValid + UIView.animate(withDuration: 0.25) { + self.submitButton.backgroundColor = self.isValid ? .systemBlue : .systemGray4 + } + } + + // MARK: Private + + private func makeField(_ placeholder: String) -> UITextField { + let textField = UITextField() + textField.placeholder = placeholder + + textField.layer.cornerRadius = 10 + textField.layer.borderWidth = 1 + textField.layer.borderColor = UIColor.systemGray4.cgColor + textField.backgroundColor = UIColor.secondarySystemBackground + textField.textColor = .label + + textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 12, height: 0)) + textField.leftViewMode = .always + + textField.heightAnchor.constraint(equalToConstant: 56.0).isActive = true + textField.translatesAutoresizingMaskIntoConstraints = false + textField.validateOnInputChange(isEnabled: true) + + return textField + } + + // MARK: Notifications + + private func registerKeyboardNotifications() { + NotificationCenter.default.addObserver( + self, + selector: #selector(keyboardWillShow(_:)), + name: UIResponder.keyboardWillShowNotification, + object: nil + ) + + NotificationCenter.default.addObserver( + self, + selector: #selector(keyboardWillHide(_:)), + name: UIResponder.keyboardWillHideNotification, + object: nil + ) + } + + private func unregisterKeyboardNotifications() { + NotificationCenter.default.removeObserver(self) + } + + // MARK: - Actions + + @objc + private func submit() { + guard isValid else { + let alert = UIAlertController( + title: "The form is invalid", + message: "Please check the highlighted fields and correct the entered information.", + preferredStyle: .alert + ) + alert.addAction(UIAlertAction(title: "Got it", style: .default)) + + present(alert, animated: true) + return + } + + let alert = UIAlertController( + title: "Success 🎉", + message: "Your account has been successfully created.", + preferredStyle: .alert + ) + alert.addAction(UIAlertAction(title: "OK", style: .default)) + + present(alert, animated: true) + } + + @objc + private func keyboardWillShow(_ notification: Notification) { + guard + let userInfo = notification.userInfo, + let frameValue = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue + else { return } + + let keyboardFrame = frameValue.cgRectValue + let bottomInset = keyboardFrame.height - view.safeAreaInsets.bottom + + scrollView.contentInset.bottom = bottomInset + + if let firstResponder = view.findFirstResponder(), + !scrollView.frame.contains(firstResponder.frame) + { + scrollView.scrollRectToVisible(firstResponder.frame, animated: true) + } + } + + @objc + private func keyboardWillHide(_: Notification) { + scrollView.contentInset.bottom = .zero + } +} + +extension UIView { + func findFirstResponder() -> UIView? { + if isFirstResponder { return self } + for sub in subviews { + if let found = sub.findFirstResponder() { return found } + } + return nil + } +} From be750a4ea20fb9b1e072e2d0b30c8203f7bbdaad Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Thu, 27 Nov 2025 16:16:41 +0400 Subject: [PATCH 2/2] docs: add the `example` section --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 9a73386..de6482d 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,11 @@ Validator is a modern, lightweight Swift framework that provides elegant and typ - [Form Validation](#form-validation) - [Built-in Validators](#built-in-validators) - [Custom Validators](#custom-validators) +- [Examples](#examples) +- [Communication](#communication) - [Contributing](#contributing) + - [Development Setup](#development-setup) + - [Code of Conduct](#code-of-conduct) - [License](#license) ## Requirements @@ -395,6 +399,13 @@ SecureField("Password", text: $password) } ``` +## Examples + +You can find usage examples in the [Examples](./Examples/) directory of the repository. + +These examples demonstrate how to integrate the package, configure validation rules, +and build real-world user interfaces using `ValidatorCore` and `ValidatorUI`. + ## Communication - 🐛 **Found a bug?** [Open an issue](https://github.com/space-code/validator/issues/new?template=bug_report.md)