bookshelf-modelbase
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -12,3 +12,13 @@ var _ = require('lodash'); | ||
initialize: function (attrs, options) { | ||
this.validation || (this.validation = Joi.any()); | ||
if (this.validate) { | ||
this.validate = Joi.object(this.validate).keys({ | ||
// id might be number or string, for optimization | ||
id: Joi.any().required(), | ||
createdAt: Joi.date().required(), | ||
updatedAt: Joi.date().required() | ||
}) | ||
} else { | ||
this.validate = Joi.object({ n: Joi.string().required() }); | ||
} | ||
this.validate || (this.validate = Joi.any()); | ||
this.on('saving', this.validateSave); | ||
@@ -20,5 +30,5 @@ }, | ||
validateSave: function () { | ||
var validation = Joi.validate(this.attributes, this.validation); | ||
if (validation.error) { | ||
throw new Error(validation.error); | ||
var validation = Joi.validate(this.attributes, this.validate); | ||
if (validation.errors) { | ||
throw new Error(validation.errors); | ||
} else { | ||
@@ -25,0 +35,0 @@ return validation.value; |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
"main": "./lib", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "istanbul cover _mocha" | ||
"test": "mocha test/**" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/bsiddiqui/bookshelf-modelbase.git" | ||
}, | ||
"author": "Grayson Chao", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/bsiddiqui/bookshelf-modelbase/issues" | ||
}, | ||
"homepage": "https://github.com/bsiddiqui/bookshelf-modelbase", | ||
"dependencies": { | ||
@@ -28,3 +20,2 @@ "bluebird": "^2.3.11", | ||
"chai": "^1.10.0", | ||
"istanbul": "^0.3.5", | ||
"knex": "^0.7.3", | ||
@@ -31,0 +22,0 @@ "mocha": "^2.0.1", |
@@ -1,3 +0,3 @@ | ||
ModelBase [![Build Status](https://travis-ci.org/bsiddiqui/bookshelf-modelbase.svg?branch=master)](https://travis-ci.org/bsiddiqui/bookshelf-modelbase) [![Code Climate](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase/badges/gpa.svg)](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase) [![Test Coverage](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase/badges/coverage.svg)](https://codeclimate.com/github/bsiddiqui/bookshelf-modelbase) [![npm version](https://badge.fury.io/js/bookshelf-modelbase.svg)](http://badge.fury.io/js/bookshelf-modelbase) | ||
========== | ||
ModelBase ![Build Status](https://travis-ci.org/bsiddiqui/bookshelf-modelbase.svg?branch=master) | ||
========= | ||
@@ -41,4 +41,7 @@ ##Why | ||
}, { | ||
validation: <Joi validation object, defaults to Joi.any()> | ||
// validation is passed to Joi.object(), so use a raw object | ||
validation: { | ||
firstName: Joi.string() | ||
} | ||
}) | ||
``` |
var Joi = require('joi'); | ||
var sinon = require('sinon'); | ||
var mocha = require('mocha'); | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
var chai = require('chai'), | ||
expect = chai.expect; | ||
var db = require('./db'); | ||
var bookshelf = require('bookshelf')(db); | ||
var ModelBase = require('../lib/index')(bookshelf); | ||
bookshelf = require('bookshelf')(db); | ||
ModelBase = require('../lib/index')(bookshelf); | ||
describe('modelBase', function () { | ||
describe('model base', function () { | ||
var specimen; | ||
beforeEach(function () { | ||
specimenClass = ModelBase.extend({ | ||
var specimenClass = ModelBase.extend({}, { | ||
validation: { | ||
a: Joi.string().valid('test') | ||
} | ||
}); | ||
specimen = new ModelBase({ name: 'hello' }, { | ||
validation: { name: Joi.string().valid('hello') } | ||
}); | ||
specimen = new specimenClass({ name: 'hello' }); | ||
}); | ||
describe('initialize', function () { | ||
it('should error if not passed bookshelf object', function () { | ||
expect(function () { | ||
require('../lib/index')(); | ||
}).to.throw(/Must pass an initialized bookshelf instance/); | ||
}); | ||
it('should default to any validation', function () { | ||
specimen = new ModelBase(); | ||
expect(specimen.validation.isJoi).to.eql(true); | ||
expect(specimen.validation._type).to.eql('any'); | ||
}); | ||
}) | ||
describe('parse', function () { | ||
it('should convert snake case to camel case', function () { | ||
expect(specimen.parse({ variable_name: 'snake_case' })) | ||
return expect(specimen.parse({ variable_name: 'snake_case' })) | ||
.to.eql({ variableName: 'snake_case' }) | ||
@@ -43,3 +33,3 @@ }); | ||
it('should convert camel case to snake case', function () { | ||
expect(specimen.format({ variableName: 'snake_case' })) | ||
return expect(specimen.format({ variableName: 'snake_case' })) | ||
.to.eql({ variable_name: 'snake_case' }) | ||
@@ -51,12 +41,8 @@ }); | ||
it('should validate own attributes', function () { | ||
expect(specimen.validateSave()).to.contain({ | ||
return expect(specimen.validateSave()).to.contain({ | ||
name: 'hello' | ||
}); | ||
}); | ||
it('should error on invalid attributes', function () { | ||
specimen.set('name', 1); | ||
expect(function () { | ||
specimen.validateSave(); | ||
}).to.throw(/ValidationError/); | ||
return expect(specimen.validateSave).to.throw() | ||
}); | ||
@@ -67,3 +53,3 @@ }); | ||
it('should itself be extensible', function () { | ||
expect(ModelBase.extend({ tableName: 'test' })) | ||
return expect(ModelBase.extend({ tableName: 'test' })) | ||
.to.itself.respondTo('extend'); | ||
@@ -70,0 +56,0 @@ }); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
47
6426
9
112
1
1
1