# be-sure

> NPM module for terse, 'throwy' validation

Latest version **1.0.0** (published 2021-11-17) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install be-sure
pnpm add be-sure
yarn add be-sure
bun add be-sure
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-11-17 |
| First published | 2021-11-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ents24 |
| Maintainers | ents24 |
| Keywords | terse, validation, throwy |

## Links

- npm: https://www.npmjs.com/package/be-sure
- Repository: https://github.com/Ents24/npm-be-sure
- Homepage: https://github.com/Ents24/npm-be-sure#readme
- Issues: https://github.com/Ents24/npm-be-sure/issues
- npm.io page: https://npm.io/package/be-sure

## 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.0 (latest) — 2021-11-17

## README

# Be Sure
NPM module for terse, "throwy" validation

A common problem in web apps is where to validate parameters, and how, and what to do on failure. On one hand, it's best if every function validates its params thoroughly. On the other hand validation-in-place can be verbose and make modules harder to read. `beSure` attempts to provide the shortest validation syntax possible to keep code DRY and encourage consistent use by:

1. Being very concise
2. Always **throw**ing on validation failure
3. Applying customisation and handler logic elsewhere

It probably belongs at specific level(s) of an app e.g. "all standalone libs" or "all functions in the state-management layer" - thrown errors are therefore anticipated at that level and error handling is applied higher up.

 ## BEFORE - verbose, boilerplate, hard to read

```js
UPDATE_USER_ACTION(userId, userName, dateOfBirth){

  if (typeof userId !== 'number' || userId != == toInteger(value)) {
    throw 'Invalid userId: ' + userId;
  }

  if (typeof userName !== 'string' || userName.length < 5) {
    throw 'Invalid userName: ' + userName;
  }

  if (dateOfBirth instanceof Date !== true || dateOfBirth < new Date()) {
    throw 'Invalid dateOfBirth: ' + dateOfBirth;
  }

  // function body ...

}
```

 ## AFTER - terse, consistent

```js
UPDATE_USER_ACTION(userId, userName, dateOfBirth){

    beSure(userId, 'id')
    beSure(userName, 'string-not-empty')
    beSure(dateOfBirth, 'date-in-past')

  // function body ...

}
```

## Simple Use

Require and use basic validators

```js
const { beSure } = require('@ents24/be-sure')
beSure(myParam, 'slug')
```

## Custom Use

Provide your own validator or redefine existing ones via a wrapper module (see [custom.js](test/custom.js) for example)

In `./libs/be-sure-custom.js`

```js
const beSureCustom = require('@ents24/be-sure')

// Add a new validator
const catCheck = value => typeof value === 'object' && value.says === 'meow'
beSureCustom.setValidator('cat', catCheck)

// Redefine 'int' validator - allow anything up to MAX_SAFE_INTEGER
const maxSafeIntegerCheck = value => {
  if (typeof value !== 'number' || value !== parseInt(value, 10)) {
    return false
  }

  if (value > Number.MAX_SAFE_INTEGER) {
    return false
  }

  return true
}

beSureCustom.setValidator('int', maxSafeIntegerCheck)

module.exports = beSureCustom;
```

In `./lib/various-animal-utils.js`

```js
const { beSure } = require('./lib/beSureCustom');

const cat = {
  name: 'Colin',
  says: 'meow'
}
const dog = {
  name: 'Michael',
  says: 'woof'
}

// ✓ No problem
beSure(cat, 'cat')

// ✗ ValidationError: [object Object] is not a valid cat
beSure(dog, 'cat')

const maxSignedInt32 = 2147483647

// ✓ Int above 32 bit now valid
beSure(maxSignedInt32 + 100, 'int')
```

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