Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prop-type-conditionals

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prop-type-conditionals

React proptype utility library

  • 0.0.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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()
}

Keywords

FAQs

Package last updated on 31 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc