CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

ValidatedInjectAdditions 0.3.0

ValidatedInjectAdditions 0.3.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2016
SPMSupports SPM

Maintained by Valentin Knabel.



 
Depends on:
EasyInject~> 0.8
ValidatedExtension~> 4.0
 

ValidatedInjectAdditions adds some convenience methods for projects that are using EasyInject and ValidatedExtension.

Installation

EasyInject is a Swift only project and supports Swift Package Manager, Carthage and CocoaPods.

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .Package(url: "https://github.com/vknabel/EasyInject.git", majorVersion: 0, minor: 6),
        .Package(url: "https://github.com/vknabel/ValidatedExtension.git", majorVersion: 3, minor: 0),
        .Package(url: "https://github.com/vknabel/ValidatedInjectAdditions.git", majorVersion: 0, minor: 2)
    ]
)

Example

This code snippet is built on top of (EasyInject)[https://www.github.com/vknabel/EasyInject/README.md]'s example.

import EasyInject
import ValidatedExtension
import ValidatedInjectAdditions

// Define your a Validator as known from ValidatedExtension
struct NetworkServiceInjectorValidator: Validator {
    typealias WrappedType = AnyInjector<String>
    static func validate(_ value: AnyInjector<String>) throws -> Bool {
        let base = try value.resolving(from: .baseUrl)
        return URL(string: base)
            .flatMap({ _ in true })
            ?? false
    }
}

// Add properties to Validated<NetworkServiceInjectorValidator>
extension ValidatedType where ValidatorType == NetworkServiceInjectorValidator {
    var baseUrl: String {
        return try! value.resolving(from: .baseUrl)
    }
}

final class NetworkService: Providable, ValidatedDependency {
    // Declare your dependency
    typealias DependencyValidator = NetworkServiceInjectorValidator

    let baseUrl: String
    init<VT : ValidatedType where VT.ValidatorType == DependencyValidator>(validated dependencies: VT) {
        // Just focus on setting thing up 
        baseUrl = dependencies.baseUrl
    }
}

var injector = LazyInjector<String>().globalize().erase()
injector.provide(for: .baseUrl, usingFactory: { _ in "https://my.base.url/" })

// this will throw an error if .baseUrl is missing
// Otherwise initializes NetworkService
let networkService = try NetworkService(injector: &injector)

Author

Valentin Knabel, [email protected]

License

ValidatedInjectAdditions is available under the MIT license. See the LICENSE file for more info.