dzm
dzm

Reputation: 23534

React PropTypes not showing is required

I have these PropTypes setup:

Select.propTypes = {
  onSelect: PropTypes.func.isRequired,
  data: PropTypes.arrayOf(PropTypes.shape({
     key: PropTypes.string.isRequired,
     value: PropTypes.string.isRequired
   }).isRequired).isRequired
}

onSelect and data both will show errors if not defined. However, the shape of the object in the array for data, is not validating. I can use data={[]} and it doesn't error.

Is there something I'm missing here? Thank you

Upvotes: 2

Views: 586

Answers (1)

Tharaka Wijebandara
Tharaka Wijebandara

Reputation: 8065

I think this is expected. When a prop is a required array you can pass either an empty array or an array with elements of given type. Not only for a shape, even for PropTypes.arrayOf(PropTypes.number).isRequired it will consider both [] and [9] as valid but not ["9"]. It doesn' make a change even if you use PropTypes.arrayOf(PropTypes.number.isRequired).isRequired.

Basically, you can't specify a minimum number of elements should be in an array or restrict assigning an empty array with prop types.

Upvotes: 1

Related Questions