Skip to content

Commit e67538b

Browse files
committed
Merge pull request #160 from CalebMorris/customValidatorDetection
Add customValidators option to prop-types (fixes #145)
2 parents e4e0b42 + f024c8d commit e67538b

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

lib/rules/prop-types.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function(context) {
1818

1919
var configuration = context.options[0] || {};
2020
var ignored = configuration.ignore || [];
21+
var customValidators = configuration.customValidators || [];
2122

2223
var componentList = new ComponentList();
2324

@@ -72,6 +73,15 @@ module.exports = function(context) {
7273
return ignored.indexOf(name) !== -1;
7374
}
7475

76+
/**
77+
* Checks if prop should be validated by plugin-react-proptypes
78+
* @param {String} validator Name of validator to check.
79+
* @returns {Boolean} True if validator should be checked by custom validator.
80+
*/
81+
function hasCustomValidator(validator) {
82+
return customValidators.indexOf(validator) !== -1;
83+
}
84+
7585
/**
7686
* Checks if the component must be validated
7787
* @param {Object} component The component to process
@@ -203,6 +213,15 @@ module.exports = function(context) {
203213
* the property is declared without the need for further analysis.
204214
*/
205215
function buildReactDeclarationTypes(value) {
216+
if (
217+
value &&
218+
value.callee &&
219+
value.callee.object &&
220+
hasCustomValidator(value.callee.object.name)
221+
) {
222+
return true;
223+
}
224+
206225
if (
207226
value.type === 'MemberExpression' &&
208227
value.property &&
@@ -610,6 +629,12 @@ module.exports.schema = [{
610629
items: {
611630
type: 'string'
612631
}
632+
},
633+
customValidators: {
634+
type: 'array',
635+
items: {
636+
type: 'string'
637+
}
613638
}
614639
},
615640
additionalProperties: false

tests/lib/rules/prop-types.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,87 @@ eslintTester.addRuleTest('lib/rules/prop-types', {
597597
classes: true,
598598
jsx: true
599599
}
600+
}, {
601+
code: [
602+
'var Hello = React.createClass({',
603+
' propTypes: {',
604+
' firstname: CustomValidator.string',
605+
' },',
606+
' render: function() {',
607+
' return <div>{this.props.firstname}</div>;',
608+
' }',
609+
'});'
610+
].join('\n'),
611+
args: [1, {customValidators: ['CustomValidator']}],
612+
ecmaFeatures: {
613+
jsx: true
614+
}
615+
}, {
616+
code: [
617+
'var Hello = React.createClass({',
618+
' propTypes: {',
619+
' outer: CustomValidator.shape({',
620+
' inner: CustomValidator.map',
621+
' })',
622+
' },',
623+
' render: function() {',
624+
' return <div>{this.props.outer.inner}</div>;',
625+
' }',
626+
'});'
627+
].join('\n'),
628+
args: [1, {customValidators: ['CustomValidator']}],
629+
ecmaFeatures: {
630+
jsx: true
631+
}
632+
}, {
633+
code: [
634+
'var Hello = React.createClass({',
635+
' propTypes: {',
636+
' outer: React.PropTypes.shape({',
637+
' inner: CustomValidator.string',
638+
' })',
639+
' },',
640+
' render: function() {',
641+
' return <div>{this.props.outer.inner}</div>;',
642+
' }',
643+
'});'
644+
].join('\n'),
645+
args: [1, {customValidators: ['CustomValidator']}],
646+
ecmaFeatures: {
647+
jsx: true
648+
}
649+
}, {
650+
code: [
651+
'var Hello = React.createClass({',
652+
' propTypes: {',
653+
' outer: CustomValidator.shape({',
654+
' inner: React.PropTypes.string',
655+
' })',
656+
' },',
657+
' render: function() {',
658+
' return <div>{this.props.outer.inner}</div>;',
659+
' }',
660+
'});'
661+
].join('\n'),
662+
args: [1, {customValidators: ['CustomValidator']}],
663+
ecmaFeatures: {
664+
jsx: true
665+
}
666+
}, {
667+
code: [
668+
'var Hello = React.createClass({',
669+
' propTypes: {',
670+
' name: React.PropTypes.string',
671+
' },',
672+
' render: function() {',
673+
' return <div>{this.props.name.get("test")}</div>;',
674+
' }',
675+
'});'
676+
].join('\n'),
677+
args: [1, {customValidators: ['CustomValidator']}],
678+
ecmaFeatures: {
679+
jsx: true
680+
}
600681
}, {
601682
code: [
602683
'class Comp1 extends Component {',

0 commit comments

Comments
 (0)