Skip to content

Multi-type prop validation is not consistent #3879

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

Closed
aphofstede opened this issue Oct 8, 2016 · 6 comments · Fixed by #3885
Closed

Multi-type prop validation is not consistent #3879

aphofstede opened this issue Oct 8, 2016 · 6 comments · Fixed by #3885

Comments

@aphofstede
Copy link

aphofstede commented Oct 8, 2016

Vue.js version

2.0.1

Reproduction Link

https://jsfiddle.net/alexman/f7sb0rd3/9/

Steps to reproduce

Create a component with a prop of type [String, Boolean] and instantiate it with a boolean value or shorthand prop notation. (<my-component my-prop></my-component>)

What is Expected?

Booleans are accepted and passed to the validator as a boolean value.

What is actually happening?

You'll see that it will not validate booleans / shorthand prop notation.
Defining it reversed, [Boolean, String], does seem to work as expected except for not being able to pass false as a value.

Observations

I noticed validateProp() does the following "magic", which doesn't seem to take the option of an array of types into account?

  if (getType(prop.type) === 'Boolean') {
    if (absent && !hasOwn(prop, 'default')) {
      value = false
    } else if (value === '' || value === hyphenate(key)) {
      value = true
    }
  }
@defcc
Copy link
Member

defcc commented Oct 9, 2016

getType func seems not to deal with multi-type props, it just returns the first type

I will try to make a pr

@posva
Copy link
Member

posva commented Oct 9, 2016

I'm not sure to understand the issue. Were you expecting the value to be cast as a Boolean? Although you said it was either a String or a Boolean?. Why would using the order to specify what the prop should be cast to be non-consistent?

@aphofstede
Copy link
Author

aphofstede commented Oct 9, 2016

Not consistent: Because the behaviour differs between type: [Boolean, String] and type: [String, Boolean].
About expectations: Using just the type: Boolean does some magic to convert the eventual value to a boolean type. With multiple allowed types, when the caller passes a "boolean'able" value, I would expect value to be a boolean type also (which is then passed to the custom validator). If it cannot be resolved to a boolean but conforms to another type in the array of allowed types (and after it passes custom validation) -> also valid. Otherwise -> invalid.

@posva
Copy link
Member

posva commented Oct 9, 2016

What do you mean by magic? Do you refer when there's no value?
I'd say it's normal to differ because types are differents (order in array matters)

@aphofstede
Copy link
Author

I referred to the piece of code mentioned in "Observations" above.
Why does the order matter? I interpret it as "any of X, Y, Z"

@posva
Copy link
Member

posva commented Oct 10, 2016

I'll share the rest of the team.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants