Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including react-validator-prop-types with all npm packages installed. Try it out:

var reactValidatorPropTypes = require("react-validator-prop-types")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

react-validator-prop-types v1.1.0

react-validator-prop-types

React PropType wrappers around validator.js functions.

What problem does this solve

Great validation libraries already exist, but the default set of React PropTypes is pretty sparse by comparison. This just wraps the popular validator.js library into prop types that support the isRequired convention.

Usage

var ValidatorPropTypes = require('react-validator-prop-types');
React.createClass({
  propTypes: {
    background: ValidatorPropTypes.hexColor,
    email: ValidatorPropTypes.email.isRequired,
    username: ValidatorPropTypes.lowercase.isRequired,
  }
  ...
});

The following prop types are available:

  • email
  • url
  • fqdn
  • ip
  • alpha
  • numeric
  • alphanumeric
  • base64
  • hexadecimal
  • hexColor
  • lowercase
  • uppercase
  • int
  • float
  • uuid
  • date
  • creditCard
  • json
  • multibyte
  • ascii
  • fullWidth
  • halfWidth
  • variableWidth
  • surrogatePair
  • mongoId
  • currency

Missing functions ?

There's no easy way to wrap validator functions that take multiple, or optional arguments. This is why functions like isMobilePhone aren't supported (because of the locale argument). Suggestions for an api for those are encouraged.

Acknowledgements

Thanks to Chris O'Hara for validator.js.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free