Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
deku-prop-types
Advanced tools
Prop type validation for Deku components
npm install --save deku-prop-types
Note: propType validations are not performed in production environments
function-component.jsx
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const Counter = ({props}) => <div>{props.count}</div>
Counter.propTypes = {
count: PropTypes.number.isRequired
}
export default validate(Counter)
object-component.jsx
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const Counter = {
propTypes: {
count: PropTypes.number.isRequired
},
render({props}) {
return <div>{props.count}</div>
}
}
export default validate(Counter)
import connect from 'deku-redux-connect'
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const Counter = ({props}) => <div>{props.count}</div>
Counter.propTypes = {
count: PropTypes.number.isRequired
}
const mapStateToProps = ({count}) => ({count})
export default connect(
mapStateToProps
)(validate(Counter))
Validate prop is of any type (not undefined)
Validate prop is an array
Validate prop is an array consisting of a type
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const NamesList = ({props}) => <div>
{props.names.map(name => <div>{name}</div>)}
</div>
NamesList.propTypes = {
names: PropTypes.arrayOf(PropTypes.string)
}
export default validate(NamesList)
Validate prop is a boolean
Validate prop is a function
Validate prop is an instance of a function or class
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const ItemList ({props}) => <div>
{props.list.map(item => <div>{item}</div>)}
</div>
ItemList.propTypes = {
list: PropTypes.instanceOf(Array)
}
export default validate(ItemList)
Validate prop is a number
Validate prop is an object
Validate prop has keys all matching an allowed type
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const NameCard = ({props}) => <div>{props.person.firstName + ' ' + props.person.lastName}</div>
NameCard.propTypes = {
person: PropTypes.objectOf(PropTypes.string)
}
export default validate(NameCard)
Validate prop is one of the allowed values
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const Color = ({props}) => <div>{props.color}</div>
Color.propTypes = {
color: PropTypes.oneOf(['red', 'green', 'blue'])
}
export default validate(Color)
Validate prop is one of the allowed types
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const Age = ({props}) => <div>{props.age}</div>
Age.propTypes = {
age: PropTypes.oneOfType([PropTypes.number, PropTypes.number])
}
export default validate(Age)
Validate an object's properties are of a certain type
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'
const ConfigDisplay = ({props}) => <div>{props.config.port + ' ' + props.config.host}</div>
ConfigDisplay.propTypes = {
config: PropTypes.shape({
host: PropTypes.string,
port: PropTypes.number
})
}
export default validate(ConfigDisplay)
Validate prop is a string
Note: all PropTypes can be required by specifying isRequired
like below:
PropTypes.number.isRequired
A function may be passed instead of a propType.
import {element} from 'deku'
import {validate} from 'deku-prop-types'
const Counter = ({props}) => <div>{props.count}</div>
Counter.propTypes = {
count(props, propName) {
if (props[propName] < 10) {
return new Error('count must be less than 10')
}
}
}
export default validate(Counter)
type: function
| object
Validate props passed to component match the specified type. An Error
is thrown if a prop is not valid.
type: number
default: 1
If a missing propType is discovered, validate will by default log a warning. This functionality may be configured.
0
- Do not warn or throw error
1
- Log a warning
2
- Throw an error
MIT © Dustin Specker
FAQs
Prop type validation for Deku components
We found that deku-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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.