# compose-validators

> Composable JavaScript validators

Latest version **1.0.1** (published 2020-05-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install compose-validators
pnpm add compose-validators
yarn add compose-validators
bun add compose-validators
```

## 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.0.1 |
| Published | 2020-05-22 |
| First published | 2020-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 218.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Anton Telesh |
| Maintainers | antontelesh |
| Keywords | validation |

## Links

- npm: https://www.npmjs.com/package/compose-validators
- Repository: https://github.com/Antontelesh/compose-validators
- Homepage: https://github.com/Antontelesh/compose-validators#readme
- Issues: https://github.com/Antontelesh/compose-validators/issues
- npm.io page: https://npm.io/package/compose-validators

## 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.1 (latest) — 2020-05-22
- 1.0.0 — 2020-05-22

## README

# compose-validators

Composable JavaScript validators

## Install

with npm:

```
npm install compose-validators
```

or yarn:

```
yarn add compose-validators
```

## [**API Docs**](docs/README.md)

## Why

npm contains several popular packages that perform different kinds of validations.
But none of them provides clean and composable functions that can be used as validators.
Some of them invent their own domain-specific languages, forcing you to learn custom syntax.
Others require you to dive deep into JSON schemas and don't give you clean output.

This library is an attempt to solve these problems.
It is focused on

- Composition. In `compose-validators` everything is a validator, so you can compose your validators to get more complex ones while having the same API everywhere.
- Type safety. It is written in TypeScript meaning that you get your validators being statically typed out of the box.
- Small size. We want the library to be as lightweight as possible while keeping the source code readable. Also, it has zero runtime dependencies. It's now 729 bytes according to [size-limit](ai/size-limit).

## Concepts

### Validator

Every validator is a function that accepts a value to be validated and returns a validation result.

### Validation result

Validation result is always a plain object with collected validation errors. If the object is empty it means the value is valid.

Having validation result as an object allows us to combine results from composed validators like [`object`](docs/README.md#object) or [`arrayOf`](docs/README.md#arrayOf). This way you can nest your objects and arrays and therefore validate deep structures, like the following example:

```ts
import { object, string, required, compose } from "compose-validators";

const validator = object({
  name: compose(string, required),
  address: object({
    city: compose(string, required),
  }),
});

validator({
  name: "John Doe",
  address: {
    city: "",
  },
});
```

Applying this validator to an object with `address.city` being an empty string,
you will get the following validation result:

```json
{
  "address": {
    "city": {
      "required: true
    }
  }
}
```

This validation result is non deterministic about your actual error messages,
it does not deal with any sort of i18n,
though provides enough information for you to display a clean error message.

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