npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deku-prop-types

v0.4.1

Published

Prop type validation for Deku components

Downloads

11

Readme

deku-prop-types

NPM version Build Status Coverage Status

Code Climate Dependencies DevDependencies

Prop type validation for Deku components

Install

npm install --save deku-prop-types

Notes

Usage

Note: propType validations are not performed in production environments

function-component.jsx

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const Counter = ({props}) => <div>{props.count}</div>
Counter.propTypes = {
  count: PropTypes.number.isRequired
}

export default validate(Counter)

object-component.jsx

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const Counter = {
  propTypes: {
    count: PropTypes.number.isRequired
  },
  render({props}) {
    return <div>{props.count}</div>
  }
}

export default validate(Counter)

Usage with deku-redux-connect

import connect from 'deku-redux-connect'
import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const Counter = ({props}) => <div>{props.count}</div>
Counter.propTypes = {
  count: PropTypes.number.isRequired
}

const mapStateToProps = ({count}) => ({count})

export default connect(
  mapStateToProps
)(validate(Counter))

Supported types

PropTypes.any

Validate prop is of any type (not undefined)

PropTypes.array

Validate prop is an array

PropTypes.arrayOf

Validate prop is an array consisting of a type

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const NamesList = ({props}) => <div>
  {props.names.map(name => <div>{name}</div>)}
</div>

NamesList.propTypes = {
  names: PropTypes.arrayOf(PropTypes.string)
}

export default validate(NamesList)

PropTypes.bool

Validate prop is a boolean

PropTypes.func

Validate prop is a function

PropTypes.instanceOf

Validate prop is an instance of a function or class

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const ItemList ({props}) => <div>
  {props.list.map(item => <div>{item}</div>)}
</div>

ItemList.propTypes = {
  list: PropTypes.instanceOf(Array)
}

export default validate(ItemList)

PropTypes.number

Validate prop is a number

PropTypes.object

Validate prop is an object

PropTypes.objectOf

Validate prop has keys all matching an allowed type

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const NameCard = ({props}) => <div>{props.person.firstName + ' ' + props.person.lastName}</div>

NameCard.propTypes = {
  person: PropTypes.objectOf(PropTypes.string)
}

export default validate(NameCard)

PropTypes.oneOf

Validate prop is one of the allowed values

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const Color = ({props}) => <div>{props.color}</div>

Color.propTypes = {
  color: PropTypes.oneOf(['red', 'green', 'blue'])
}

export default validate(Color)

PropTypes.oneOfType

Validate prop is one of the allowed types

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const Age = ({props}) => <div>{props.age}</div>

Age.propTypes = {
  age: PropTypes.oneOfType([PropTypes.number, PropTypes.number])
}

export default validate(Age)

PropTypes.shape

Validate an object's properties are of a certain type

import {element} from 'deku'
import {PropTypes, validate} from 'deku-prop-types'

const ConfigDisplay = ({props}) => <div>{props.config.port + ' ' + props.config.host}</div>

ConfigDisplay.propTypes = {
  config: PropTypes.shape({
    host: PropTypes.string,
    port: PropTypes.number
  })
}

export default validate(ConfigDisplay)

PropTypes.string

Validate prop is a string

Note: all PropTypes can be required by specifying isRequired like below:

PropTypes.number.isRequired

Custom Validators

A function may be passed instead of a propType.

import {element} from 'deku'
import {validate} from 'deku-prop-types'

const Counter = ({props}) => <div>{props.count}</div>
Counter.propTypes = {
  count(props, propName) {
    if (props[propName] < 10) {
      return new Error('count must be less than 10')
    }
  }
}

export default validate(Counter)

API

validate(component, [warningLevel])

component

type: function | object

Validate props passed to component match the specified type. An Error is thrown if a prop is not valid.

warningLevel

type: number

default: 1

If a missing propType is discovered, validate will by default log a warning. This functionality may be configured.

0 - Do not warn or throw error

1 - Log a warning

2 - Throw an error

License

MIT © Dustin Specker