npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@techdecaf/model

v0.0.5

Published

object modeling framework based on joi

Downloads

3

Readme

@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("[email protected]"),
};

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

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

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: '[email protected]' }

let user = new User(example);

console.log(user.validate());
// returns
// User {
//   firstName: 'John',
//   lastName: 'Doe',
//   email: '[email protected]' }

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