From 0269f114e759c9e19f47c7208859a61b10eaa34c Mon Sep 17 00:00:00 2001 From: st-sloth Date: Thu, 17 May 2018 12:59:28 +0500 Subject: [PATCH 1/2] Fix: Allow props with `validator` without `type` in `vue/require-prop-types` --- docs/rules/require-prop-types.md | 14 ++++++++++++++ lib/rules/require-prop-types.js | 4 +++- tests/lib/rules/require-prop-types.js | 26 ++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/rules/require-prop-types.md b/docs/rules/require-prop-types.md index c163a6709..8f91cb17d 100644 --- a/docs/rules/require-prop-types.md +++ b/docs/rules/require-prop-types.md @@ -34,6 +34,20 @@ props: { } ``` +```js +props: { + status: { + required: true, + validator: function (value) { + return ( + value === null || + Array.isArray(value) && value.length > 0 + ) + } + } +} +``` + ## :wrench: Options Nothing. 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: ` From 56af9ddd7a44332563ad90ffdb1fba02b9b3a942 Mon Sep 17 00:00:00 2001 From: st-sloth Date: Sun, 15 Jul 2018 14:39:23 +0500 Subject: [PATCH 2/2] Fix: Allow props with `validator` without `type` in `vue/require-prop-types` (update docs according to PR discussion) --- docs/rules/require-prop-types.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/rules/require-prop-types.md b/docs/rules/require-prop-types.md index 8f91cb17d..4e54223d6 100644 --- a/docs/rules/require-prop-types.md +++ b/docs/rules/require-prop-types.md @@ -17,24 +17,24 @@ 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,