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

es7-checktypes-decorator

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es7-checktypes-decorator

An es7 decorator used to validate class and methods arguments.

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

es7-checktypes-decorator

v0.2.1

A simple es7 decorator to control your class and methods arguments types or instances. If the decorator detects an anomaly, it will throw a ArgumentTypeError with a message showing the wrong argument.

Installation

Configure babel to use decorators:

npm i --save-dev babel-transform-decorators-legacy and add "plugins": ["transform-decorators-legacy"] in your .babelrc.

then: npm i --save-dev es7-checktypes-decorator

Usage

First, require the decorator:

import { checktypes } from 'es7-checktypes-decorator';

The decorator will control if your arguments have the good type and / or are an instance of a Class.

You have simply to call the decorator before a method or a class. Like that:

@checktypes('Number', CustomClass)
// This class expects to receive arg1 as Number (typeof) and arg2 as CustomClass instance (instanceof).
class Class_expecting_Number_type_and_CustomClass_instance {
    constructor(arg1, arg2) {}

    @checktypes('String', 'Number')
    method_expecting_String_type_and_Number_type(arg1, arg2) {}

    @checktypes(Array, Date, CustomClass)
    method_expecting_Array_and_Date_and_CustomClass_instances(arg1, arg2, arg3) {}  

    @checktypes(['String', CustomClass], ['Number', 'String'])
    // This method expects to receive arg1 as String or CustomClass instance
    // and arg2 as Number or String.
    method_expecting_String_type_or_CustomClass_and_Number_type_or_String_type(arg1, arg2) {}
}

If the validation is ok, your class or your methods will be executed as always.

If the validation is wrong, the decorator will throw a ArgumentTypeError (inherited from TypeError) with a custom message.

Keywords

FAQs

Package last updated on 09 Jul 2017

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