Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 9ed6c65

Browse files
committed
Merge pull request #22 from Aldredcz/patch-1
Making `all` validator chainable (isRequired)
2 parents d056173 + d953fff commit 9ed6c65

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/all.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {createChainableTypeChecker} from './common';
2+
13
export default function all(...propTypes) {
24
if (propTypes === undefined) {
35
throw new Error('No validations provided');
@@ -11,13 +13,15 @@ export default function all(...propTypes) {
1113
throw new Error('No validations provided');
1214
}
1315

14-
return function validate(props, propName, componentName) {
16+
function validate(props, propName, componentName) {
1517
for (let i = 0; i < propTypes.length; i++) {
1618
const result = propTypes[i](props, propName, componentName);
1719

1820
if (result !== undefined && result !== null) {
1921
return result;
2022
}
2123
}
22-
};
24+
}
25+
26+
return createChainableTypeChecker(validate);
2327
}

test/allSpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@ describe('all', function() {
5656

5757
validators[2].should.not.have.been.called;
5858
});
59+
60+
it('always fails when prop value is missing and isRequired is used', function() {
61+
const missingPropName = 'missing';
62+
const allValidator = all(...validators).isRequired;
63+
const expectedErr = new Error(
64+
`Required prop '${missingPropName}' was not specified in '${componentName}'.`
65+
);
66+
67+
const result = allValidator(props, missingPropName, componentName);
68+
expect(result.toString()).to.equal(expectedErr.toString()); // cannot compare values, as we got different Error instances here.
69+
70+
validators[0].should.not.have.been.called;
71+
validators[1].should.not.have.been.called;
72+
validators[2].should.not.have.been.called;
73+
});
5974
});

0 commit comments

Comments
 (0)