Newton's Preposterous Miscalculation
Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

prop-type-conditionals

0.0.11 • Public • Published

PropType Conditionals

npm version npm downloads npm downloads

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:

// Import individual validator
import { isValidIf } from 'prop-type-conditionals'
 
// Import entire module
import conditionals from 'prop-type-conditionals'

Methods

  1. isAllowedIfNone
  2. isOneOfType
  3. isRequiredIf
  4. isValidIf
  5. 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

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

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

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

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

import { isWholeNumber } from 'prop-type-conditionals'
 
Component.propTypes = {
    foo: isWholeNumber()
}

Install

npm i prop-type-conditionals

DownloadsWeekly Downloads

19

Version

0.0.11

License

ISC

Unpacked Size

10.7 kB

Total Files

4

Last publish

Collaborators

  • avatar