Set of useful validators which are build on top of Validation
from purescript-polyform.
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.
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):
type SearchResult image =
{ page ∷ Int
, perPage ∷ Int
, totalCount ∷ Int
, searchId ∷ String
, photos ∷ Array image
}
searchResult
:: forall err m
. Monad m
=> Validation m
(Array (Variant (JsError err)))
Json
(SearchResult Image)
searchResult = collect
{ page: field "page" int
, perPage: field "per_page" int
, totalCount: field "total_count" int
, searchId: field "search_id" string
, photos: field "data" $ arrayOf image
}
Now using affjaxJson
validator from this library we can chain these two validators to get fully validated request:
searchValidation
:: forall ext err
. Validation
( Aff( ajax :: AJAX| ext))
(Array(Variant(SearchErrorRow err)))
(AffjaxRequest Unit)
(SearchResult Image)
searchValidation = searchResult <<< affjaxJson
For more usage examples please refer these simple libraries: purescript-shutterstock, purescript-pexels or purescript-nasa-images.