Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PropType for an exact shape plus "...rest" properties #405

Open
effulgentsia opened this issue Nov 16, 2023 · 1 comment
Open

Add a PropType for an exact shape plus "...rest" properties #405

effulgentsia opened this issue Nov 16, 2023 · 1 comment
Labels
new validator request Requests for a new kind of validator. question

Comments

@effulgentsia
Copy link

effulgentsia commented Nov 16, 2023

Suppose I want something like this:

PropTypes.exact({
  x: PropTypes.boolean,
  y: PropTypes.string.isRequired,
  "...rest": PropTypes.objectOf(PropTypes.string)
})

The above won't work because I don't want to apply that last validator to a property named ...rest, I want to apply it to the rest object that you would get when you destructure the remaining properties.

Is this possible to do with existing prop types, or would it require a new prop type, such as:

PropTypes.exactPlusRest({
  x: PropTypes.boolean,
  y: PropTypes.string.isRequired
}, PropTypes.string)
@ljharb
Copy link
Collaborator

ljharb commented Dec 1, 2023

If not for x, you could combine y and objectOf and it’d work fine.

I suspect you can use the combinators in npmjs.com/airbnb-prop-types to do what you want, but it’d be a bit cumbersome. I’ll have to think on this one.

@ljharb ljharb added question new validator request Requests for a new kind of validator. labels Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new validator request Requests for a new kind of validator. question
Projects
None yet
Development

No branches or pull requests

2 participants