Custom validator function that checks if object properties exist in Vue.js

Sometimes when passing an object as a prop to a Vue component it's important to verify that it contains all expected properties. In Vue.js this can be accomplished by writing a custom validator function.

What object properties are expected? Here's an example of a valid object:

{
  id: 1,
  name: 'Hey!'
}

Validation:

props: {
  myProp: {
    type: Object,
    required: true,
    validator: object => {
      const missingProperties = [
        'id',
        'name'
      ].filter(property => !object.hasOwnProperty(property))

      return missingProperties.length === 0
    }
  }
}

Similarly it's sometimes useful to validate an array of objects and confirm that every object in the array has expected properties:

props: {
  myProp: {
    type: Array,
    required: true,
    validator: items => {
      const validItems = items.filter(item => {
        return item.hasOwnProperty('id') && item.hasOwnProperty('name')
      })

      return validItems.length === items.length
    }
  }
}