Created
March 1, 2017 14:50
-
-
Save kmtr/1e69a1baca8eb5a36cab3c6bae49c553 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
function validate(val, ...validateFunctions) { | |
for (let f of validateFunctions) { | |
const err = f(val); | |
if (err !== val) { | |
return err; | |
} | |
} | |
return null; | |
} | |
function validateRequired(val) { | |
if (!val) { | |
return "required"; | |
} | |
return val; | |
} | |
function validateIsNum(val) { | |
if (val !== 0 && !val) { | |
return val; | |
} | |
if (!Number.isInteger(val)) { | |
return "shuld be num"; | |
} | |
return val; | |
} | |
console.log(validate("test", validateRequired, validateIsNum)) | |
console.log(validate("", validateRequired, validateIsNum)) | |
console.log(validate(1, validateRequired, validateIsNum)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment