@techdecaf/model

0.0.5 • Public • Published

@techdecaf/model

object modeling framework based on joi

Contents

Requirements

Installation

npm install --save @techdecaf/model

Joi Documentation

Please see Joi's Documentation to reference schema creation

Usage

Basic

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

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

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() parameter to all of your keys

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

Readme

Keywords

none

Package Sidebar

Install

npm i @techdecaf/model

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

13.8 kB

Total Files

13

Last publish

Collaborators

  • techdecafceo