What is the cause of [Vue warn]: Invalid prop: custom validator check failed for prop "value"

Andrey Khataev picture Andrey Khataev · May 15, 2019 · Viewed 17.5k times · Source

I have nuxt.js app, which uses vuejs-datepicker:

<template>
<!-- ... -->

    <DatePicker :value="datePicker.value" />
 <!-- ... -->
 </template>

and bunch of date variables:

<script>
import DatePicker from 'vuejs-datepicker'
import { DateTime } from 'luxon'

const moment = require('moment')

const date = new Date(2016, 8, 16)
const date2 = moment('2016-09-16').toDate()
const date3 = DateTime.local(2016, 9, 16).toJSDate()

export default {
  components: {
    DatePicker
  },
  data() {
    return {
      datePicker: {
        mondayFirst: true,
        format: 'dd.MM.yyyy',
        value: date
      }
    }
  }
}

When I bind its 'value' property to usual Date variable 'date', everything is ok, but when I choose date2 or date3, I get this annoying warning

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

found in

---> <DatePicker>
       <StaticPositionsTab> at components/StaticPositions.vue
         <BTab>
           <BTabs>
             <Pages/index.vue> at pages/index.vue
               <Nuxt>
                 <Layouts/default.vue> at layouts/default.vue
                   <Root>

I found custom validator for value property and it is very straightforward and simple and return true in all three cases:

value: {
      validator: function (val) { return utils$1.validateDateInput(val); }
    }

...

validateDateInput (val) {
    return val === null || val instanceof Date || typeof val === 'string' || typeof val === 'number'
  }

but what makes the difference then? Could it be Vue.js bug itself?

Answer

ittus picture ittus · May 16, 2019

date2 and date3 are object, while value requires null | Date | string | number

const date2 = moment('2016-09-16').toDate()
const date3 = DateTime.local(2016, 9, 16).toJSDate()

typeof date2 // object
typeof date3 // object

You should use .toString() to convert them to string

const date2 = moment('2016-09-16').toString()
const date2 = moment('2016-09-16').toISOString()
const date3 = DateTime.local(2016, 9, 16).toString()

or using unix timestamp

const date2 = moment('2016-09-16').unix()
const date3 = moment('2016-09-16').valueOf()
const date4 = moment('2016-09-16').getTime()
const date5 = DateTime.local(2016, 9, 16).valueOf()