# validate-arguments

> Validate arguments, declaritive

Latest version **0.0.8** (published 2014-05-22) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install validate-arguments
pnpm add validate-arguments
yarn add validate-arguments
bun add validate-arguments
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2014-05-22 |
| First published | 2013-05-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Matthijs van Henten |
| Maintainers | mvhenten |
| Keywords | validation, declarative |

## Links

- npm: https://www.npmjs.com/package/validate-arguments
- Repository: git@github.com:mvhenten/validate-arguments-js
- Homepage: https://github.com/mvhenten/validate-arguments-js
- Issues: https://github.com/mvhenten/validate-arguments-js/issues
- npm.io page: https://npm.io/package/validate-arguments

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) >=1.2.1
- [sliced](https://npm.io/package/sliced.md) ~0.0.5

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

- 0.0.8 (latest) — 2014-05-22
- 0.0.7 — 2014-03-31
- 0.0.6 — 2014-03-11
- 0.0.5 — 2014-03-10
- 0.0.4 — 2014-03-06
- 0.0.3 — 2013-07-13
- 0.0.2 — 2013-06-04
- 0.0.1 — 2013-05-14

## README

validate-arguments-js
=====================

Validate arguments, declarative.

[![Build Status](https://drone.io/github.com/mvhenten/validate-arguments-js/status.png)](https://drone.io/github.com/mvhenten/validate-arguments-js/latest)

## Installation

```bash
    npm install validate-arguments
```

The code itself depends on `lodash`, and will propably run just fine in the browser using `require.js`.

## Documentation

This module levarages some of the validation methods from `lodash`, but by offering you a declaritive syntax and nice error strings:

* array
* boolean
* date
* element
* empty
* finite
* function
* null
* number
* object
* plainObject
* regexp
* string

And adds the following additional ones:

* whole ( An integer )
* real ( A real number that isn't NaN )
* natural ( Positive integer )
* primitive ( 'number', 'boolean', 'string' )

When passing a constructor (function) as an `isa`, an `instanceof` check is done.

```javascript
    var Validate = require('validate-arguments');
    
    function doSomething(withNamedArguments) {
        var args = Validate.named(arguments, {
            number: 'whole',
            name: 'string'
            callback: 'function'
            options: {
                isa: 'plainObject',
                optional: true
            },
            validation: Validate // performs an instanceof
        });
    
        if (!args.isValid()) {
            throw args.errorString();
        }
    
        // continue safely
    }
```
Look at the [test cases](https://github.com/mvhenten/validate-arguments-js/blob/master/test/validate.js) for more examples.

Node that validations may be nested:

```javascript
    var args = Validate.named(arguments, {
        address: {
            primary: {
                street: 'string',
                housenumber: 'number'
            }
        },
    });
```

## Methods

#### `named( named, validationSpec )`

Returns a `validationObject` for further inspection. `named` should be a non-empty plain `Object`, containing all the keys documented in the `validationSpec`.
The `validationSpec` should be an object, where the keys match the desired input. You may use the form `{ thing: 'string' }` over `{ thing: { isa: 'string' } }`.

When passed an `arguments` object instead of a plain object, the first key of the arguments is used.

#### `positional( arguments, ... )`

Validate positional arguments, either an array or arguments object. Spec may be provided as an array in the second argument, or a variable number of arguments.

#### `validate( arguments, ... )`

Validate arguments, freeform.

If the second argument contains a string, it is treated as a positional argument with one element.

### Return values

Both `validateObject` and `validatePositional` return a `validationObject` with the following methods:

* isValid: A boolean indication the validness
* errors: An array containing positions or keys of invalid arguments
* get: Retrieve values from the original input, array index or key
* values: Return an array of values ( not that usable )
* errorString: An error string explaining what went wrong (verbosely)

## Testing

```bash
cd validate-arguments-js
npm install
npm test
```

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