1. kimagure

    No comment

    kimagure
Changes in body
Source | HTML | Preview
@@ -1,20 +1,21 @@
-In many programs we write, we have a need to validate data before we perform any more operations. Unfortunately, most of time we end up with a solution that requires us to build a list of errors and check this list's contents for being empty in some kind of way, but there are easier and nicer solutions to this problem. This is where PureScript's Validation library comes into play.
+In many programs that we write we have the need to validate data before we perform any more operations. Unfortunately, most of time we end up with a solution that requires us to build a list of errors and check this list's contents for being empty, but there are easier and nicer solutions to this problem. This is where PureScript's Validation library comes into play.
## What is Validation?
```hs
data V err result
```
The Validation package gives us a nice way to validate values using applicative validation. From the docs:
```
-Applicative validation differs from monadic validation using Either in that it allows us to collect multiple errors using a Semigroup, whereas Either terminates on the first error.
+Applicative validation differs from monadic validation using Either in that it allows us to collect
+multiple errors using a Semigroup, whereas Either terminates on the first error.
```
-In other terms, while modeling validations with Either would fall out on the first error, we can collect one or more errors in this case using a Semigroup, or a structure that is capable of associative appending of elements.
+In other words, modeling validations with Either would fall out on the first error. We can collect one or more errors in this case using a structure that is an instance of Semigroup.
Note that this does not work with a Monoid. Using a Monoid would not make sense in that it would provide an identity that cannot reasonably be used -- there is no case in which you would have an empty list of errors and fall on the error case of the validation.
## Record validation example
@@ -102,11 +103,11 @@
errorMyValidated2 :: MyValidated FormData
errorMyValidated2 =
{ appleColor: _, bananaColor: _, carrotColor: testData.carrotColor }
<$> appleIsRed "yellow"
<*> bananaIsNotGreen "green"
-
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
printMyValidated testMyValidated
-- output:
-- the apples were red and the bananas yellow