avery
v0.3.0
Published
Immutable models with virtuals and validation.
Downloads
16
Readme
Avery
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 : '[email protected]',
password : 'supersecret',
firstName : 'Sterling',
lastName : 'Archer'
});
myUser.get('fullName'); // === "Sterling Archer"
myUser.get('email'); // === "[email protected]"
myUser.set('id', 2);
myUser.get('id'); // === 1
var mutatedUser = myUser.set('id', 2);
myUser === mutatedUser; // === false
mutatedUser.get('id'); // === 2