# fun-validation

> 🥳 JS Functional validation library

Latest version **1.0.4** (published 2022-05-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install fun-validation
pnpm add fun-validation
yarn add fun-validation
bun add fun-validation
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-05-10 |
| First published | 2022-04-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 60.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Dusan Jovanov |
| Maintainers | dusanjovanov |
| Keywords | validation, functional |

## Links

- npm: https://www.npmjs.com/package/fun-validation
- Repository: https://github.com/dusanjovanov/fun-validation
- Homepage: https://github.com/dusanjovanov/fun-validation#readme
- Issues: https://github.com/dusanjovanov/fun-validation/issues
- npm.io page: https://npm.io/package/fun-validation

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2022-05-10
- 1.0.3 — 2022-04-25
- 1.0.2 — 2022-04-25
- 1.0.1 — 2022-04-25
- 1.0.0 — 2022-04-25

## README

![Fun validation logo](https://raw.githubusercontent.com/dusanjovanov/fun-validation/master/logo.png 'Fun validation logo')

JS Functional validation library
<br />

[![npm](https://img.shields.io/npm/v/fun-validation?color=%231E90FF&label=npm&style=for-the-badge)](https://www.npmjs.com/package/fun-validation)

# Installation

```bash
npm install fun-validation
```

```bash
yarn add fun-validation
```

# Usage

This library exports a bunch of small validation functions that all have the following signature:

```tsx
(value: any) => boolean
// or when the validation requires additional parameters, we have currying:
(n:number) => (value: any) => boolean
```

So you would call a function like this:

```tsx
import { isEmail } from 'fun-validation';

const isEmailValid = isEmail(someString);
```

The library also exports the `validate` function.

This function has the following signature:

```tsx
(value: any, rules) => validationResult;
```

Where:

- `rules` must have the same shape as the value that was passed in
- and consequently the `validationResult` will also be in that same shape

Meaning:

- If the `value` is a primitive (string, number...etc), the `rules` must be a validation fn with the signature: `(value: any) => boolean`, and the `validationResult` will be a boolean

```tsx
import { validate, isString, isInteger, isNumberMax } from 'fun-validation';

const someString = 'this is a string';

validate(someString, isString); // returns boolean

// or, you can compose your own validation fn

const someNumber = 3;

validate(someNumber, value => isInteger(value) && isNumberMax(5)(value)); // returns boolean
```

- If the `value` is an object (a plain object), the `rules` must an object with the same shape as value but the leaf nodes (primitives) have validation fns as values, and the `validationResult` will be and object in the same shape

```tsx
import { validate, isString } from 'fun-validation';
import { isDate } from 'date-fns';

const user = {
  name: 'Dusan',
  dateOfBirth: new Date(1992, 9, 2),
};

const rules = {
  name: isString,
  dateOfBirth: isDate,
};

const result = validate(user, rules);

// result
{
  name: boolean;
  dateOfBirth: boolean;
}
```

- If the `value` is an array, the `rules` must an array with two elements. First element is a validation fn and is used to validate the array itself (for example length of the array). The second element corresponds to the shape of the element in the `value` array. The `validationResult` will be an array with the first element as boolean (result of validating the array itself), and the second element as an array of booleans (result of validation for each of the elements in the `value` array).

```tsx
import { validate, isArray, isString, isInteger } from 'fun-validation';

// array of primitives

const hobbies = ['tennis', 'basketball'];

const rules = [isArray, isString];

const result = validate(hobbies, rules);

// result
[true, [true, true]];

// array of objects

const friends = [
  { name: 'Dusan', age: 29 },
  { name: 'Peter', age: 33 },
];

const rules = [isArray, { name: isString, age: isInteger }];

const result = validate(friends, rules);

// result
[
  true,
  [
    { name: true, age: true },
    { name: true, age: true },
  ],
];
```

You get the picture!

So, to recapitulate:

- The `rules` must be of the same shape as `value` with the leaf nodes (primitives) being functions that look like this: `(value: any) => boolean`
- The `validationResult` is more or less of the same shape as `value` and `rules` (except in the case of arrays) while the leaf nodes are always booleans

# API

```tsx
const validate: (value: any, rules) => validationResult

const isString: (value: any): boolean;

const isStringLongerThan: (len: number) => (value: any) => boolean

const isStringShorterThan:(len: number) => (value: any) => boolean

const isStringOfLength: (len: number) => (value: any) => boolean

const isStringOfMinLength: (len: number) => (value: any) => boolean

const isStringOfMaxLength:(len: number) => (value: any) => boolean                                                                                                       7

const isFunction: (obj: any) => boolean

const isObject: (value: any) => boolean

const isPromise: (value: any) => boolean

const isArray: (value: any) => boolean

const isArrayLongerThan: (len: number) => (value: any) => boolean

const isArrayShorterThan: (len: number) => (value: any) => boolean

const isArrayOfLength: (len: number) => (value: any) => boolean

const isArrayMinLength: (len: number) => (value: any) => boolean

const isArrayMaxLength: (len: number) => (value: any) => boolean

const isNumber: (value: any) => boolean

const isInteger: (value: any) => boolean

const isFloat: (value: any) => boolean

const isNumberMoreThan: (n: number) => (value: any) => boolean

const isNumberLessThan: (n: number) => (value: any) => boolean

const isNumberEqual: (n: number) => (value: any) => boolean

const isNumberMin: (n: number) => (value: any) => boolean

const isNumberMax: (n: number) => (value: any) => boolean

const isPattern: (regex: RegExp) => (value: any) => boolean

const isEmail: (value: any) => boolean

const isUrl: (value: any) => boolean
```

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