Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Alternative method of validationResult merge method #126

Open
nrikiji opened this issue Sep 19, 2019 · 2 comments
Open

Alternative method of validationResult merge method #126

nrikiji opened this issue Sep 19, 2019 · 2 comments

Comments

@nrikiji
Copy link

nrikiji commented Sep 19, 2019

f9e0b59#diff-82cfdd33ac3981dee67efa99e2338154L14

this is question.

With this commit, ValidationResult.swift's merge method has been deleted, but I would like to know if there is an alternative method here

@piv199
Copy link

piv199 commented Oct 25, 2019

@adamwaite this seems reasonable.

As fo this commit I also can't understand why you have moved to ValidationError everywhere thus it gives additional overhead of passing non-owning errors?

@muni510
Copy link

muni510 commented Nov 19, 2019

f9e0b59#diff-82cfdd33ac3981dee67efa99e2338154L14

this is question.

With this commit, ValidationResult.swift's merge method has been deleted, but I would like to know if there is an alternative method here

I have extended the class with merge method

`
extension ValidationResult {
/**

 Merges the receiving validation rule with another.
 
 ```
 .valid.merge(.valid) // = .valid
 .valid.merge(.invalid([err])) // = .invalid([err])
 .invalid([err1]).merge(.invalid([err2]) // = .invalid([err1, err2])
 ```
 
 - Parameters:
    - result: The result to merge into the receiver.
 
 - Returns:
 Merged validation result.
 
 */
public func merge(with result: ValidationResult) -> ValidationResult {
    switch self {
    case .valid: return result
    case .invalid(let errorMessages):
        switch result {
        case .valid:
            return self
        case .invalid(let errorMessagesAnother):
            return .invalid([errorMessages, errorMessagesAnother].flatMap { $0 })
        }
    }
}

/**
 
 Merges the receiving validation rule with multiple others.
 
 - Parameters:
    - results: The results to merge the receiver.
 
 - Returns:
 Merged validation result.
 
 */
public func merge(with results: [ValidationResult]) -> ValidationResult {
    return results.reduce(self) { return $0.merge(with: $1) }
}

/**
 
 Merges multiple validation rules together.
 
 - Parameters:
    - results: The results to merge.
 
 - Returns:
 Merged validation result.
 
 */
public static func merge(results: [ValidationResult]) -> ValidationResult {
    return ValidationResult.valid.merge(with: results)
}

}`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants