Created
May 26, 2018 17:06
-
-
Save maciejsmolinski/69fccf5082db3174a42fd0d2a216b0bc to your computer and use it in GitHub Desktop.
sanctuary-validations.js
This file contains hidden or 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
const validations = { | |
address: (street, postcode, city) => { | |
if (street && postcode && city) { | |
return S.Right(`${street}, ${postcode}, ${city}`) | |
} | |
return S.Left(`Incorrect address provided (${street}, ${postcode}, ${city})`); | |
}, | |
email: (email) => { | |
if (/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email)) { | |
return S.Right(email); | |
} | |
return S.Left(`Incorrect email address provided (${email})`); | |
} | |
} | |
const register = function (email, street, postcode, city) { | |
const result = S.sequence( | |
S.Right, | |
[ | |
validations.email(email), | |
validations.address(street, postcode, city) | |
] | |
); | |
if (S.isRight(result)) { | |
console.log('Registered!'); | |
} else { | |
console.warn('Error found', result.value); | |
} | |
} | |
register(); | |
register('test'); | |
register('[email protected]'); | |
register('[email protected]', 'a'); | |
register('[email protected]', 'a', 'b'); | |
register('[email protected]', 'a', 'b', 'c'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment