check-prop-types
Advanced tools
Comparing version 1.0.0 to 1.1.0
13
index.js
@@ -60,2 +60,15 @@ /** | ||
/** | ||
* Same as checkPropTypes but throws on error | ||
*/ | ||
function assertPropTypes() { | ||
if (process.env.NODE_ENV !== 'production') { | ||
var error = checkPropTypes.apply(null, arguments); | ||
if (error) { | ||
throw new Error(error); | ||
} | ||
} | ||
} | ||
module.exports = checkPropTypes; | ||
module.exports.assertPropTypes = assertPropTypes; |
{ | ||
"name": "check-prop-types", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Check PropTypes, returning errors instead of logging them", | ||
@@ -16,2 +16,6 @@ "main": "index.js", | ||
"author": "uniphil", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:ratehub/check-prop-types.git" | ||
}, | ||
"license": "MIT", | ||
@@ -18,0 +22,0 @@ "devDependencies": { |
@@ -38,2 +38,25 @@ # checkPropTypes | ||
### assertPropTypes | ||
To throw errors instead of returning them, a helper called `assertPropTypes` is included: | ||
```js | ||
import PropTypes from 'prop-types'; | ||
import { assertPropTypes } from 'check-prop-types'; | ||
const HelloComponent = ({ name }) => ( | ||
<h1>Hi, {name}</h1> | ||
); | ||
HelloComponent.propTypes = { | ||
name: PropTypes.string.isRequired, | ||
}; | ||
assertPropTypes(HelloComponent.propTypes, { name: 'Julia' }, 'prop', HelloComponent.name); | ||
// fine... | ||
assertPropTypes(HelloComponent.propTypes, { name: 123 }, 'prop', HelloComponent.name); | ||
// throws Error: Failed prop type: Invalid prop `name` of type `number` supplied to `HelloComponent`, expected `string`. | ||
``` | ||
See [test.js](./test.js) for more usage examples. |
13
test.js
@@ -42,1 +42,14 @@ const test = require('tape'); | ||
}); | ||
test('assertPropTypes throws instead of returning error', function(assert) { | ||
var assertPropTypes = checkPropTypes.assertPropTypes; | ||
assert.plan(2); | ||
assert.doesNotThrow(function() { | ||
assertPropTypes({x: PropTypes.number}, {x: 1}, 'prop', 'c'); | ||
}); | ||
assert.throws(function() { | ||
assertPropTypes({x: PropTypes.number}, {x: ''}, 'prop', 'c'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8971
115
62