You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Set of useful validators which are build on top of `Validation` from purescript-polyform.
4
+
5
+
## Usage
6
+
7
+
`Validation` type from polyform has Applicative and Category instance. Using these two interfaces and provided validators you can quite easily write whole validation chain: from ajax request to resulting object. This validation flow will aggregate in typed manner all encoutered errors at given failing step.
8
+
9
+
Let's look at real example - here we are making a request to shutterstock API and validating its response. `Data.Record.Fold.collect` is like sequence but over record (so it takes an record of `Validation`s and returns `Validation` into record):
10
+
11
+
```purescript
12
+
type SearchResult image =
13
+
{ page ∷ Int
14
+
, perPage ∷ Int
15
+
, totalCount ∷ Int
16
+
, searchId ∷ String
17
+
, photos ∷ Array image
18
+
}
19
+
20
+
searchResult
21
+
:: forall err m
22
+
. Monad m
23
+
=> Validation m
24
+
(Array (Variant (JsError err)))
25
+
Json
26
+
(SearchResult Image)
27
+
searchResult = collect
28
+
{ page: field "page" int
29
+
, perPage: field "per_page" int
30
+
, totalCount: field "total_count" int
31
+
, searchId: field "search_id" string
32
+
, photos: field "data" $ arrayOf image
33
+
}
34
+
```
35
+
36
+
Now using `affjaxJson` validator from this library we can chain these two validators to get fully validated request:
37
+
38
+
```purescript
39
+
searchValidation
40
+
:: forall ext err
41
+
. Validation
42
+
( Aff( ajax :: AJAX| ext))
43
+
(Array(Variant(SearchErrorRow err)))
44
+
(AffjaxRequest Unit)
45
+
(SearchResult Image)
46
+
searchValidation = searchResult <<< affjaxJson
47
+
```
48
+
49
+
## Examples
50
+
51
+
For more usage examples please refer these simple libraries: [purescript-shutterstock](https://github.com/lambdaterms/purescript-shutterstock), [purescript-pexels](https://github.com/lambdaterms/purescript-pexels) or [purescript-nasa-images](https://github.com/lambdaterms/purescript-nasa-images).
0 commit comments