Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
prop-types-extra
Advanced tools
The prop-types-extra npm package provides additional prop type validators that are not included in the core prop-types library. These validators help in defining more complex prop types for React components, making it easier to enforce stricter type checking and improve code quality.
all
The 'all' validator ensures that the prop satisfies all the provided validators. In this example, 'myProp' must be a string and one of the specified options ('option1' or 'option2').
const PropTypes = require('prop-types-extra');
MyComponent.propTypes = {
myProp: PropTypes.all([PropTypes.string, PropTypes.oneOf(['option1', 'option2'])])
};
elementType
The 'elementType' validator checks that the prop is a valid React component type. This can be useful for higher-order components or components that accept other components as props.
const PropTypes = require('prop-types-extra');
MyComponent.propTypes = {
myProp: PropTypes.elementType
};
deprecated
The 'deprecated' validator allows you to mark a prop as deprecated and provide a message. This is useful for maintaining backward compatibility while guiding developers towards newer APIs.
const PropTypes = require('prop-types-extra');
MyComponent.propTypes = {
myProp: PropTypes.deprecated(PropTypes.string, 'Use newProp instead.')
};
isRequiredForA11y
The 'isRequiredForA11y' validator ensures that the prop is required for accessibility purposes. This helps in enforcing accessibility best practices in your components.
const PropTypes = require('prop-types-extra');
MyComponent.propTypes = {
myProp: PropTypes.isRequiredForA11y(PropTypes.string)
};
The 'prop-types' package is the core library for type-checking props in React. It provides basic validators like string, number, array, object, etc. While 'prop-types' covers the most common use cases, 'prop-types-extra' extends its functionality with more specialized validators.
The 'airbnb-prop-types' package provides additional prop type validators inspired by Airbnb's internal practices. It includes validators for things like non-empty strings, unique arrays, and more. It offers similar extended functionality as 'prop-types-extra' but with a different set of validators.
Additional PropTypes for React.
import elementType from 'prop-types-extra/lib/elementType';
// or
import { elementType } from 'prop-types-extra';
const propTypes = {
someProp: elementType,
};
If you want to minimize bundle size, import only the validators you use via:
import elementType from 'prop-types-extra/lib/elementType'
$ npm i -S react
$ npm i -S prop-types-extra
all(...validators)
This validator checks that all of the provided validators pass.
const propTypes = {
vertical: PropTypes.bool.isRequired,
block: all(
PropTypes.bool.isRequired,
({ block, vertical }) => (
block && !vertical ?
new Error('`block` requires `vertical` to be set to have any effect') :
null
),
),
};
The provided validators will be validated in order, stopping on the first failure. The combined validator will succeed only if all provided validators succeed.
As in the example, this can be used to make a type assertion along with additional semantic assertions.
componentOrElement
Checks that the value is a ReactComponent
or a DOMElement
.
const propTypes = {
container: componentOrElement,
requiredContainer: componentOrElement.isRequired,
};
This ensures that the value is of the right type to pass to ReactDOM.findDOMNode()
, for cases where you need a DOM node.
deprecated(validator, reason)
This validator will log a deprecation warning if the value is present.
const propTypes = {
collapsable: deprecated(PropTypes.bool, 'Use `collapsible` instead.'),
};
If the collapsable
prop above is specified, this validator will log the warning:
The prop `collapsable` of `MyComponent` is deprecated. Use `collapsible` instead.
This validator warns instead of failing on invalid values, and will still call the underlying validator if the deprecated value is present.
This validator will only warn once on each deprecation. To clear the cache of warned messages, such as for clearing state between test cases intended to fail on deprecation warnings, call deprecated._resetWarned()
.
elementType
Checks that the value is a React element type. This can be either a string (for DOM elements) or a ReactClass
(for composite components).
const propTypes = {
Component: elementType.isRequired,
};
This ensures that the value of is the right type for creating a ReactElement
, such as with <Component {...props} />
.
isRequiredForA11y(validator)
This validator checks that the value required for accessibility are present.
const propTypes = {
id: isRequiredForA11y(PropTypes.string),
};
If the id
prop above is not specified, the validator will fail with:
The prop `id` is required to make `MyComponent` accessible for users of assistive technologies such as screen readers.
FAQs
React PropType Utilities
The npm package prop-types-extra receives a total of 986,675 weekly downloads. As such, prop-types-extra popularity was classified as popular.
We found that prop-types-extra 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.