包详细信息

@znck/prop-types

znck1.1kMIT0.6.3

Fluent prop validation for Vue

vue, prop-types, props, validation

自述文件

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

npm install --save @znck/prop-types

Examples

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

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

// babel.config.js or .babelrc.js
...
  plugins: [
    '@znck/prop-types/remove'
  ]
...

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

<script>
import PropTypes from 'prop-types';

export default {
  props: {
    // You can declare that a prop is a specific JS primitive. By default, these
    // are all optional.
    optionalArray: PropTypes.array,
    optionalBool: PropTypes.bool,
    optionalFunc: PropTypes.func,
    optionalNumber: PropTypes.number,
    optionalObject: PropTypes.object,
    optionalString: PropTypes.string,
    optionalSymbol: PropTypes.symbol,

    // You can also declare that a prop is an instance of a class. This uses
    // JS's instanceof operator.
    optionalMessage: PropTypes.instanceOf(Message),

    // You can ensure that your prop is limited to specific values by treating
    // it as an enum.
    optionalEnum: PropTypes.oneOf(['News', 'Photos']),

    // An object that could be one of many types
    optionalUnion: PropTypes.oneOfType([
      PropTypes.string,
      PropTypes.number,
      PropTypes.instanceOf(Message)
    ]),

    // An array of a certain type
    optionalArrayOf: PropTypes.arrayOf(PropTypes.number),

    // An object with property values of a certain type
    optionalObjectOf: PropTypes.objectOf(PropTypes.number),

    // An object taking on a particular shape
    optionalObjectWithShape: PropTypes.shape({
      color: PropTypes.string,
      fontSize: PropTypes.number
    }),

    // You can chain any of the above with `isRequired` to make sure a warning
    // is shown if the prop isn't provided.
    requiredFunc: PropTypes.func.isRequired,

    // A value of any data type
    requiredAny: PropTypes.any.isRequired,

    // You can also supply a custom validator.
    customArrayProp: PropTypes.string.validate(value => value === 'foo'),
  }
}
</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

更新日志

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.6.3 (2019-08-16)

  • chore(release): 0.6.2 (d144e96)
  • fix: Preserve Boolean types (6a27feb)
  • docs: update wrong plugin name in README (#8) (ace8761), closes #8

0.6.2 (2019-08-16)

  • fix: Preserve Boolean types (f72d562)

0.6.1 (2019-02-22)

  • fix: Use console.warn for warn logs (e382c29)

0.6.0 (2019-02-16)

  • feat: Add context logger to PropTypes.run helper, rename value to defaultValue and rename validate t (381c253)

0.5.4 (2019-02-10)

  • chore: Remove .npmignore and use files option (5d3ed99)

0.5.3 (2019-02-10)

  • fix: Remove test files from npm bundle (ec40910)

0.5.2 (2019-02-10)

  • chore: Use pnpm and test types (5067a7f)
  • fix: Remove unnecessary variadic arguments (5109471)
  • doc: Nuxt will also set NODE_ENV (#7) (1500c9d), closes #7

0.5.1 (2019-01-26)

Bug Fixes

  • Use Array.concat as fallback to Array.flat (df7c646)

0.5.0 (2019-01-23)

Features

  • Add description function (1ac3249)

0.4.3 (2019-01-22)

Bug Fixes

  • Add missing type definition for oneOf (6ce9364)

0.4.2 (2019-01-20)

Bug Fixes

  • Copy every prop to production props list (1410742)

0.4.1 (2019-01-19)

Bug Fixes

0.4.0 (2019-01-19)

Bug Fixes

  • Add babel transform plugin to remove prop-types (49ae0d5)

0.4.0-alpha.3 (2019-01-19)

Features

  • Wrap props in NODE_ENV production check (a11582d)

0.4.0-alpha.2 (2019-01-19)

Bug Fixes

  • Export CJS module for remove (39993f5)

0.4.0-alpha.1 (2019-01-19)

Features

0.4.0-alpha.0 (2019-01-19)

Features

  • Add babel plugin to remove prop-types (6857cb5)