# validatoo

> a tool to validate an nested object. (based on schema)

Latest version **0.1.3** (published 2015-08-12) · MIT license · 0 weekly downloads

## Install

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

## 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.1.3 |
| Published | 2015-08-12 |
| First published | 2015-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | ishiduca@gmail.com |
| Maintainers | ishiduca |
| Keywords | validate, validator, schema |

## Links

- npm: https://www.npmjs.com/package/validatoo
- Repository: https://github.com/ishiduca/validatoo
- Issues: https://github.com/ishiduca/validatoo/issues
- npm.io page: https://npm.io/package/validatoo

## Dependencies (2)

- [array-reduce](https://npm.io/package/array-reduce.md) 0.0.0
- [component-bind](https://npm.io/package/component-bind.md) ^1.0.0

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

- 0.1.3 (latest) — 2015-08-12
- 0.1.2 — 2015-05-30
- 0.1.1 — 2015-05-30
- 0.1.0 — 2015-05-29
- 0.0.4 — 2015-04-07
- 0.0.3 — 2015-04-07
- 0.0.2 — 2015-04-07
- 0.0.1 — 2015-04-07

## README

# validatoo

a tool to validate an nested object. (based on schema)

## usage

```js
var validatoo = require('validatoo')
var schema    = validatoo.schema
var validator = validatoo.validator

var s = schema({
    required: validator(
        function (v) { return v > 0 && v === parseInt(v, 10) }
      , '"required" key is required and must be an "Interger"'
    )
}, {
    optional: validator(
        /^\w{4,8}$/
      , '"optional" key is optional and must be 4~8 charactors'
    )
})

try {
    s({required: 3.14})
} catch (err) {
    // "required" key is required and must be an "Interger"
}

try {
    s({required: 10, optional: 'foo'})
} catch (err) {
    // "optional" key is optional and must be 4~8 charactors
}

```

## api

### var validate = validatoo.validator(RegExpOrFunc[,errorMessage])

return a validated function

* **RegExpOrFunc** __RegExp__ | __function__ in order to validate
* **errorMessage** __String__ | __function__ error message

```js
var validate = validatoo.validator({
    /^\d{3}$/
  , 'ex : "123", "001"'
})

try {
    validate('01a')
} catch (err) {
    // ex: "123", "001"
}
```

### var schema = validatoo.schema(required[,optional])

return a schema function

* **required** __Object__ collection of validated function of the required elements
* **optional** __Object__ collection of validated function of the optional elements

```js
var scheme = validatoo.schema({
    id: validatoo.validator(
        /^[0-9a-zA-Z-]+$/
      , '"id" key is required'
    )
  , name: validatoo.validator(
        function (v) { return 'string' === typeof v && v.trim().length > 0 }
      , '"name" key is required'
    )
}, {
    nickName: validatoo.validator(
        function (v) { return 'string' === typeof v && v.trim().length > 0 }
      , '"nickName" key is optional.'
    )
})

try {
    schema({
        id: '451da500-dccb-11e4-b0aa-7b2099ec21e8'
      , nickName: 'bimota'
    })
} catch (err) {
    // SchemaRequiredKeyNotFoundError: required key "name" not found
}
```

## complex example

```js
var address = validatoo.schema({
    country: validatoo.validator(
        /^\w+$/
      , '"address.country" is required'
    )
  , city: validatoo.validator(
        /^\w+$/
      , '"address.city" is required'
    )
})

var user = validatoo.schema({
    firstName:  validatoo.validator(/^\w+$/)
  , familyName: validatoo.validator(/^\w+$/)
}, {
    address: address
})


try {
    user({
        firstName: 'John'
      , familyName: 'Moo'
    })
} catch (err) {
    // no error
}

try {
    user({
        firstName: 'John'
      , familyName: 'Moo'
      , address: {
            city: 'magicCity'
        }
    })
} catch (err) {
    // "address.country" is required
}
```

## test

```
$ npm test
```

### for browser

```
$ npm run testling
```


## author

ishiduca@gmail.com

## license

MIT

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