hidar
hidar

Reputation: 5939

how to check if an object prop has the required object properties

I am passing an object to my component as:

<foo :ob='object'></foo>

And inside my component, I have this declaration:

props: {
  ob: {
    type: Object,
    required: false, 
    default: {}
  }
}

Usually, the expected object to pass is something like this:

{

  bar: [],
  tar: {
    a: false,
  }
}

Now, how do I make sure in props that the object passed is similar. I don't need to check the values, I just need to know it contains the object keys ,bar, tar and on property inside tar, which is: a

Upvotes: 3

Views: 1896

Answers (1)

Alex Michailidis
Alex Michailidis

Reputation: 4143

You can create a custom validator

props: {
  ob: {
    type: Object,
    required: false, 
    validator: function (obj) {
      return 'bar' in obj &&
      'tar' in obj &&
      obj.tar instanceof Object &&
      'a' in obj.tar
    }
  }
}

Upvotes: 6

Related Questions