# asasalad

> Redux Form Validation Utility Designed to Work With Immutable.js

Latest version **2.0.3** (published 2016-08-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install asasalad
pnpm add asasalad
yarn add asasalad
bun add asasalad
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.3 |
| Published | 2016-08-16 |
| First published | 2016-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Author | Mateusz Okon |
| Maintainers | teusz |
| Keywords | redux, validation, immutable, form |

## Links

- npm: https://www.npmjs.com/package/asasalad
- Repository: https://github.com/contrarian/asasalad
- Homepage: https://github.com/contrarian/asasalad#readme
- Issues: https://github.com/contrarian/asasalad/issues
- npm.io page: https://npm.io/package/asasalad

## Dependencies (1)

- [immutable](https://npm.io/package/immutable.md) 3.8.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.3 (latest) — 2016-08-16
- 2.0.2 — 2016-08-15
- 2.0.1 — 2016-08-15
- 1.1.6 — 2016-08-08
- 1.1.5 — 2016-08-08
- 1.0.5 — 2016-08-08
- 1.0.4 — 2016-08-08
- 1.0.3 — 2016-08-08
- 1.0.2 — 2016-08-08
- 1.0.1 — 2016-08-08

## README

# Asasalad
[![travis build](https://img.shields.io/travis/contrarian/asasalad.svg?style=flat-square)](https://travis-ci.org/contrarian/asasalad)

Asasalad is a [Redux](https://github.com/reactjs/redux) form validation utility designed to work with [Immutable.js](https://github.com/facebook/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](https://facebook.github.io/immutable-js/docs/#/Map).


### checkFactory 

```javascript 
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 

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

###validatorFactory 

```javascript 
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 

```javascript 
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 

```javascript 
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 

```javascript 

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 

```javascript 
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 

```javascript 
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](https://www.amazon.com/Functional-JavaScript-Introducing-Programming-Underscore-js/dp/1449360726/ref=sr_1_1?s=books&ie=UTF8&qid=1471316204&sr=1-1) 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.

```javascript 
const validateUsername = validatorFactory(usernameLengthCheck, usernameCharCheck)

const validatePassword = validatorFactory(passwordCheck)

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

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

```

---
_Source: https://npm.io/package/asasalad · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
