Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Last active April 30, 2019 10:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busypeoples/0bbf2f5a4fae4e738bc22bdd95ad2434 to your computer and use it in GitHub Desktop.
Save busypeoples/0bbf2f5a4fae4e738bc22bdd95ad2434 to your computer and use it in GitHub Desktop.
Small Validation Library
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