Created
December 9, 2020 18:29
-
-
Save asimonok/3d09f903814dc7d0cbf2dde36a910796 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
const createValidation = (...validators) => (value) => { | |
const errors = validators.reduce((errors, validator) => { | |
const error = validator(value) | |
if (error) { | |
return errors.concat(error) | |
} | |
return errors | |
}, []) | |
return errors.length > 0 ? errors : null | |
} | |
const createValidator = (checkFn, errorMessage) => { | |
return value => { | |
if (!checkFn(value)) { | |
return errorMessage | |
} | |
} | |
} | |
const hasEmail = value => /^(([^<>()\[\]\\.,;:\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(value) | |
const hasNoEmpty = (value = '') => !!value.trim() | |
const hasAdult = (age) => age >= 18 | |
module.exports = { | |
createValidation, | |
createValidator, | |
hasEmail, | |
hasNoEmpty, | |
hasAdult, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment