question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule Proposal: `vue/require-valid-default-prop`

See original GitHub issue

Please describe what the rule should do:

Enforces prop default values to be valid. In vue we have 2 ways to define default prop value:

  • by function
    {
      foo: {
        default () {}
      }
    }
    
  • by literal value
    {
      foo: {
        default: ''
      }
    }
    

Literal value is not is not valid when type is specified and set to Array or Object

This rule should also check type of literal values and determine if types match. When variable is passed as default we should omit checking

What category of rule is this? (place an “X” next to just one item)

[ ] Enforces code style [x] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Valid:

Vue.component('example', {
  props: {
    // basic type check (`null` means accept any type)
    propA: Number,
    // multiple possible types
    propB: [String, Number],
    // a number with default value
    propD: {
      type: Number,
      default: 100
    },
    // object/array defaults should be returned from a factory function
    propE: {
      type: Object,
      default: function () {
        return { message: 'hello' }
      }
    }
  }
})

Invalid:

Vue.component('example', {
  props: {
    propA: {
      type: String,
      default: {}
    },
    propB: {
      type: String,
      default: []
    },
    propC: {
      type: Object,
      default: []
    },
    propD: {
      type: Array,
      default: []
    },
    propE: {
      type: Object,
      default: { message: 'hello' }
    }
  }
})

see more at: https://vuejs.org/v2/guide/components.html#Prop-Validation

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
armano2commented, Aug 1, 2017

@michalsnik we should distinct both of rules, require-default-prop should be optional, and its should require to specify default value if "required" is not set or set to false.

require-valid-default-prop should be enabled by default (in next release), its catching errors / issues.

1reaction
lcspringcommented, Jun 19, 2019

good

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue/require-valid-default-prop
This rule checks whether the default value of each prop is valid for the given type. It should report an error when default...
Read more >
vue/require-valid-default-prop | eslint-plugin-vue - GitHub Pages
This rule checks whether the default value of each prop is valid for the given type. It should report an error when default...
Read more >
eslint-plugin-vue - npm
Start using eslint-plugin-vue in your project by running `npm i ... vue/require-valid-default-prop, enforce props default values to be valid.
Read more >
vue props type null - You.com | The Search Engine You Control
In your code, you are saying that the prop is required and so Vuejs is showing the ... child.vue <script> export default {...
Read more >
Validation Provider - VeeValidate
<template> <ValidationProvider rules="required" v-slot="{ errors }"> <input ... The default tag can be changed using the provider's tag prop.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found