# type2

> simple data validator

Latest version **0.2.5** (published 2020-02-28) · MIT license · 0 weekly downloads

## Install

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

## 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.2.5 |
| Published | 2020-02-28 |
| First published | 2018-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 41.5 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | lizmlzhou |
| Maintainers | lizzz |

## Links

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

## Dependencies (1)

- [validator](https://npm.io/package/validator.md) ^9.4.1

## Recent versions

- 0.2.5 (latest) — 2020-02-28
- 0.2.4 — 2018-05-23
- 0.2.3 — 2018-03-31
- 0.2.2 — 2018-03-30
- 0.2.1 — 2018-03-30
- 0.2.0 — 2018-03-23
- 0.1.0 — 2018-03-23

## README

# type2 - a simple data validator

## Installation
```
npm install type2 --save
```

## Usage
```javascript
const type = require('type2')

// create a validation rule
const rule = {
    type: type.oneOf(['admin', 'user']),
    name: type.string().length(20),
    age: type.number().range(18, 30),
    gender: type.number().optional(),
    company: type.string().when(data => data.name == 'foo'),
    projects: [{
        url: type.string().match(/^https?:\/\//),
        start_time: type.number().min(+new Date(2018, 3, 1)),
        is_finish: type.bool()
    }]
}

// some data need to validate
const data = {
    type: 'admin',
    name: 'bar',
    age: 18,
    gender: null,
    company: null,
    projects: [
        {
            url: 'https://project.example.com/1',
            start_time: Date.now(),
            is_finish: true
        },
        {
            url: 'https://project.example.com/2',
            start_time: Date.now(),
            is_finish: true
        }
    ]
}

// done
console.log(type.check(data, rule))
```

## Plugin

you can also get the power of [validator](https://www.npmjs.com/package/validator) by using the validator plugin:

```javascript
const type = require('type2')
const validator = require('type2/lib/plugin/validator')

// apply a plugin
type.apply(validator)

// create a validation rule
const rule = {
    websites: [{
        url: type.url(),
        bgcolor: type.hexColor()
        fgcolor: type.hexColor()
        email: type.email()
    }]
}

// some data need to validate
const data = {
    websites: [
        {
            url: 'https://project.example.com/1'
            bgcolor: '#ffffff'
            fgcolor: '#000000',
            email: 'project@company.com'
        },
        {
            url: 'https://project.example.com/2'
            bgcolor: '#eeffee'
            fgcolor: '#134f00',
            email: 'project@company.com'
        }
    ]
}

// done
console.log(type.check(data, rule))
```

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