React prop-types validation with ow
- when
process.env.NODE_ENVis'production'it will be a shim function - it exports the
owobject (or shim in'production') as a property - will return the unprocessed
ArgumentError
prop-types has only very basic validation, and it is super verbose to add custom validators.
import propType, { ow } from 'ow-prop-type'
class MyComponent extends React.Component {
static propTypes = {
// propType with a predicate
total: propType(
ow
.number
.integer
.greaterThanOrEqual(0)
),
// propType with a callback, must return a predicate
current: propType((props) => {
return ow
.number
.integer
.greaterThanOrEqual(0)
.lessThanOrEqual(props.total)
}
}
}