Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chrislaughlin/prop-type-utils

A collection of utility functions for react prop types
https://github.com/chrislaughlin/prop-type-utils

javascript proptypes reactjs validation

Last synced: about 1 month ago
JSON representation

A collection of utility functions for react prop types

Awesome Lists containing this project

README

        

# Prop Type Utils

[![npm package][npm-badge]][npm]

Prop Typ Utils is a collection of _useful_ prop type validation rules.

PropType Utils Logo

Logo designed by [Liffy Designs](http://www.liffydesigns.co.uk/)

Install

```
npm i prop-type-utils
```

Use

You can import the named utils from the main package:
```javascript
import { includes, isGreaterThan } from 'prop-type-utils';

......

static propTypes = {
age: isGreaterThan(18),
title: includes('cool')
}
```

Or you can import the individual utils as you need them

```javascript
import isBetween from 'prop-type-utils/lib/isBetween';

......

static propTypes = {
score: isBetween(10, 60)
}
```

## Utils:
- [isEven](#iseven)
- [isGreaterThan](#isgreaterthan)
- [isLessThan](#islessthan)
- [isBetween](#isbetween)
- [includes](#includes)
- [isRequiredWhen](#isrequiredwhen)
- [isRequiredWhenAll](#isrequiredwhenAll)
- [isOnlyOneAllowed](#isOnlyOneAllowed)
- Have more? Raise a PR

### isEven

```javascript
import isEven from 'prop-type-utils/isEven';

Foo.propTypes = {
bar: isEven
}

//Examples

//Error

//Success
```

### isGreaterThan

```javascript
import isGreaterThan from 'prop-type-utils/isGreaterThan';

Foo.propTypes = {
bar: isGreaterThan(5)
}

//Examples

//Error

//Success
```

### isLessThan

```javascript
import isLessThan from 'prop-type-utils/isLessThan';

Foo.propTypes = {
bar: isLessThan(5)
}

//Examples

//Error

//Success
```

### isBetween

```javascript
import isBetween from 'prop-type-utils/isBetween';

Foo.propTypes = {
bar: isBetween(5, 10)
}

//Examples

//Error

//Success
```

### includes

```javascript
import includes from 'prop-type-utils/includes';

Foo.propTypes = {
bar: includes('react')
}

//Examples

//Error

//Success
```

### isRequiredWhen

```javascript
import includes from 'prop-type-utils/isRequiredWhen';

Foo.propTypes = {
isShowing: PropTypes.bool
bar: isRequiredWhen('isShowing')
}

//Examples

//Error

//Success

```

### isRequiredWhenAll

```javascript
import isRequiredWhenAll from 'prop-type-utils/isRequiredWhenAll';

Foo.propTypes = {
isShowing: PropTypes.bool,
foo: PropTypes.string,
bar: isRequiredWhenAll(['isShowing', 'foo'])
}

//Examples

//Error

//Error

//Success

//Success

```

### isOnlyOneAllowed

```javascript
import isOnlyOneAllowed from 'prop-type-utils/isOnlyOneAllowed';

Foo.propTypes = {
isShowing: isOnlyOneAllowed(['isShowing','foo', 'bar']),
foo: isOnlyOneAllowed(['isShowing','foo', 'bar']),
bar: isOnlyOneAllowed(['isShowing','foo', 'bar'])
}

//Examples

//Error

//Error

//Success

//Success

```

[build-badge]: https://img.shields.io/travis/user/repo/master.png?style=flat-square
[build]: https://travis-ci.org/user/repo

[npm-badge]: https://img.shields.io/npm/v/npm-package.png?style=flat-square
[npm]: https://www.npmjs.org/package/npm-package