prop-types-plus
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. 🙃
Installation
yarn add prop-types-plus
Validators
[!NOTE] Since PropTypes are only useful for developement, when
NODE_ENV
is set toproduction
mock functions are exported instead of the real validators.
arrayOfShape
Validates the type of the entries in an array in the given order.
Signature
arrayOfShape(shape: PropTypes[])
Usage
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
};
requiredIf
Tests whether a prop is required based on a given condition.
Signature
requiredIf(
type: PropTypes,
condition: (props: object) => boolean,
errorMessage?: string
)
Usage
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`."
)
};
stringOfShape
Tests whether a string match the given regex.
Signature
stringOfShape(regex: RegExp)
Usage
import { stringOfShape } from "prop-types-plus";
const propTypes = {
foo: stringOfShape(/^#.+/),
fooRequired: stringOfShape(/^#.+/).isRequired
};
unnecessaryWhen
Tests whether a prop is unnecessary based on a given condition.
Signature
unnecessaryWhen(
type: PropTypes,
condition: (props: object) => boolean,
errorMessage?: string
)
Usage
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`."
)
};