Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@cody-greene/prop-types
Advanced tools
The lovable React.PropTypes interface, modified for general use
The loveable React.PropTypes interface, modified for general use. The Error messages generated
// node or browser
const PropTypes = require('prop-types')
Simple: bool
, string
, number
, array
, object
, func
Complex:
oneOf(['enum1', 'enum2')
: Value must strictly equal (===
) one of these enumerated values. Adding null
or undefined
to the list will make the value optional.
oneOfType([PropTypes.*, ...])
: At least one validator must succeed
objectOf(PropTypes.*)
: Every value in the obect should be the same type
arrayOf(PropTypes.*)
: Every element in the array should be the same type
shape({...})
: An object taking on a particular shape. If all props are optional, then the object itself is optional. The returned validator, unlike any other builtin, only requires two arguments validate(val, displayName)
.
bool
,oneOf
,shape
: do not directly support.isRequired
const validate = PropTypes.shape({
foo: PropTypes.number.isRequired,
bar: PropTypes.arrayOf(PropTypes.string),
baz: PropTypes.oneOf(['green', 'red', 'blue'])
})
let err = validate({
foo: 29,
bar: ['x', 'y', 7],
baz: 'green'
}, 'MyDisplayName')
console.log(err.toString())
// TypeError: MyDisplayName: ".baz[2]" is number, expected string
Remeber, these type definitions are all just functions so you can write your own too! Use PropTypes.optional(fn)
to wrap your base fn with a null/undefined check and expose the original fn as .isRequired
/**
* @param {any} val
* @param {string} displayName
* @param {string} key Optional with PropTypes.shape validator
* @param {object} props Optional with PropTypes.shape validator
* @return {Error|null}
* @example customChecker(val, displayName, key, props)
* note: Checkers should have very shallow call-stacks
* Avoid using things like Array.forEach() here
*/
const myLuckyNumber = PropTypes.optional(function (val, displayName, key, props) {
if (val !== 7) return new TypeError(`${displayName}: ${key} is not 7`)
})
const validate = PropTypes.shape({
foo: myLuckyNumber.isRequired,
bar: PropTypes.arrayOf(myLuckyNumber)
})
Add parameter validation to job handlers for something like [node-resque][] or [arquebus][] where JSON.parse() is used to generate input. This pattern could also apply to querystring parameters when writing a REST API, or untrusted input when building a dynamic SQL statement.
/**
* Define a background job similar to how React Components are defined
* @param {string} displayName
* @param {object} propTypes
* @param {function} perform(props, done)
* @return {function}
*/
function createJob(opt) {
const validate = PropTypes.shape(opt.propTypes)
return function validateAndRun(props, done) {
let err = validate(props, opt.displayName, 'props')
if (err) done(err)
else opt.perform(props, done)
}
}
const calsync = createJob({
displayName: 'CalendarSync',
propTypes: {
uid: PropTypes.string.isRequired,
fromDate: PropTypes.number.isRequired,
private: PropTypes.bool
},
perform(props, done) {
console.log(props)
done()
}
}
// Register the job handler with node-resque or arquebus
// Or just run it directly for the sake of a short example
calsync({
uid: '010101010101',
private: true
}, function (err) {
if (err) console.log(err.stack)
else console.log('done!')
})
// TypeError: CalendarSync "props.fromDate" is required
FAQs
The lovable React.PropTypes interface, modified for general use
We found that @cody-greene/prop-types demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.