# form-validations

> Form validation done right

Latest version **1.1.0** (published 2016-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install form-validations
pnpm add form-validations
yarn add form-validations
bun add form-validations
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2016-11-28 |
| First published | 2016-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Marvin Hagemeister |
| Maintainers | marvinhagemeister |
| Keywords | form, validation |

## Links

- npm: https://www.npmjs.com/package/form-validations
- Repository: https://github.com/marvinhagemeister/form-validations
- Homepage: https://github.com/marvinhagemeister/form-validations#readme
- Issues: https://github.com/marvinhagemeister/form-validations/issues
- npm.io page: https://npm.io/package/form-validations

## Dependencies (1)

- [type-checks](https://npm.io/package/type-checks.md) ^1.1.0

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

- 1.1.0 (latest) — 2016-11-28
- 1.0.0 — 2016-11-26

## README

[![Build Status](https://travis-ci.org/marvinhagemeister/form-validations.svg?branch=master)](https://travis-ci.org/marvinhagemeister/form-validations) [![NPM version](http://img.shields.io/npm/v/form-validations.svg)](https://www.npmjs.org/package/form-validations)

# Form validation in 1KB

A library that makes form validation fun again. When trying out various different
form validation libraries, I didn't found one where one could easily change
different rules, and customize the error messages as well.

Simple example:

```js
import { validString } from "form-validations";

const isString = validString("not a string"); // Simple helper for custom messages

isString("hello world!"); // Returns: []. No errors means field is valid
isString(1234); // Returns: ["not a string"]
````

A more complex example with chained validations:

```js
import { chain, firstError, validString } from "form-validations";

const isString = validString("not a string");

// Custom validator
function isHello(value) {
  if (!/hello/g.test(value)) {
    return "value does not contain 'hello'";
  }

  return true;
};

// Collect all errors
const validate = chain(isString, isHello);

validate("hello world!"); // Returns: []. No errors means field is valid
validate(1234); // Returns: ["not a string", "value does not contain 'hello'"]

// Stop on first error
const validate = firstError(isString, isHello);
validate(1234); // Returns: ["not a string"]
```

This library was built to allow easy chaining of validations.

## API

### `chain(...validators): string[]`

Easily chain validators together. Each validator can push an error message
to the resulting array.

### `firstError(...validators): string[]`

Contrary to `chain` this function will stop at the first validator that returns
an error. The resulting string array will always have a length of 1.

### Available validations

Each validation function must return either `true` or an error message of type `string`.

| Validation | Check if |
|---|---|
| `validString` | ...value is a `string` |
| `validNumber` | ...value is a `number` |
| `validBool` | ...value is a `boolean` |
| `validDateFormat` | ...value has format: `YYYY-MM-DD` |
| `validDateTimeFormat` | ...value has format: `YYYY-MM-DD hh:mm:ss` |
| `validDateUTCFormat` | ...value has format `YYYY-MM-DDThh:mm:ssZ` |
| `oneOf` | ...value is inside the specified array |
| `required` | ...value is not empty, `undefined` or `null`|

### Available normalizers

Often you need to normalize data when dealing with form elements.

```js
import { normalizeBoolean } from "form-validations";

normalizeBoolean("false"); // returns false;
normalizeBoolean(1); // returns true;
normalizeBoolean([]); // returns false;
```

| Normalize | description |
|---|---|
| `normalizeBoolean` | Returns a boolean |

## LICENSE

MIT, see `LICENSE.md`

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