prop-types icon indicating copy to clipboard operation
prop-types copied to clipboard

Export createChainableTypeChecker?

Open d07RiV opened this issue 8 years ago • 3 comments

Is there any reason why this function isn't exported (other than the ReactPropTypesSecret bit)? Every library or project that implements custom type checkers needs to write their own copy of it.

d07RiV avatar Dec 20 '17 13:12 d07RiV

fwiw, airbnb-prop-types doesn't; we use a different, more explicit/less metaprogrammy approach.

ljharb avatar Dec 20 '17 19:12 ljharb

I'm envisioning writing stuff like ofAtLeastSize that checks array length, and withProps that checks that props exist on an element, so I could write something like:

static propTypes = {
  children: PropTypes
    .array
    .ofAtLeastSize(1)
    .arrayOf(withProps({ name: PropTypes.string.isRequired }))
    .isRequired,
}

I'm aware I could write all of the above into one gross custom validator, but the idea of composable chainable validation calls to me with a very pretty voice.

Am I trying to get too clever with prop-types?

Asday avatar May 11 '19 17:05 Asday

@Asday airbnb-prop-types already has withShape that allows for it, eg, withShape(PropTypes.array, { length: range(1, Infinity) }).

ljharb avatar May 11 '19 18:05 ljharb