Skip to content

Commit

Permalink
(new validation) Adds a function to aggregate lists of validations (#71)
Browse files Browse the repository at this point in the history
This closes #71.
  • Loading branch information
robotlolita committed Mar 5, 2017
1 parent 6f59f61 commit a677e96
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/data/validation/collect.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/data/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
/*~ stability: experimental */
module.exports = {
...require('./validation'),
collect: require('./collect'),

/*~
* type: |
Expand Down
25 changes: 25 additions & 0 deletions test/specs-src/data.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a677e96

Please sign in to comment.