# typetree

> Object validation using a type tree definition.

Latest version **0.5.2** (published 2019-07-17) · MIT license · 0 weekly downloads

## Install

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

## 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.5.2 |
| Published | 2019-07-17 |
| First published | 2019-05-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | sysaxis |
| Maintainers | sysaxis |
| Keywords | object, validation, compare, definition, type, tree |

## Links

- npm: https://www.npmjs.com/package/typetree
- Repository: https://github.com/sysaxis/js-typetree
- Homepage: https://github.com/sysaxis/js-typetree#readme
- Issues: https://github.com/sysaxis/js-typetree/issues
- npm.io page: https://npm.io/package/typetree

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.5.2 (latest) — 2019-07-17
- 0.5.1 — 2019-07-13
- 0.5.0 — 2019-07-12
- 0.1.0 — 2019-05-31

## README

typetree
======

Object validation using a definition laid out as a type-tree.

## Install
```
npm install typetree
```

## Usage
```js
const {validateObject} = require('typetree')
```

### Defining a typetree
Typetree definitions can be JavaScript built-in objects
```js
Boolean
Number
String
Object
```
or a literal values such as
**null**, **"string literal"**, **12** (number), **undefined** (indicates that the values can be ommited)


Here's a comprehesive sample (validation object for a company):
```js
var validObject = {
    id: Number,
    name: String,
    address: {
        city: String,
        // optional values
        country: ['EE', 'FI', 'SE']
    },
    // an array of objects
    branches: [
        {
            name: String,
            subId: Number
        }
    ],
    // either true, false or can be ommited
    active: [Boolean, undefined],
    // strict value
    type: 'SME'
}
```

### Examples

#### Validating an object
```js
validateObject(validObject, {
    id: 10,
    name: 'John Wick',
    email: 'john.wick@example.com'
})

// result: undefined

validateObject(validObject, {
    id: 10,
    name: null,
})

// result: [
//    'expected String at name',
//    'expected String at email'
// ]
```

#### Validating optional values
```js
var validObject = {
    patient: {
        bloodType: ['A', 'B', 'AB', 'O']
    }
}

validateObject(validObject, {
    patient: {
        bloodType: 'A'
    }
})

// result: undefined

validateObject(validObject, {
    patient: {
        bloodType: 'Z'
    }
})

// result: [ 'expected any from [A, B, AB, O] at patient.bloodType' ]
```

#### Validating array contents
Provide an array with a single element in order to define an array validation object
```js
var validObject = {
    characters: [{filmId: Number, name: String}]
}

validateObject(validObject, { characters: [
    {
        filmId: 10,
        name: 'John Wick'
    },
    {
        filmId: 10
    }
]})

// result: [ 'expected String at characters[1].name' ]
```
In order to validate array contents with optionals, provide an array within an array
```js
var validObject = {
    characters: [[
        {filmId: Number, name: String},
        {deleted: true}
    ]]
}

validateObject(validObject, {
    characters: [
        {
            filmId: 10,
            name: 'John Wick'
        },
        {
            filmId: 10,
            deleted: true
        }
    ]
})

// result: undefined

validateObject(validObject, {
    characters: [
        {
            filmId: 10,
            name: 'John Wick'
        },
        {
            filmId: 10
        }
    ]
})

// result: [ 'expected any from [Object {filmId<Number>, name<String>}, Object {deleted: true}] at characters[1]' ]
```

## Test
```
node test
```

## Licence

MIT

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