bookshelf-modelbase
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -5,3 +5,3 @@ var _ = require('lodash'); | ||
function modelBase(bookshelf, params) { | ||
module.exports = function modelBase(bookshelf, params) { | ||
if (!bookshelf) { | ||
@@ -16,10 +16,10 @@ throw new Error('Must pass an initialized bookshelf instance'); | ||
// id might be number or string, for optimization | ||
id: Joi.any().required(), | ||
createdAt: Joi.date().required(), | ||
updatedAt: Joi.date().required() | ||
id: Joi.any().optional(), | ||
createdAt: Joi.date().optional(), | ||
updatedAt: Joi.date().optional() | ||
}) | ||
} else { | ||
this.validate = Joi.object({ n: Joi.string().required() }); | ||
this.validate = Joi.any(); | ||
} | ||
this.validate || (this.validate = Joi.any()); | ||
this.on('saving', this.validateSave); | ||
@@ -32,4 +32,4 @@ }, | ||
var validation = Joi.validate(this.attributes, this.validate); | ||
if (validation.errors) { | ||
throw new Error(validation.errors); | ||
if (validation.error) { | ||
throw new Error(validation.error); | ||
} else { | ||
@@ -58,4 +58,1 @@ return validation.value; | ||
}; | ||
module.exports = modelBase; | ||
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
"main": "lib/index.js", | ||
"main": "./lib", | ||
"scripts": { | ||
"test": "mocha test/**" | ||
"test": "istanbul cover _mocha" | ||
}, | ||
"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": { | ||
@@ -20,2 +28,3 @@ "bluebird": "^2.3.11", | ||
"chai": "^1.10.0", | ||
"istanbul": "^0.3.5", | ||
"knex": "^0.7.3", | ||
@@ -22,0 +31,0 @@ "mocha": "^2.0.1", |
@@ -1,3 +0,3 @@ | ||
ModelBase ![Build Status](https://travis-ci.org/bsiddiqui/bookshelf-modelbase.svg?branch=master) | ||
========= | ||
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) | ||
========== | ||
@@ -42,3 +42,3 @@ ##Why | ||
// validation is passed to Joi.object(), so use a raw object | ||
validation: { | ||
validate: { | ||
firstName: Joi.string() | ||
@@ -45,0 +45,0 @@ } |
var Joi = require('joi'); | ||
var sinon = require('sinon'); | ||
var mocha = require('mocha'); | ||
var chai = require('chai'), | ||
expect = chai.expect; | ||
var chai = require('chai'); | ||
var expect = chai.expect; | ||
var db = require('./db'); | ||
bookshelf = require('bookshelf')(db); | ||
ModelBase = require('../lib/index')(bookshelf); | ||
var bookshelf = require('bookshelf')(db); | ||
var ModelBase = require('../lib/index')(bookshelf); | ||
describe('model base', function () { | ||
describe('modelBase', function () { | ||
var specimen; | ||
beforeEach(function () { | ||
var specimenClass = ModelBase.extend({}, { | ||
validation: { | ||
a: Joi.string().valid('test') | ||
} | ||
specimenClass = ModelBase.extend({ | ||
validate: { name: Joi.string().valid('hello') } | ||
}); | ||
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.validate.isJoi).to.eql(true); | ||
expect(specimen.validate._type).to.eql('any'); | ||
}); | ||
}) | ||
describe('parse', function () { | ||
@@ -43,5 +56,9 @@ it('should convert snake case to camel case', function () { | ||
}); | ||
}); | ||
it('should error on invalid attributes', function () { | ||
specimen.set('name', 1); | ||
return expect(specimen.validateSave).to.throw() | ||
expect(function () { | ||
specimen.validateSave(); | ||
}).to.throw(/ValidationError/); | ||
}); | ||
@@ -48,0 +65,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
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
8863
10
124
0
0
0
6