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

feat: introduce forbidUnknowProps #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ yarn add @dhis2/prop-types
<dd><p>Ensure the prop value is an array with a length between a minimum and maximum.
If a third <code>propType</code> argument is passed each item in the array needs to be of that prop-type</p>
</dd>
<dt><a href="#forbidUnknowProps">forbidUnknowProps(propTypes)</a> ⇒ <code>Object</code></dt>
<dd><p>Ensure that no other props, apart from the ones in the prop-types definition
have been passed tot the component</p>
</dd>
<dt><a href="#instanceOfComponent">instanceOfComponent(Component)</a> ⇒ <code>Error</code> | <code>null</code></dt>
<dd><p>Ensure the prop value is an instance of a certain component</p>
</dd>
Expand Down Expand Up @@ -54,6 +58,36 @@ LotsOfLists.propTypes = {
mandatoryArrayBetweenOneAndTen: arrayWithLength(1,10).isRequired,
}
```
<a name="forbidUnknowProps"></a>

## forbidUnknowProps(propTypes) ⇒ <code>Object</code>
Ensure that no other props, apart from the ones in the prop-types definition
have been passed tot the component

**Kind**: global function
**Returns**: <code>Object</code> - Returns a copy of the original prop-types object with an appended
property `forbidUnknowProps`, which is a prop-types validator that will return an error
if props have been added that the component that were not in the prop-types object

| Param | Type | Description |
| --- | --- | --- |
| propTypes | <code>object</code> | The prop-types definition of the component |

**Example**
```js
import React from 'react'
import propTypes from '@dhis2/prop-types'

const MyComponent = ({ score, maxScore }) => <div>{score} out of {maxScore}</div>
MyComponent.propTypes = propTypes.forbidUnknowProps({
score: propTypes.number,
maxScore: propTypes.number,
})

Note: `forbidUnknowProps` is different from the `exact` method form the standard
prop-types library, because `forbidUnknowProps` is meant to be applied on a full component
prop-type definition, while `exact` is meant to be a applied on an individual prop-type
```
<a name="instanceOfComponent"></a>

## instanceOfComponent(Component) ⇒ <code>Error</code> \| <code>null</code>
Expand Down
37 changes: 37 additions & 0 deletions src/forbidUnknownProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Ensure that no other props, apart from the ones in the prop-types definition
* have been passed tot the component
* @param {object} propTypes - The prop-types definition of the component
* @return {Object} Returns a copy of the original prop-types object with an appended
* property `forbidUnknowProps`, which is a prop-types validator that will return an error
* if props have been added that the component that were not in the prop-types object
* @example
* import React from 'react'
* import propTypes from '@dhis2/prop-types'
*
* const MyComponent = ({ score, maxScore }) => <div>{score} out of {maxScore}</div>
* MyComponent.propTypes = propTypes.forbidUnknowProps({
* score: propTypes.number,
* maxScore: propTypes.number,
* })
*
* Note: `forbidUnknowProps` is different from the `exact` method form the standard
* prop-types library, because `forbidUnknowProps` is meant to be applied on a full component
* prop-type definition, while `exact` is meant to be a applied on an individual prop-type
*/

const forbidUnknowProps = propTypes => ({
...propTypes,
forbidUnknowProps: (props, _, componentName) => {
const unknownProps = Object.keys(props).filter(
propKey => !propTypes[propKey]
)
if (unknownProps.length > 0) {
const list = unknownProps.join(', ')
return new Error(`${componentName}: unknown props found: ${list}`)
}
return null
},
})

export { forbidUnknowProps }
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import propTypes from 'prop-types'

import { arrayWithLength } from './arrayWithLength.js'
import { forbidUnknowProps } from './forbidUnknowProps.js'
import { instanceOfComponent } from './instanceOfComponent.js'
import { mutuallyExclusive } from './mutuallyExclusive.js'

const customPropTypes = {
arrayWithLength,
forbidUnknowProps,
instanceOfComponent,
mutuallyExclusive,
}

export { arrayWithLength, instanceOfComponent, mutuallyExclusive }
export {
arrayWithLength,
forbidUnknowProps,
instanceOfComponent,
mutuallyExclusive,
}

export default {
...propTypes,
Expand Down