Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  12] [ 6]  / answers: 1 / hits: 62611  / 5 Years ago, sun, december 1, 2019, 12:00:00

Even though VueJS 2 official documentation about prop validation is stating (as a code example's comment line):



// Basic type check (null and undefined values will pass any type
validation)



I'm getting the following error with this code reproduction — why is that?


[Vue warn]: Invalid prop: type check failed for prop "value". Expected String, Number, Boolean, got Null 

<template>
<div>
<h1>{{ title }}:</h1>
<MyInput :value="null" />
</div>
</template>

<script>
Vue.component('MyInput', Vue.extend({
props: {
value: {
type: [String, Number, Boolean],
required: true,
},
},
template: `
<select v-model="value">
<option value="null">
null value
</option>
<option value="">
Empty value
</option>
</select>`,
}));

export default {
data: () => ({
title: 'VueJS Using Prop Type Validation With NULL and `undefined` Values?'
}),
};
</script>

More From » validation

 Answers
15

// Basic type check (null and undefined values will pass any type validation)




This applies only when required: true is NOT set. In your code, you are saying that the prop is required and so Vuejs is showing the warning



Related discussion: https://forum.vuejs.org/t/shouldnt-null-be-accepted-as-prop-value-with-any-type/63887


[#51417] Saturday, November 23, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;