Another library of custom PropType validators. It is greatly inspired by airbnb/prop-types
, which probably already offers the same custom types and more. I just wanted to create my own. ๐
yarn add prop-types-plus
Note
Since PropTypes are only useful for developement, when NODE_ENV
is set to production
mock functions are exported instead of the real validators.
Validates the type of the entries in an array in the given order.
arrayOfShape(shape: PropTypes[])
import { string, number } from "prop-type";
import { arrayOfShape } from "prop-types-plus";
const propTypes = {
foo: arrayOfShape([string, number.isRequired]),
fooRequired: arrayOfShape([string, number.isRequired]).isRequired
};
Tests whether a prop is required based on a given condition.
requiredIf(
type: PropTypes,
condition: (props: object) => boolean,
errorMessage?: string
)
import { string } from "prop-type";
import { requiredIf } from "prop-types-plus";
const propTypes = {
foo: requiredIf(
string,
(props) => props.bar
"The prop `foo` is required when there is `bar`."
)
};
Tests whether a string matches the given regex.
stringOfShape(regex: RegExp)
import { stringOfShape } from "prop-types-plus";
const propTypes = {
foo: stringOfShape(/^#.+/),
fooRequired: stringOfShape(/^#.+/).isRequired
};
Tests whether a prop is unnecessary based on a given condition.
unnecessaryWhen(
type: PropTypes,
condition: (props: object) => boolean,
errorMessage?: string
)
import { string } from "prop-type";
import { unnecessaryWhen } from "prop-types-plus";
const propTypes = {
foo: requiredIf(
string,
(props) => props.bar
"The prop `foo` is unnecessary when there is `bar`."
)
};