Skip to content

Commit 23fa9d4

Browse files
st-slothmichalsnik
st-sloth
authored andcommitted
Fix: Allow props with validator without type in vue/require-prop-types (#476)
* Fix: Allow props with `validator` without `type` in `vue/require-prop-types` * Fix: Allow props with `validator` without `type` in `vue/require-prop-types` (update docs according to PR discussion)
1 parent 4b130cb commit 23fa9d4

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

docs/rules/require-prop-types.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,32 @@ props: ['status']
1717
:+1: Examples of **correct** code for this rule:
1818

1919
```js
20+
// Without options, just type reference
2021
props: {
2122
status: String
2223
}
2324
```
2425

2526
```js
27+
// With options with type field
2628
props: {
2729
status: {
2830
type: String,
2931
required: true,
30-
validate: function (value) {
31-
return ['syncing', 'synced', 'version-conflict', 'error'].indexOf(value) !== -1
32+
}
33+
}
34+
```
35+
36+
```js
37+
// With options without type field but with validator field
38+
props: {
39+
status: {
40+
required: true,
41+
validator: function (value) {
42+
return (
43+
value === null ||
44+
Array.isArray(value) && value.length > 0
45+
)
3246
}
3347
}
3448
}

lib/rules/require-prop-types.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ module.exports = {
3737
p.value.elements.length > 0
3838
)
3939
)
40-
return Boolean(typeProperty)
40+
const validatorProperty = node.properties
41+
.find(p => utils.getStaticPropertyName(p.key) === 'validator')
42+
return Boolean(typeProperty || validatorProperty)
4143
}
4244

4345
function checkProperties (items) {

tests/lib/rules/require-prop-types.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,32 @@ ruleTester.run('require-prop-types', rule, {
7070
`,
7171
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
7272
},
73+
{
74+
filename: 'test.vue',
75+
code: `
76+
export default {
77+
props: {
78+
foo: {
79+
validator: v => v
80+
}
81+
}
82+
}
83+
`,
84+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
85+
},
86+
{
87+
filename: 'test.vue',
88+
code: `
89+
export default {
90+
props: {
91+
foo: {
92+
['validator']: v => v
93+
}
94+
}
95+
}
96+
`,
97+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
98+
},
7399
{
74100
filename: 'test.vue',
75101
code: `

0 commit comments

Comments
 (0)