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

asasalad

v2.0.3

Published

Redux Form Validation Utility Designed to Work With Immutable.js

Downloads

18

Readme

Asasalad

travis build

Asasalad is a Redux form validation utility designed to work with Immutable.js.

##Overview

  1. Installation
  2. API
  3. Influences
  4. Explain the Name
  5. Future

Installation

npm install --save asasalad 

##API

###Note Every mention of a map (e.g. error map) refers to an Immutable.js Map.

checkFactory

checkFactory(fun, field, ...messages)

####Description Augments a predicate with field and messages properties which are used to build an error map during validation.

####Arguments

  1. fun (function) : a predicate that checks for a property that must be true of the form, return false if check fails
  2. field (string) : the target field of the check (used to populate the error map)
  3. messages (...string) : messages that describes why the check failed

####Returns

  1. check (function) : the given predicate (i.e fun) augmented with field and messages properties.

####Example

const passwordCheck = checkFactory(({ password }) =>
                             /^.{3,20}$/.test(password),
                             'password',
                             'password must be 3-20 characters')

###validatorFactory

validatorFactory(...checks)

####Description Takes a collection of checks created with the help of checkFactory and returns a validator.

####Arguments

  1. checks (...function) : checks are predicates with field and messages properties as returned from checkFactory

####Returns

  1. validator (function) : a validator takes two arguments: errors map and a state map, runs all the checks specified during its creation, and returns an updated error map.

####Example

export const usernameLengthCheck = checkFactory(( { username }) =>
                                   /^.{3,20}$/.test(username),
                                   'username',
                                   "username must be 3 to 20 characters")

export const usernameCharCheck = checkFactory(({ username }) =>
                                 /^[a-z0-9_-]*$/.test(username),
                                 'username',
                                 "username can only include a-z, 0-9 , _ , and -")
                              
const validateUsername = validatorFactory(usernameLengthCheck, usernameCharCheck)

###combineValidators

combineValidators(...validators)

####Description Takes a collection of validators created with validatorFactory and returns a mega validator. combineValidators makes creating a validator for the entire form a snap if you've already defined validators for the various fields in the form.

For example, you can have a seperate validator for each field, which will run after some field specific event (e.g. onBlur). Then you can combine the valdiators for each field into a complete form validator, which you can run (e.g before sending an AJAX request.)

####Arguments

  1. validators (...function) : validators are functions with the signature (errors, state) -> errors, and should be created with validatorFactory.

####Returns

  1. validator (function) : a mega validator that is functionally equivalent to chaining calls to all the validators passed as arguments to combineValidators.

####Example


const validateUsername = validatorFactory(usernameLengthCheck, usernameCharCheck)

const validateEmail = validatorFactory(emailCheck)

const validatePassword = validatorFactory(passwordCheck, confirmCheck)

const validateConfirm = validatorFactory(confirmCheck)

const validateForm = combineValidators(validateUsername,
                                       validateEmail,
                                       validatePassword,
                                       validateConfirm)

validate

validate(state, validator)

####Description Validate is used inside a Redux reducer. It takes your form state, and an Asasalad validator, and returns a new form state with an updated error map.

####Arguments

  1. state (map) : Immutable.js map that contains a form's state
  2. validator (function) : a validator built with either validatorFactory or combineValidators

####Returns

  1. state (map) : form state with an updated error map

####Example

const signupFormReducer = (state = initialState, action) => {
  switch (action.type) {
    ...
    case 'VALIDATE_SIGNUP_USERNAME':
      return validate(state, validateUsername)
    case 'VALIDATE_SIGNUP_EMAIL':
      return validate(state, validateEmail)
    case 'VALIDATE_SIGNUP_PASSWORD':
      return validate(state, validatePassword)
    ...

Influences

The design of Asasalad was influenced by examples from Functional JavaScript by Michael Fogus.

Explain the Name

If I'm eating an amazing cookie, I might say it's valid as a salad (asasalad).

Future

I am currently working on another library that builds atop of Asasalad called Franklin, which willbuild complete redux forms given little more than a formName and validatorMap.

const validateUsername = validatorFactory(usernameLengthCheck, usernameCharCheck)

const validatePassword = validatorFactory(passwordCheck)

const validatorMap = {
  username : validateUsername,
  password : validatePassword,
}

const { reducer, container } = generateForm('login', validatorMap)