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-tsa

v1.0.0

Published

Async friendly validation middleware for Redux

Downloads

17

Readme

Redux TSA

Build Status codecov codebeat badge npm version

Async friendly validation middleware for Redux

Overview

  1. Gist
  2. API
  3. Performance
  4. Complementary Libraries
  5. Examples

Gist

You define validators

const even: SyncValidator<State, Transaction, "amount"> = {
    check({ field }) {
        return Number.isInteger(field / 2)
    },
    error({ fieldKey }) {
        return `${fieldKey} must be even`;
    },
};

const goodPerson: AsyncValidator<State, Transaction, 'target'> = {
    check({ field }) {
        // backgroundCheck calls an API that performs a background check
        return backgroundCheck(field);
    },
    error({ field }) {
        return `${field} is a criminal`;
    },
};

You use validate or validateSync to specify validation

type TransactionType = "DEPOSIT" | "WITHDRAWAL";

interface Transaction extends Redux.Action {
    type: TransactionType;
    target: string;
    amount: number;
}

function withdrawal(target: string, amount: number): Transaction {
    const action: Transaction = {
        amount,
        target,
        type: "WITHDRAWAL",
    };

    // you can mix sync and async validators, they will be normalized internally
    const validatorMap: ValidatorMap<State, Transaction> = {
        target: [goodPerson],
        amount: [fundsAvailable, even],
    };

    return validate({ action, validatorMap });
}

Redux TSA will perform the specified validation:

  • If validation succeeds, the original action is passed to the next middleware
  • If validation fails, an error action is passed to the next middleware

You use the isTSAErrorAction type guard to check for failure:

// inside a reducer
case "WITHDRAWAL":
    if (isTSAErrorAction(action)) {
        return { ...state, errors: action.fieldErrors! };
    } else {                
        return initialState;
    }

API

Functions

  • reduxTSA
  • validate
  • validateSync
  • isTSAErrorAction

reduxTSA

reduxTSA is the middleware:


import reduxTSA from "redux-tsa";

applyMiddleware(reduxTSA)

validate

validate specifies async validation for an action:


/**
 * mode specifies the max number of errors that should be captured per field
 * fieldErrors and processErrors both affect the error count
 * mode defaults to Infinity, which captures as many errors as possible
 * mode=0 specifies binary validation
 * a lower mode means faster validation because the validators for a given field are raced
 */
interface ValidateInput<S, A extends Redux.Action> {
    action: A;
    validatorMap: ValidatorMap<S, A>;
    mode?: number;
}

validate<A extends Redux.Action>(input: ValidateInput) => A;

validateSync

validateSync specifies sync validation for an action:


/**
 * mode specifies the max number of errors that should be populated per field
 * fieldErrors and processErrors both affect the error count
 * mode defaults to Infinity, which captures as many errors as possible
 * mode=0 specifies binary validation
 * a lower mode means faster validation becuase sync validation is performed lazily
 */
interface ValidateSyncInput<S, A extends types.Action> {
    action: A;
    validatorMap: types.SyncValidatorMap<S, A>;
    mode?: number;
}

validateSync<A extends Redux.Action>(input: ValidateSyncInput) => A;

isTSAErrorAction

isTSAErrorAction is a type guard that is used to determine whether an action passed validation:

isError<A extends Redux.Action>(action: TSAAction<A>): action is TSAErrorAction<A>

Types/Interfaces

  • AsyncValidator
  • SyncValidator
  • SyncValidatorMap
  • Validator
  • ValidatorMap
  • TSAAction
  • TSAError
  • ErrorMap

Only the above types/interfaces are exported. Other types/interfaces are also listed below for clarity.

AsyncValidator


// ValidatorInput is used for both AsyncValidators and SyncValidators
interface ValidatorInput<S, A extends Redux.Action, K extends keyof A> {
    fieldKey: K;
    field: A[K];
    action: A;
    state: S;
}

// ProduceError is used for both AsyncValidators and SyncValidators
type ProduceError<S, A extends Redux.Action, K extends keyof A> = (
    input: ValidatorInput<S, A, K>
) => TSAError;

type AsyncCheck<S, A extends Redux.Action, K extends keyof A> = (
    input: ValidatorInput<S, A, K>
) => Promise<boolean>;

interface AsyncValidator<S, A extends Redux.Action, K extends keyof A> {
    check: AsyncCheck<S, A, K>;
    error: ProduceError<S, A, K>;
}

SyncValidator

type SyncCheck<S, A extends Redux.Action, K extends keyof A> = (
    input: ValidatorInput<S, A, K>
) => boolean;

interface SyncValidator<S, A extends Redux.Action, K extends keyof A> {
    check: SyncCheck<S, A, K>;
    error: ProduceError<S, A, K>;
}

SyncValidatorMap

type SyncValidatorMap<S, A extends Redux.Action> = {
    [K in keyof A]?: Array<SyncValidator<S, A, K>>;
};

Validator


type Validator<S, A extends Redux.Action, K extends keyof A> = 
    SyncValidator<S, A, K> | AsyncValidator<S, A, K>;

ValidatorMap


type Validator<S, A extends Redux.Action, K extends keyof A> = SyncValidator<S, A, K> | AsyncValidator<S, A, K>;

type ValidatorMap<S, A extends Redux.Action> = {
    [K in keyof A]?: Array<Validator<S, A, K>>;
};

TSAAction


/**
 * fieldErrors are the errors produced by your validators
 * processErrors are the errors that occur when trying to run your validators (e.g. failed network request)
 * fieldErrors and processErrors will be null only if mode=0
 */
interface ErrorActionHelp<A extends Redux.Action, T extends keyof A> {
    type: A[T];
    error: boolean;
    fieldErrors: types.ErrorMap<A> | null;
    processErrors: types.ErrorMap<A> | null;
}

type ErrorAction<A extends Redux.Action> = ErrorActionHelp<A, "type">;

type TSAAction<A extends Redux.Action> = A | ErrorAction<A>;

TSAError

type TSAError = Error | string;

ErrorMap

type ErrorMap<A extends Redux.Action> = { [K in keyof A]?: TSAError[] };

Performance

Redux TSA makes validation fast using the concept of a mode. mode is specified when calling either validate or validateSync, and specifies how many errors to capture per field. The lower the mode, the faster validation will be.

Async Performance

When performing async validation:

  • Redux TSA races the validators for each field. Redux TSA is done validating a field as soon as mode number of errors are found.
  • Redux TSA runs the validators for each field concurrently.

Sync Performance

When performing sync validation:

  • Redux TSA runs the validators lazily. If mode number of errors were already found for a field, then Redux TSA will not run any more validators for that field.

Complementary Libraries

  1. Redux Transform: lets you transform the properties of an action in much the same way that Redux TSA lets validate the properties of an action.

Examples

An example application using Redux TSA: