Code Monkey home page Code Monkey logo

prop-types-plus's Introduction

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 to production 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 matches 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`."
  )
};

prop-types-plus's People

Contributors

arkellys avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.