Validator icon indicating copy to clipboard operation
Validator copied to clipboard

Alternative method of validationResult merge method

Open nrikiji opened this issue 4 years ago • 2 comments

https://github.com/adamwaite/Validator/commit/f9e0b5916e493e9750915a473b0fdf3581fe7aa0#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

nrikiji avatar Sep 19 '19 05:09 nrikiji

@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?

piv199 avatar Oct 25 '19 14:10 piv199

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)
}

}`

muni510 avatar Nov 19 '19 13:11 muni510