Last active
April 30, 2019 10:08
-
-
Save busypeoples/0bbf2f5a4fae4e738bc22bdd95ad2434 to your computer and use it in GitHub Desktop.
Small Validation Library
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 { converge, mergeAll } = require("ramda"); | |
// Small validation function... | |
const validate = (validations) => { | |
return converge((...results) => mergeAll(results), validations); | |
}; | |
// Validators | |
const isGt4 = x => x > 4; | |
const isGT8 = x => x > 8; | |
// Values | |
const fieldValues = { | |
a: 3, | |
b: 9 | |
}; | |
const validateForm = validate([ | |
({ a }) => ({ a: isGt4(a) ? true : "Needs to be greater than 4" }), | |
({ b }) => ({ b: isGT8(b) ? true : "Needs to be greater than 8" }) | |
]); | |
console.log(validateForm(fieldValues)); | |
// => { a: 'Needs to be greater than 4', b: true } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment