Hello. In this video, I want to show you another way to model failures, which is more appropriate for reporting validation errors to the end users. In the previous videos, we have seen how to use exceptions or Try to handle errors. These solutions have the property of stopping the execution flow of the program on the first error. As a consequence, they might not be appropriate for reporting validation errors to the end users. Indeed, in this case, we might want to provide a list of errors, like, a list of properties that are invalid in a JSON object, or a list of invalid fields in a form. In practice, your company or project is likely to use a third-party library to handle validation errors. In this video, we introduce a general framework to reason about validation errors without relying on a specific library. First, we model validation errors as a collection of String messages with this type alias, Errors. Then we model the type of "a validated value of type A" to be either some Errors or A. We do this with this type Errors, validated of A is Either, Errors, or A. The type Either comes from the Scala standard library, and models a value that can be either of one type or of another type. Either takes two type parameters, the left type, in this case Errors, and the right type, in this case A. So a valid value of type Validated of Int, can be constructed by using the Right constructor, with an Int value. Conversely, an invalid value of type Validated of Int, can be constructed by using the Left constructor, with a collection of error messages. One way of consuming an Either value is to use pattern matching to inspect its content. There are two possible cases, either it's Right or it's a Left. Like we did with Try, we can use high-level transformation operations on the type Either to transform a valid value without having to deal with the error cases. So if I call the operation map, and the validInt value, with this function, I still get a valid value, with an Int, 84. If I call map on the value invalidInt, I still get the initial invalid value. It is worth nothing that the operation map transforms the right value of an Either. For this reason, our Validated of A type, sets the error on the left side and the success on the right side. There is also an operation flatMap on the type Either. It works like flatMap on Try. It takes a continuation function that also returns a validated value. For instance, to parse two dates and compute the period between them, assuming we have this method parseDate, which returns a validated date, we perform a first call to parseDate with the first string, so we get a Validated LocalDate. Then we call the operations flatMap on this result. In the continuation function, we perform a second call to parseDate and then we call the operation map on its result to compute the period between the two dates. So we parse the first date and if it worked, we parse the second date, and if it worked, we compute the duration between them. If you remember well, this implementation is very similar to the one we had with Try in the previous video. However, there is a problem with this implementation. If we call it on two invalid dates, we only get one error. Although, our goal in this video is to report all the possible errors to the users. So here we would like to report two errors. So we need an operation that combines two Validated values such that two valid values produce another valued value, containing a pair, and one or two invalid values produce an invalid value containing all the validation errors. The signature of this operation would be a method that takes two Validated values and it would return a Validated pair of values. Here we take two type parameters, A and B, because two input values can have different types. With such an operation, validateBoth, we can implement validatePeriod like this. We call validateBoth on parseDate of str1 and parseDate of str2, so we get a Validated of a pair of dates. Then we call map to transform this pair of dates into the period between them. With this definition, if we call validateDuration on two invalid dates, both errors are reported. Further record, here is the implementation of validateBoth. We match on both validated values and handle all the possible combinations. In case both are valid, we return a valid pair. In case, the first is invalid, we return its errors. In case the second is invalid, we return its errors. In case both are invalid, we return all their errors. In summary, in this video, we have discovered a new operation for combining validation results, which accumulates validation errors. We have called it validateBoth, but it may exist under the name product or zip in third-party libraries. In addition to validateBoth, transforming values with map and chaining operations that validate values with flatMap are still useful operations.