# prop-type-conditionals

> React proptype utility library

Latest version **0.0.11** (published 2019-05-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install prop-type-conditionals
pnpm add prop-type-conditionals
yarn add prop-type-conditionals
bun add prop-type-conditionals
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.11 |
| Published | 2019-05-31 |
| First published | 2018-03-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tony Dinh |
| Maintainers | tonydinh |
| Keywords | react, prop-types, proptypes, prop, utility, conditional, condition |

## Links

- npm: https://www.npmjs.com/package/prop-type-conditionals
- Repository: https://github.com/tony-dinh/prop-types
- Homepage: https://github.com/tony-dinh/prop-types#readme
- Issues: https://github.com/tony-dinh/prop-types/issues
- npm.io page: https://npm.io/package/prop-type-conditionals

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.11 (latest) — 2019-05-31
- 0.0.10 — 2019-05-29
- 0.0.9 — 2019-05-21
- 0.0.8 — 2019-05-21
- 0.0.7 — 2019-05-21
- 0.0.6 — 2018-05-24
- 0.0.5 — 2018-05-24
- 0.0.4 — 2018-04-24
- 0.0.3 — 2018-04-23
- 0.0.2 — 2018-03-09

## README

# PropType Conditionals

[![npm version](https://img.shields.io/npm/v/prop-type-conditionals.svg?)](https://www.npmjs.com/package/prop-type-conditionals)
[![npm downloads](https://img.shields.io/npm/dt/prop-type-conditionals.svg)](https://www.npmjs.com/package/prop-type-conditionals)
[![npm downloads](https://img.shields.io/npm/dm/prop-type-conditionals.svg)](https://www.npmjs.com/package/prop-type-conditionals)

A React PropType utility library that consists of various custom proptype validators.

## Installation
```
$ npm install --save prop-type-conditionals
```



## Usage
You may import the individual validators as needed or you can import the entire module:

```js
// Import individual validator
import { isValidIf } from 'prop-type-conditionals'

// Import entire module
import conditionals from 'prop-type-conditionals'
```
---

### Methods
1. [isAllowedIfNone](#isallowedifnone)
1. [isOneOfType](#isoneoftype)
1. [isRequiredIf](#isrequiredif)
1. [isValidIf](#isvalidif)
1. [isWholeNumber](#iswholenumber)

---

#### `isAllowedIfNone`
Returns a function that validates that the prop is not defined if any of the exclusive props are already defined. The `.isRequired` chained validator specifies that the prop is required if none of the exclusive props are defined.

**@param** {Array[String]} exclusivePropNames

**@param** {PropTypes.validator} validator

```js
import { isAllowedIfNone } from 'prop-type-conditionals'

Component.propTypes = {
    foo: PropTypes.string,
    bar: PropTypes.string,
    baz: isAllowedIfNone(['foo', 'bar'], PropTypes.string),
}
```

---

#### `isOneOfType`
Returns a function that validates that the prop's type matches one of the component constructors or element type specified. If this validator is used on the `children` prop, it validates that all child components pass validation.

**@param** {Array} allowedTypes

```js
import { isOneOfType } from 'prop-type-conditionals'
import Foo from 'components/foo'
import Bar from 'components/bar'

Component.propTypes = {
    children: isOneOfType([Foo, Bar])
}
```

---

#### `isRequiredIf`
Returns a function that validates that a prop is required if the condition function returns `true`.

**@param** {Function} condition

**@param** {Function} validator

```js
import { isRequiredIf } from 'prop-type-conditionals'

const condition = (props, propName) => true

Component.propTypes = {
    foo: isRequiredIf(condition, PropTypes.string)
}
```

---

#### `isValidIf`
Returns a function that validates that the prop passes the user defined condition.

**@param** {Function} condition

**@param** {PropTypes.validator} defaultValidator

```js
import { isValidIf } from 'prop-type-conditionals'

const condition = (props, propName) => true

Component.propTypes = {
    foo: isValidIf(condition, PropTypes.string)
}
```

---

#### `isWholeNumber`
Returns a function that validates that the prop is a whole number

```js
import { isWholeNumber } from 'prop-type-conditionals'

Component.propTypes = {
    foo: isWholeNumber()
}
```

---
_Source: https://npm.io/package/prop-type-conditionals · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
