# validatez

> object validation

Latest version **1.2.0** (published 2017-05-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install validatez
pnpm add validatez
yarn add validatez
bun add validatez
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2017-05-25 |
| First published | 2016-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | sankooc |
| Maintainers | sankooc |
| Keywords | validation, validate |

## Links

- npm: https://www.npmjs.com/package/validatez
- Repository: https://github.com/sankooc/validatez
- Homepage: https://github.com/sankooc/validatez#readme
- Issues: https://github.com/sankooc/validatez/issues
- npm.io page: https://npm.io/package/validatez

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.2

## 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.2.0 (latest) — 2017-05-25
- 1.1.1 — 2017-04-27
- 1.1.0 — 2017-04-27
- 1.0.4 — 2016-12-08
- 1.0.3 — 2016-12-08
- 1.0.2 — 2016-12-07
- 1.0.1 — 2016-12-06
- 1.0.0 — 2016-12-05

## README

# validatez

[![NPM](https://nodei.co/npm/validatez.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/validatez/)

![travis](https://travis-ci.org/sankooc/validatez.svg?branch=master)
![dm](https://img.shields.io/npm/dm/validatez.svg)
![version](https://img.shields.io/npm/v/validatez.svg)

simple way to validate object

## install

`$ npm install validatez`

## base usage

```
const creator = require('validatez');

const schema = {
  name: {
    type: String,
    errMessage: (type, val) =>
      `error occur ${type}-${val}`
    ,
  },
  age: {
    type: Number,
    range: [1, 100],
    errMessage: 'age incorrect',
    required: false,
  },
};

const validator = creator.create(schema);

let data = {
  name: 'atom',
}
validator(data); // pass

data = {
  name: 'atom',
  age: 200,
}
validator(data); // throw exception

```


## type refer

```
const schema = {
  address: {
    type: String,
    pattern: /^\w{10}$/,
  },
  address2: {
    type: '&address', // & + field to refer existed schema
  },
};

const validator = validate.create(schema);

let data = {
  address: 'china beijing bejing',
  address2: 'bejing chaoyangqu chaoyangbeilu',
}
validator(data); // pass

```

## buildin types

all buildin type start with `@`

```

const schema = {
  email: {
    type: '@email',
  },
};
const validator = validate.create(schema);

let data = {
  email: 'sankooc@scd.com',
}
validator(data); // pass

```

 all buildin types [HERE](doc/types.md)

 if you need more buildin types, create [ISSUE](https://github.com/sankooc/validatez/issues/new)

 also you can define or override buildin type

 ```
 validate.register({
   code: {
     type: String,
     pattern: /^\d{16}$/,
   }
 })

 const schema = {
   zcode: {
     type: '@code'
   },
 };
 validator = validate.create(schema);

 data = {
   zcode: '1234123412341234',
 };

 validator(data); //pass

 ```

## simple define

```

const schema = {
  email: '@email',
  name: String,
};
const validator = validate.create(schema);

let data = {
  email: 'sankooc@scd.com',
  name: 'sankooc',
}
validator(data); // pass


```



## simple inject for control flow

### if your code managed by `Promise` your code will be like below

```

function process(data){
  // next
}
getSomeData().then(process)

```

### add data validation

```

const schema = {
  address: {
    type: String,
  },
};

const option = {
  errorType: Promise,
}
const validator = validate.create(schema, option);

getSomeData().then(validator).then(process)

```


### if your code managed by `async` your code will be like below


```

function process(data, callback){
  // next
}
async.waterfall([getSomeData, process],callback);

```

### add data validation

```

const schema = {
  address: {
    type: String,
  },
};

const option = {
  errorType: Function,
}
const validator = validate.create(schema, option);

async.waterfall([getSomeData, validator, process],callback);

```


## support string case

```

const schema = {
  userName: {
    type: String,
  },
};
const option = {
  field: 'snake' // snake, camel, kebab
}
const validator = validate.create(schema, option);

let data = {
  user_name: 'atom',
}
validator(data); // pass

```

## schema option

| key       | type          |default value| desc                         |   
|-----------|---------------|-------------|------------------------------|
| type      |string/function|String       |                              |
| required  |boolean        |false        |                              |
| errMessage|string/function|`error param`| error message                |
| range     |array          |             | enable when type is `Number` |
| pattern   |regex          |             | enable when type is `String` |



## valiate option

| key     |type    |default value | desc             |
|---------|--------|--------|------------------------|
|handle   |string  |`error` | 'error'/'promise'/'callback' |
|field    |string  |`strict`| snake/camel/kebab      |

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