Vue: Improve prop validator API

Created on 29 Aug 2018  ·  4Comments  ·  Source: vuejs/vue

What problem does this feature solve?

  • Allows to write validation logic depending on multiple props.
  • Allows to provide custom validation messages.

What does the proposed API look like?

type Validator = (value: any, key: string, props: Object, warn: (message: string) => void) => boolean

Example:

export default {
  props: {
    foo: {
      validator(value, key, props, warn) {
        if ('bar' in props && 'foo' in props) warn('Foo and Bar cannot be used together.')
        return true
      }
....
feature request

Most helpful comment

I'm a fan of adding the prop as an argument. I think we can return a string from the validator function and let Vue treat that as an error.

My counter proposal is

type ValidatorOutput = boolean | string
type Validator = (value: any, key: string, props: Object) => ValidatorOutput

All 4 comments

Should not the warn function be returned to stop the rest of the validator code from executing?

I'm a fan of adding the prop as an argument. I think we can return a string from the validator function and let Vue treat that as an error.

My counter proposal is

type ValidatorOutput = boolean | string
type Validator = (value: any, key: string, props: Object) => ValidatorOutput

Closing in favor of #9467. Also, I think changing arguments of prop validator function would require an RFC now.

For a global workaround, see https://github.com/vuejs/vue/issues/9467#issuecomment-465708425

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cherry-geqi picture cherry-geqi  ·  35Comments

ecmel picture ecmel  ·  52Comments

karevn picture karevn  ·  42Comments

rpkilby picture rpkilby  ·  50Comments

alenyu picture alenyu  ·  43Comments