Permalink
Browse files

(new validation) Adds a function to aggregate lists of validations (#71)

This closes #71.
  • Loading branch information...
robotlolita committed Mar 5, 2017
1 parent 6f59f61 commit a677e967e4e1cf9f68a5503d5a49d802a3b9ce6a
Showing with 50 additions and 0 deletions.
  1. +24 −0 src/data/validation/collect.js
  2. +1 −0 src/data/validation/index.js
  3. +25 −0 test/specs-src/data.validation.js
@@ -0,0 +1,24 @@
//----------------------------------------------------------------------
//
// This source file is part of the Folktale project.
//
// Licensed under MIT. See LICENCE for full licence information.
// See CONTRIBUTORS for the list of contributors to the project.
//
//----------------------------------------------------------------------
const { Success } = require('./validation');
/*~
* stability: experimental
* type: |
* forall a, b: (Array (Validation a b)) => Validation a b
* where a is Semigroup
*/
const collect = (validations) =>
validations.reduce((a, b) => a.concat(b), Success());
module.exports = collect;
@@ -10,6 +10,7 @@
/*~ stability: experimental */
module.exports = {
...require('./validation'),
collect: require('./collect'),
/*~
* type: |
@@ -11,7 +11,32 @@ const { property, forall} = require('jsverify');
const _ = require('folktale/data/validation');
const laws = require('../helpers/fantasy-land-laws');
describe('Data.Validation', () => {
describe('collect(validations)', () => {
it('failures are concatenated', () => {
const a = _.Failure('a');
const b = _.Failure('b');
$ASSERT(_.collect([a, b]) == _.Failure('ab'));
});
it('successes are ignored in the presence of failures', () => {
const a = _.Failure('a');
const b = _.Failure('b');
const c = _.Success('c');
$ASSERT(_.collect([a, b, c]) == _.Failure('ab'));
});
it('keeps the last success', () => {
const a = _.Success(1);
const b = _.Success(2);
$ASSERT(_.collect([a, b]) == _.Success(2));
});
});
describe('Functor', () => {
property('map', 'json', 'json -> json', (a, f) => {
return _.of(f(a)).equals(_.of(a).map(f))

0 comments on commit a677e96

Please sign in to comment.