New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vue-types

Package Overview
Dependencies
Maintainers
1
Versions
97
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-types

Prop types utility for Vue

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
77K
decreased by-11.11%
Maintainers
1
Weekly downloads
 
Created
Source

vue-types

Prop type definitions for Vue.js. Compatible with both Vue 1.x and 2.x

Introduction

vue-types is a collection of configurable prop type definitions for Vue.js components, inspired by React.PropTypes.

When to use

While basic prop type definition in Vue is simple and convenient, detailed prop validation can become verbose on complex components. This is the case for vue-types.

Instead of:

export default {
  props: {
    id: {
      type: Number,
      default: 10
    },
    name: {
      type: String,
      required: true
    },
    age: {
      type: Number,
      validator(value) {
        return Number.isInteger(value)
      }
    },
    nationality: String
  },
  methods: {
    // ...
  }
};

You may write:


import VuePropTypes from 'vue-pro-types';

export default {
  props: {
    id: VuePropTypes.number.def(10),
    name: VuePropTypes.string.isRequired,
    age: VuePropTypes.integer,
    // No need for `default` or `required` key, so keep it simple
    nationality: String 
  },
  methods: {
    // ...
  }
}

Installation

NPM

npm install vue-types --save

Documentation

Native Types

Most native types come with a default value, a .def() method to reassign the default value for the current prop and a isRequired flag to set the required: true key

const numProp = vueTypes.number
// numProp === { type: Number, default : 0}

const numPropCustom = vueTypes.number.def(10)
// numPropCustom ===  { type: Number, default : 10}

const numPropRequired = vueTypes.number.isRequired
// numPropCustom ===  { type: Number, required : true}

const numPropRequiredCustom = vueTypes.number.def(10).isRequired
// numPropRequiredCustom ===  { type: Number, default: 10, required : true}
VuePropTypes.any

Validates any type of value and has no default value.

VuePropTypes.array

Validates that a prop is an array primitive.

  • Default: Array
VuePropTypes.bool

Validates boolean props.

  • default: true
VuePropTypes.func

Validates that a prop is a function.

  • default: an empty function
VuePropTypes.number

Validates that a prop is a number.

  • default: 0
VuePropTypes.integer

Validates that a prop is an integer (uses Number.isInteger).

  • default: 0
VuePropTypes.object
VuePropTypes.object

Validates that a prop is an object.

  • default: Object
VuePropTypes.string
VuePropTypes.string

Validates that a prop is a string.

  • default: ''

Custom Types

Custom types have not default value, a .def() method to assign a default value for the current prop and a isRequired flag to set the required: true key

const oneOfPropDefault = vueTypes.oneOf([0, 1]).def(1)
// oneOfPropCustom.default === 1

const oneOfPropRequired = vueTypes.oneOf([0, 1]).isRequired
// oneOfPropCustom.required ===  true

const oneOfPropRequiredCustom = vueTypes.oneOf([0, 1]).def(1).isRequired
// oneOfPropRequiredCustom.default ===  1
// oneOfPropRequiredCustom.required === true
VuePropTypes.instanceOf()
class Person {
  // ...
}

export default {
  props: {
    user: VuePropTypes.instanceOf(Person)
  }
}

Validates that a prop is an instance of a JavaScript constructor. This uses JavaScript's instanceof operator.

VuePropTypes.oneOf()
VuePropTypes.oneOf(arrayOfValues)

Validates that a prop is one of the provided values.

export default {
  props: {
    genre: VuePropTypes.oneOf(['action', 'thriller'])
  }
}
VuePropTypes.oneOfType()

Validates that a prop is an object that could be one of many types. Accepts both simple and vue-types types.

export default {
  props: {
    theProp: VuePropTypes.oneOfType([
      String, 
      VuePropTypes.integer,
      VuePropTypes.instanceOf(Person)
    ])
  }
}
VuePropTypes.arrayOf()

Validates that a prop is an array of a certain type.

export default {
  props: {
    theProp: VuePropTypes.arrayOf(String)
  }
}

//accepts: ['my', 'string']
//rejects: ['my', 1]
VuePropTypes.objectOf()

Validates that a prop is an object with values of a certain type.

export default {
  props: {
    userData: VuePropTypes.objectOf(String)
  }
}

//accepts: userData = {name: 'John', surname: 'Doe'}
//rejects: userData = {name: 'John', surname: 'Doe', age: 30}
VuePropTypes.shape()

Validates that a prop is an object taking on a particular shape. Accepts both simple and vue-types types.

export default {
  props: {
    userData: VuePropTypes.shape({
      name: String,
      age: VuePropTypes.integer
    })
  }
}

//accepts: userData = {name: 'John', age: 30}
//rejects: userData = {name: 'John', age: 'wrong data'}

License

MIT

Copyright (c) 2016 Marco Solazzi

Keywords

FAQs

Package last updated on 28 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc