diff --git a/docs/rules/require-prop-types.md b/docs/rules/require-prop-types.md index c163a6709..4e54223d6 100644 --- a/docs/rules/require-prop-types.md +++ b/docs/rules/require-prop-types.md @@ -17,18 +17,32 @@ props: ['status'] :+1: Examples of **correct** code for this rule: ```js +// Without options, just type reference props: { status: String } ``` ```js +// With options with type field props: { status: { type: String, required: true, - validate: function (value) { - return ['syncing', 'synced', 'version-conflict', 'error'].indexOf(value) !== -1 + } +} +``` + +```js +// With options without type field but with validator field +props: { + status: { + required: true, + validator: function (value) { + return ( + value === null || + Array.isArray(value) && value.length > 0 + ) } } } diff --git a/lib/rules/require-prop-types.js b/lib/rules/require-prop-types.js index 2564f5c02..e54a11a08 100644 --- a/lib/rules/require-prop-types.js +++ b/lib/rules/require-prop-types.js @@ -37,7 +37,9 @@ module.exports = { p.value.elements.length > 0 ) ) - return Boolean(typeProperty) + const validatorProperty = node.properties + .find(p => utils.getStaticPropertyName(p.key) === 'validator') + return Boolean(typeProperty || validatorProperty) } function checkProperties (items) { diff --git a/tests/lib/rules/require-prop-types.js b/tests/lib/rules/require-prop-types.js index 5f9342e7a..683e8a5bb 100644 --- a/tests/lib/rules/require-prop-types.js +++ b/tests/lib/rules/require-prop-types.js @@ -70,6 +70,32 @@ ruleTester.run('require-prop-types', rule, { `, parserOptions: { ecmaVersion: 6, sourceType: 'module' } }, + { + filename: 'test.vue', + code: ` + export default { + props: { + foo: { + validator: v => v + } + } + } + `, + parserOptions: { ecmaVersion: 6, sourceType: 'module' } + }, + { + filename: 'test.vue', + code: ` + export default { + props: { + foo: { + ['validator']: v => v + } + } + } + `, + parserOptions: { ecmaVersion: 6, sourceType: 'module' } + }, { filename: 'test.vue', code: `