# @validatem/default-to

> Validatem "validator" for defaulting to a certain value, when no value is given

Latest version **1.0.0** (published 2026-07-29) · WTFPL OR CC0-1.0 license · 0 weekly downloads

## Install

```sh
npm install @validatem/default-to
pnpm add @validatem/default-to
yarn add @validatem/default-to
bun add @validatem/default-to
```

## Health

**Score 45/100 (D)** — status: active.

Positive: no vulnerabilities.

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

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2026-07-29 |
| First published | 2020-06-14 |
| Weekly downloads | 0 |
| License | WTFPL OR CC0-1.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sven Slootweg |
| Maintainers | joepie91 |
| Keywords | validatem, validator |

## Links

- npm: https://www.npmjs.com/package/@validatem/default-to
- Repository: https://codeberg.org/validatem/default-to
- npm.io page: https://npm.io/package/@validatem/default-to

## Alternatives

- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2026-07-29
- 0.1.0 — 2020-06-14

## README

# @validatem/default-to

*This validator is designed to be used with [Validatem](https://validatem.cryto.net/), the universal and composable validation library.*

Inserts a default value if one is not given, or passes through the specified value unchanged if it is - but never rejects any value. Typically used for optional values, as the 'opposite' of [@validatem/required](https://www.npmjs.com/package/@validatem/required).

Note that both `null` *and* `undefined` are treated as "no value is given".

## License, donations, and other boilerplate

Licensed under either the [WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), at your choice. In practice, that means it's more or less public domain, and you can do whatever you want with it. Giving credit is *not* required, but still very much appreciated! I'd love to [hear from you](mailto:admin@cryto.net) if this module was useful to you.

Creating and maintaining open-source modules is a lot of work. A donation is also not required, but much appreciated! You can donate [here](http://cryto.net/~joepie91/donate.html).

## Example

A runnable version of this example is included in the repository, as `example.js`.

```js
"use strict";

const { validateValue } = require("@validatem/core");
const defaultTo = require("@validatem/default-to");

let rules = [
	defaultTo("bar")
];

console.log(validateValue("foo", rules)); // foo
console.log(validateValue(null, rules)); // bar
```

## API

__If you are new to Validatem,__ please read the documentation on the [Validatem website](https://validatem.cryto.net/) first - it will explain how the general API works, and how you should use validator modules with it.

### defaultTo(defaultValueOrFunction)

Callable. Either passes through the input value as-is, or if it is `null` or `undefined`, inserts the specified `defaultValue` instead.

You may specify either a default value directly, or specify a function. If you specify a function, then it will be called *only if* the input was empty and a default value is needed. This is typically useful when generating a default value is computationally expensive, and you only want to do it when necessary. __If the default value *itself* is supposed to be a function, see the literal variant of this function below instead.__

- __defaultValueOrFunction:__ Either a literal value to default to, or a function that returns one.

Possible error codes: None.

### defaultTo.literal(defaultValue)

Same as above, except if you pass in a function as the `defaultValue`, it is used as the default value *as-is*, and is never called.

## Changelog

### 1.0.0 (July 29, 2026)

Initial (stable) release.

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