Skip to content

Instantly share code, notes, and snippets.

@Svetixbot
Last active March 6, 2018 03:43
Show Gist options
  • Save Svetixbot/b18213d6691b3073152b150536ed1137 to your computer and use it in GitHub Desktop.
Save Svetixbot/b18213d6691b3073152b150536ed1137 to your computer and use it in GitHub Desktop.
/*
* There are 2 {validation} patterns. There is always place for both of them. How do I do the second one?
* 1. Validate against different validators and return the first failure.
* 2. Validate against different validators and return all the failures.
*/
/* 1. Compose and terminate with first failure */
const validateMinMagicNumber = (min_magic) => ( // accepts a number and returns string with the first error.
isEmpty(min_magic, 'Min Magic is required') ||
failedParseNumber(min_magic) ||
outsideOfMagicRange(parseNumber(min_magic), 'Min Magic should be between 1 and 9999') ||
isGreaterThan(parseNumber(min_magic), parseNumber(MAX_MAGIC), 'Min Magic cannot be greater than MAX MAGIC') ||
isGreaterThan(parseNumber(min_magic), parseNumber(AVG_MAGIC), 'Min Magic cannot be greater than AVG Magic')
);
/* 2. Compose and collect all failures, lift them into dictionary */
/* Let's say, <*> is an operator which creates a tupple or some sort of collection of arguments,
<?> is a function with runs apply of these arguments on the next function */
const validateMagicValuesX = (min_magic, avg_magic, max_magic) => (
return (validateMinMagicNumber(min_magic) <*> validateAvgMagicNumber(avg_magic) <*> validateMaxMagicNumber(max_magic)) <?>
((minError, avgError, maxError) => {"minMagic": minError, "avgMagic": avgError, "maxMagic": maxError})
);
/* 2. Compose and collect all failures, lift them into dictionary */
const validateMagicValuesY = (min_magic, avg_magic, max_magic) => (
const errors = {};
errors.minMagic = validateMinMagicNumber(min_magic);
errors.avgMagic = validateAvgMagicNumber(avg_magic);
errors.maxMagic = validateMaxMagicNumber(max_magic);
return errors;
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment