diff options
Diffstat (limited to 'readme.md')
-rw-r--r-- | readme.md | 20 |
1 files changed, 7 insertions, 13 deletions
@@ -28,28 +28,22 @@ the use of which being problematic in performance-sensitive contexts. | |||
28 | import static org.pacien.lemonad.attempt.Attempt.*; | 28 | import static org.pacien.lemonad.attempt.Attempt.*; |
29 | 29 | ||
30 | (tree.hasLemon() ? success(tree.getLemon()) : failure("No lemon.")) | 30 | (tree.hasLemon() ? success(tree.getLemon()) : failure("No lemon.")) |
31 | .recoverError(__ -> store.buyLemon()) | 31 | .recoverError(error -> store.buyLemon()) |
32 | .transformResult(this::makeLemonade) | 32 | .transformResult(this::makeLemonade) |
33 | .ifSuccess(this::drink); | 33 | .ifSuccess(this::drink); |
34 | ``` | 34 | ``` |
35 | 35 | ||
36 | ### Validation | 36 | ### Validation |
37 | 37 | ||
38 | The `Validation` monad represents a validation of a subject which can be either valid or invalid. | 38 | The `Validation` monad represents a validation of a subject which can be successful or failed with errors. |
39 | In the latter case, the monad wraps one or multiple validation errors in addition to the subject of the validation. | 39 | Those errors are aggregated from all the checks that have failed. |
40 | |||
41 | The `Validator` functional interface represents a function which performs verification operations on a supplied subject and returns | ||
42 | a `Validation`. | ||
43 | `Validator`s can be composed to perform verifications against multiple criteria and obtain an aggregated `Validation`. | ||
44 | 40 | ||
45 | ```java | 41 | ```java |
46 | import static org.pacien.lemonad.validation.Validator.*; | 42 | import org.pacien.lemonad.validation.Validation; |
47 | |||
48 | var validator = validatingAll( | ||
49 | ensuringPredicate(not(Lemon::isRotten), "Bad lemon."), | ||
50 | validatingField(Lemon::juiceContent, ensuringPredicate(mL -> mL >= 40, "Not juicy."))); | ||
51 | 43 | ||
52 | validator.validate(lemon) | 44 | Validation.of(lemon) |
45 | .validate(not(Lemon::isRotten), "Bad lemon.") | ||
46 | .validate(Lemon::juiceContent, mL -> mL >= 40, "Not juicy") | ||
53 | .ifValid(this::makeLemonade) | 47 | .ifValid(this::makeLemonade) |
54 | .ifInvalid(errors -> makeLifeTakeTheLemonBack()); | 48 | .ifInvalid(errors -> makeLifeTakeTheLemonBack()); |
55 | ``` | 49 | ``` |