Skip to content

Instantly share code, notes, and snippets.

@danneu
Created November 9, 2017 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/a73edbabf180a05c8b22f1904cda0d50 to your computer and use it in GitHub Desktop.
Save danneu/a73edbabf180a05c8b22f1904cda0d50 to your computer and use it in GitHub Desktop.
{
console.log('=== body validation')
const body = {
uname: 'foo',
email: 'me@example.com',
pass1: 'secret',
pass2: 'secret',
}
const v = Validator.succeed(body)
.andThen(() => {
return Validator.get('uname', Validator.string()).map(uname => ({
uname,
}))
})
.andThen(acc => {
return Validator.get(
'email',
Validator.string().andThen(Validator.isEmail)
).map(email => ({
...acc,
email,
}))
})
.andThen(acc => {
return Validator.get('pass1', Validator.string()).map(pass1 => ({
...acc,
pass1,
}))
})
.andThen(acc => {
return Validator.get(
'pass2',
Validator.string().andThen(() => Validator.equalTo(acc.pass1))
)
.mapError(() => 'password must match confirmation')
.map(() => acc)
})
console.log(v.apply(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment