项目作者: znck

项目描述 :
Fluent prop validation for Vue
高级语言: JavaScript
项目地址: git://github.com/znck/prop-types.git
创建时间: 2017-11-09T05:09:58Z
项目社区:https://github.com/znck/prop-types

开源协议:MIT License

下载



prop-types






vue2
NPM version
NPM downloads
CircleCI
codecov

Introduction

Fluent prop validation for Vue that won’t land in your production code.

Use rollup-plugin-replace or DefinePlugin to replace process.env.NODE_ENV with 'production'.
If you are using Vue CLI or Nuxt, it’s already done for you.

Usage

Installation

  1. npm install --save @znck/prop-types

Examples

  1. import PropTypes from '@znck/prop-types'; // ES6
  2. var PropTypes = require('@znck/prop-types'); // ES5 with npm

Make sure to add @znck/prop-types/remove to babel config.

  1. // babel.config.js or .babelrc.js
  2. ...
  3. plugins: [
  4. '@znck/prop-types/remove'
  5. ]
  6. ...

Here is an example of using PropTypes with a Vue component, which also
documents the different validators provided:

  1. <script>
  2. import PropTypes from 'prop-types';
  3. export default {
  4. props: {
  5. // You can declare that a prop is a specific JS primitive. By default, these
  6. // are all optional.
  7. optionalArray: PropTypes.array,
  8. optionalBool: PropTypes.bool,
  9. optionalFunc: PropTypes.func,
  10. optionalNumber: PropTypes.number,
  11. optionalObject: PropTypes.object,
  12. optionalString: PropTypes.string,
  13. optionalSymbol: PropTypes.symbol,
  14. // You can also declare that a prop is an instance of a class. This uses
  15. // JS's instanceof operator.
  16. optionalMessage: PropTypes.instanceOf(Message),
  17. // You can ensure that your prop is limited to specific values by treating
  18. // it as an enum.
  19. optionalEnum: PropTypes.oneOf(['News', 'Photos']),
  20. // An object that could be one of many types
  21. optionalUnion: PropTypes.oneOfType([
  22. PropTypes.string,
  23. PropTypes.number,
  24. PropTypes.instanceOf(Message)
  25. ]),
  26. // An array of a certain type
  27. optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
  28. // An object with property values of a certain type
  29. optionalObjectOf: PropTypes.objectOf(PropTypes.number),
  30. // An object taking on a particular shape
  31. optionalObjectWithShape: PropTypes.shape({
  32. color: PropTypes.string,
  33. fontSize: PropTypes.number
  34. }),
  35. // You can chain any of the above with `isRequired` to make sure a warning
  36. // is shown if the prop isn't provided.
  37. requiredFunc: PropTypes.func.isRequired,
  38. // A value of any data type
  39. requiredAny: PropTypes.any.isRequired,
  40. // You can also supply a custom validator.
  41. customArrayProp: PropTypes.string.validate(value => value === 'foo'),
  42. }
  43. }
  44. </script>

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Author

prop-types © Rahul Kadyan, Released under the MIT License.

Authored and maintained by Rahul Kadyan with help from contributors (list).

znck.me · GitHub @Rahul Kadyan · Twitter @znck0"">@znck0