# params-validate

> Validate params

Latest version **0.0.6** (published 2014-05-10) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2014-05-10 |
| First published | 2014-02-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Michael Kroher |
| Maintainers | infrared |
| Keywords | param validation |

## Links

- npm: https://www.npmjs.com/package/params-validate
- Repository: https://github.com/infrared/params-validate-js
- Issues: https://github.com/infrared/params-validate-js/issues
- npm.io page: https://npm.io/package/params-validate

## 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.6 (latest) — 2014-05-10
- 0.0.5 — 2014-04-04
- 0.0.4 — 2014-03-25
- 0.0.2 — 2014-03-01
- 0.0.1 — 2014-02-28

## README

##Description##
Validate parameters 

##Installation##

```bash
npm install params-validate
```
or in the browser:
```html
<script src="https://raw.github.com/infrared/params-validate-js/development/lib/index.js"></script>
```
##Usage##
    
    var paramsValidate = require('params-validate');
    
    var errors = paramsValidate(data,test);
    if (errors) { ... }
    
    //or with a callback:
    
    paramsValidate(data,test,function(errors,res) {
        if (errors) {
            console.log(errors);
        } else {
            //res is true
            console.log('passed!');
        }
    });




##Examples##
```javascript
    var data = {
      username: "foo",
      firstname: "Foo",
      age: 89
    };

    var test = {
        username: "string",
        firstname: "string",
        lastname: "string",
        age: "number"
    };
    
    paramsValidate(data,test)
    
    Returns:
    [ { lastname: 'required' } ]
```

**string - test if param is a string**
          
```javascript
    var data = {
        foo: "bar"
    }
    var test = {
        foo: "string"
    }
    //passes
```
**number - test if param is a number**

```javascript
    var data = {
        foo: 5
    }
    var test = {
        foo: "number"
    }
    //passes
```
**either - test if param is either string or number**
```javascript
    var data = {
        foo: "oh hi"
    }
    var test = {
        foo: "either"
    }
    //passes
```
**object - test if param is a key/value object**
```javascript
    var data = {
        foo: { bar: "baz"}
    }
    var test = {
        foo: "object"
    }
    //passes
```
**array  - test if param is an array**
```javascript
    var data = {
        foo: [ "bar", "baz" ]
    }
    var test = {
        foo: "array"
    }
    //passes
```
**price - test if param is a price format**
```javascript
    var data = {
        cost: "5.00"
    }
    var test = {
        cost : "price"
    }
    //passes
```
**function - supply a function to validate the data**

To pass the test with a function, the function must return true. To fail the test, just return a string
```javascript
    // passes
    var test = {
        lastname: function(x) { return true }
    }
    //fails
    var test { 
        lastname: function(x) { return "foo" }
    }
```
  

**Constituents**

Optional constituents can be added for granular validation.

```javascript
    var test = {
        username: {
            regex: "\\w+"
        },
        lastname: "string",
        age: "number",
        role: {
            isin: ["user", "admin"]
        }
    }
```

**Currently supported constituents**

* regex - test data against a supplied regex
* isin - test if given value is in an array


**License**

MIT

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