# @techdecaf/model

> object modeling framework based on joi

Latest version **0.0.5** (published 2018-03-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @techdecaf/model
pnpm add @techdecaf/model
yarn add @techdecaf/model
bun add @techdecaf/model
```

## 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.5 |
| Published | 2018-03-05 |
| First published | 2018-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | dev@techdecaf.com |
| Maintainers | techdecafceo |

## Links

- npm: https://www.npmjs.com/package/@techdecaf/model
- npm.io page: https://npm.io/package/@techdecaf/model

## Dependencies (2)

- [joi](https://npm.io/package/joi.md) ^13.1.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.4

## Recent versions

- 0.0.5 (latest) — 2018-03-05
- 0.0.4 — 2018-03-03
- 0.0.3 — 2018-03-02

## README

# @techdecaf/model

object modeling framework based on joi

## Contents

- [@decaf/model](#decafmodel)
  - [Contents](#contents)
  - [Requirements](#requirements)
  - [Installation](#installation)
  - [Joi Documentation](#joi-documentation)
  - [Usage](#usage)
    - [Basic](#basic)
    - [ES6 Class Inheritance](#es6-class-inheritance)
    - [Testing and Documentation](#testing-and-documentation)
  - [Development](#development)
  - [Contributing](#contributing)

## Requirements

## Installation

`npm install --save @techdecaf/model`

## Joi Documentation

Please see [Joi's Documentation](https://github.com/hapijs/joi/blob/v13.1.2/API.md#attemptvalue-schema-message) to reference schema creation

## Usage

### Basic

```javascript
const {Model, joi} = require("@techdecaf/model");

defaultEmail = (obj) => {
    return `${obj.firstName}.${obj.lastName}@domain.io`;
};

const UserSchema = {
    firstName: joi.string().required().example("John"),
    lastName: joi.string().required().example("Doe"),
    email: joi.string()
      .email()
      .default(defaultEmail, "email")
      .example("John.Doe@domain.com"),
};

const User = new Model("User", UserSchema);

let user = new User({firstName: "foo", lastName: "bar"});
console.log(user)
// returns
// User {
//   firstName: 'foo',
//   lastName: 'bar',
//   email: 'foo.bar@domain.io'
// }

```

### ES6 Class Inheritance

#### Base Classes

You can pass in a base class which you would like your model to inherit from.  This is useful for extending @techdecaf/model with your own data storage layer or custom methods.  Simply write your own base class and then extend model to include validation.

> *Warning*: If your BaseClass has properties that are not part of your schema then validation will fail unless you also specify `allowUnknown`

```javascript

class BaseClass {
  constructor(){
    this.greeting = "hello";
  }

  greet(){
    return `${this.greeting} ${this.firstName}`;
  }
}

const User = new Model("User", UserSchema, BaseClass);

let user = new User({firstName: "foo", lastName: "bar"});

console.log(user.greet());
// returns "hello foo"

```

#### Extending

> *Note*: if you are going to extend a model adding additional non validated properties will result in an error unless you specify `allowUnknown`

```javascript
const joiOptions = {allowUnknown: true};
const User = new Model("User", UserSchema, null, joiOptions);

class MyUser extends User {
    /**
     * @param {User} user
     */
    constructor(user) {
        super(user);
        this.greeting = "hello";
    }
    /**
     * greeting
     * @return {String}
     */
    greet() {
        return `${this.greeting} ${this.firstName}`;
    }
};

let user = new MyUser({firstName: "foo", lastName: "bar"});

console.log(user.validate().greet());
// returns "hello foo"
```

### Testing and Documentation

> `Models` support generating example object which you can use for documentation or in a test suite.
to supprot the use of example objects, you must provide the [joi.example()](https://github.com/hapijs/joi/blob/v13.1.2/API.md#anyexamplevalue) parameter to all of your keys

```javascript
let example = User.example();

console.log(example);
// returns
// { firstName: 'John',
//   lastName: 'Doe',
//   email: 'John.Doe@domain.com' }

let user = new User(example);

console.log(user.validate());
// returns
// User {
//   firstName: 'John',
//   lastName: 'Doe',
//   email: 'John.Doe@domain.com' }
```

## Development

- deploy: `npm run deploy`
- test: `npm test`

## Contributing

- [ ] Clone this repo 
- [ ] Create a feature branch from dev `git checkout -b myFeature dev`
- [ ] Write your tests
- [ ] Make your suggested changes
- [ ] Submit a pull request

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