-
-
Save danneu/a73edbabf180a05c8b22f1904cda0d50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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