Skip to content

Instantly share code, notes, and snippets.

@asimonok
Created December 9, 2020 18:29
Show Gist options
  • Save asimonok/3d09f903814dc7d0cbf2dde36a910796 to your computer and use it in GitHub Desktop.
Save asimonok/3d09f903814dc7d0cbf2dde36a910796 to your computer and use it in GitHub Desktop.
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