0.3.0 • Published 9 years ago

avery v0.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Avery

NPM

Immutable models with virtuals and Joi-ful validation.

API

See usage below (it's pretty simple), and then go look at ImmutableJS.

Methods we add:

  • isValid - returns true if the model passed validation. You shouldn't need anything else.

Usage

var Avery = require('avery');

class User extends Avery.Model({
  defaults : {
    id : undefined,
    email : undefined,
    password : undefined,
    firstName : undefined,
    lastName : undefined
  },

  validate : Joi.object().keys({
    id : Joi.number(),
    email : Joi.string().email(),
    password : Joi.string(),
    firstName : Joi.string(),
    lastName : Joi.string()
  }),

  virtuals : {
    fullName : function() {
      // this is bound to the ImmutableJS record
      return this.get('firstName') + ' ' + this.get('lastName');
    }
  }
}) {

  myMethod() {
    return this.get('fullName');
  }

};

var myUser = new User({
  id : 1,
  email : 'archer@example.com',
  password : 'supersecret',
  firstName : 'Sterling',
  lastName : 'Archer'
});

myUser.get('fullName'); // === "Sterling Archer"
myUser.get('email'); // === "archer@example.com"

myUser.set('id', 2);
myUser.get('id'); // === 1

var mutatedUser = myUser.set('id', 2);
myUser === mutatedUser; // === false
mutatedUser.get('id'); // === 2
0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.1

9 years ago