Skip to content

Fix: Allow props with validator without type in vue/require-prop-types #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions docs/rules/require-prop-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/require-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/require-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
Expand Down