Validator functions for Vue prop and event should have return values

  • VUE_MISSING_RETURN_VALUE_IN_VALIDATOR
  • Error
  • Medium
  • vue

This rule applies when a validator function for Vue prop or event does not return a value.

One can check the prop value or event payload by specifying a validator function. For a validation to be successful, a truthy value should be returned from the validator.

However, validation always fails if the validator returns no value, which is likely to be a programmer's mistake.

Noncompliant Code Example

View with compliant examples side by side
<script>
export default {
  props: {
    name: { // VUE_MISSING_RETURN_VALUE_IN_VALIDATOR alarm because 'return' is missing in the validator.
      validator: value => {
        if (!value) {
          console.warn('Invalid name');
        }
      }
    }
  },
  emits: {
    load: payload => { // VUE_MISSING_RETURN_VALUE_IN_VALIDATOR alarm because 'return' is missing in the validator.
      if (!payload) {
        throw new Error('Invalid load event');
      }
    }
  },
  mounted() {
    console.log(this.name);
    this.$emit('load', 'success');
  }
}
</script>

Compliant Code Example

View with noncompliant examples side by side
<script>
export default {
  props: {
    name: {
      validator: value => {
        if (!value) {
          console.warn('Invalid name');
          return false;
        }
        return true;
      }
    }
  },
  emits: {
    load: payload => {
      if (!payload) {
        throw new Error('Invalid load event');
      }
      return true;
    }
  },
  mounted() {
    console.log(this.name);
    this.$emit('load', 'success');
  }
}
</script>

Version

This rule was introduced in DeepScan 1.42.0.

See

  • props

  • emits

  • [Vue warn]: Invalid prop: custom validator check failed for prop "name".

  • [Vue warn]: Invalid event arguments: event validation failed for event "load".

Was this documentation helpful?