Skip to content

Instantly share code, notes, and snippets.

@maciejsmolinski
Created May 26, 2018 17:06
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 maciejsmolinski/69fccf5082db3174a42fd0d2a216b0bc to your computer and use it in GitHub Desktop.
Save maciejsmolinski/69fccf5082db3174a42fd0d2a216b0bc to your computer and use it in GitHub Desktop.
sanctuary-validations.js
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('test@email.com');
register('test@email.com', 'a');
register('test@email.com', 'a', 'b');
register('test@email.com', 'a', 'b', 'c');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment