gcanti / flow-static-land

[DEPRECATED, please check out fp-ts] Implementation of common algebraic types in JavaScript + Flow
MIT License
408 stars 22 forks source link

Validation #54

Open alexeygolev opened 7 years ago

alexeygolev commented 7 years ago

Thank you for the library! It's very impressive. I'm trying to wrap my head around validation but struggling to do so. It does seem to be similar to scalaz validation but I'm not sure how would I use it. Any chance for an example? Thank you again

gcanti commented 7 years ago

It does seem to be similar to scalaz validation

Yep. Adapted from http://eed3si9n.com/learning-scalaz/Validation.html

// @flow

import { getApplicative } from 'flow-static-land/lib/Validation'
import * as either from 'flow-static-land/lib/Either'
import { stringMonoid } from 'flow-static-land/lib/Monoid'

const applicative = getApplicative(stringMonoid)

const x = either.right('event 1 ok')
const y = either.left('event 2 failed!')
const z = either.left('event 3 failed!')

const validation = applicative.ap(applicative.ap(applicative.map(a => b => c => a + b + c, x), y), z)

console.log(validation) // => Left {value0: "event 2 failed!event 3 failed!"}