ljharb / prop-types-tools

Custom React PropType validators
MIT License
671 stars 50 forks source link

Children of children / recursive validation #50

Open SelaO opened 5 years ago

SelaO commented 5 years ago

I want to validate the children of an element that's passed as children.

For example:

<x><y/></x>

Where y is wrapped with injectIntl and, so I want to check in x that y is either of type "A" or type "B", but because it's wrapped, I need to go one level deep.

Is there a way to do this with these proptypes?

childrenOfType doesn't help because it can only check if it's of type injectIntl.

ljharb commented 5 years ago

I'm not sure what you're asking - can you provide the actual component code?

SelaO commented 5 years ago

Foo:

class Foo extends Component {

};
...
export default injectIntl(Foo);

Bar:

export default class Bar extends Component {

  render() {
    const Foo = this.props.foo;
    return <Foo />;
  }
}

FormDropZoneWrapper.propTypes = {
  dropZoneComponent: PropTypes.func.isRequired, // I want here to validate that Foo is actually Foo and not some other component, but it's wrapped by intl so I can't
};

See the comment.

ljharb commented 5 years ago

In that case, you'd use PropTypes.oneOf([Foo]) - iow, nobody can ever get at the unwrapped Foo, only the wrapped one, so you only have to validate that it's the wrapped one.