npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-action-validator

v1.0.1

Published

Extensible class to validate Redux/Flux actions properties and ensure consistency across large projects

Downloads

7

Readme

redux-action-validator

Extensible class to validate Redux/Flux action properties and ensure consistency across large projects.

Installation

npm install --save redux-action-validator

Usage

First, import the ReduxAction class at the top of your file.

import ReduxAction from 'redux-action-validator'

Define Your Custom Action Types that extend ReduxAction

Create a new class that extends ReduxAction

class MyAction1 extends ReduxAction {
  constructor(o) {
    super(MyAction1.TYPE, {
      id:         ReduxAction.NUMBER,
      myList:     ReduxAction.ARRAY,
      mySample:   ReduxAction.STRING,
      myMap:      ReduxAction.OBJECT,
      myTest:     ReduxAction.BOOLEAN
    }, o)
  }
}
MyAction1.TYPE = 'MY_ACTION_1'

class MyAction2 extends ReduxAction {
  constructor(o) {
    super(MyAction2, {
      someNumber: ReduxAction.NUMBER
    }, o)
  }
}
MyAction2.TYPE = 'MY_ACTION_2'

Let's break it down.

MyAction1 is the name of the Redux/Flux action you want to create and validate.

MY_ACTION_1 is the internal string constant you want to associate with MyAction1. This will become MyAction1.type. Remeber that these constants need to be unique throughout your entire Redux application namespace.

id, myList, mySample, myMap, myTest are all properties that you want to force each MyAction1 instance to contain. These are mapped to type constants. redux-action-validator contains the following property type constants:

  • ReduxAction.NUMBER
  • ReduxAction.ARRAY
  • ReduxAction.STRING
  • ReduxAction.OBJECT
  • ReduxAction.BOOLEAN

If you don't see a constant for the type you're trying to validate, don't worry! You can also use a string. The code will be looking for the result of typeof someProp to validate it.

Notice the o variable that's part of the constructor. This is the raw Action object that will get passed to the constructor of MyAction1. Let's take a closer look at how you'll use that:

Inside your Action Creator

Every Redux action must return a standard javascript object with at least a type property. Without redux-action-validator it would look like this:

function myActionCreator(id) {
  return {
    type: 'MY_ACTION_1',
    id: id
  }
}

With redux-action-validator you end up with:

function myActionCreator(id) {
  return new MyAction1({ id: id }).toObject()
}

The benefit is that you've already established a list of expected properties and types in the MyAction1 class definition. Under the hood, redux-action-validator will test all of the properties of your action to make sure you're following the contract you decided on up front. If not, it will throw an Error with a detailed description of why it failed:

Error: Invalid Action Property for Type [MY_ACTION_1]: [id] expected to be type [number] but saw [array] instead.

This works if you pass the wrong value type of an action property. And it also works if you fail to include a specific property.

Error: Invalid Action Property for Type [MY_ACTION_1]: [id] expected to be type [number] but saw [undefined] instead.

Inside your Reducer

switch (action.type) {
  case type(MyAction1.TYPE):
    const myAction1 = new MyAction1(action).toObject()
}

Here, you have a convenient static class value TYPE to use with switch (or whatever method you use). Then, inside the case block you just need to pass the raw action from Redux into the constructor of your custom MyAction1 class. If any of the action properties are missing or of the wrong type, it will throw an error. This should help keep your Redux/Flux actions structured in a consistent and predictable way. This is especially useful if you're working on a large project with multiple contributors.

To summarize:

  1. Create all of your Action Types in a top-level file. Be sure to import the ReduxAction class and have all of your custom action types extend that base class.
  2. Import these Action Type definitions into your Redux Action Creators and Reducers
  3. Have each creator return an object from the toObject() method of your custom Action class
  4. Have each reducer use the TYPE constant of your custom Action class in the switch statement
  5. Create an instance of your custom action type by passing the raw action object to the constructor of your action type class.

At any point in this process, if an invalid action comes through an exception will be thrown.

If you ideas, suggestions or bug reports feel free to create an Issue or Pull Request on this repo.

Thanks

Michael, AngularToReact.com