bookshelf-modelbase
Advanced tools
Comparing version 2.3.1 to 2.5.0
var extend = require('xtend') | ||
var Joi = require('joi') | ||
var difference = require('lodash.difference') | ||
@@ -31,4 +32,17 @@ module.exports = function modelBase (bookshelf, params) { | ||
validateSave: function () { | ||
var validation = Joi.validate(this.attributes, this.validate) | ||
validateSave: function (model, attrs, options) { | ||
var validation | ||
// model is not new or update method explicitly set | ||
if ((model && !model.isNew()) || (options && options.method === 'update')) { | ||
var schemaKeys = this.validate._inner.children.map(function (child) { | ||
return child.key | ||
}) | ||
var presentKeys = Object.keys(attrs) | ||
var optionalKeys = difference(schemaKeys, presentKeys) | ||
// only validate the keys that are being updated | ||
validation = Joi.validate(attrs, this.validate.optionalKeys(optionalKeys)) | ||
} else { | ||
validation = Joi.validate(this.attributes, this.validate) | ||
} | ||
if (validation.error) { | ||
@@ -35,0 +49,0 @@ throw new Error(validation.error) |
{ | ||
"name": "bookshelf-modelbase", | ||
"version": "2.3.1", | ||
"version": "2.5.0", | ||
"description": "Extensible ModelBase for bookshelf-based model layers", | ||
@@ -20,4 +20,5 @@ "main": "./lib", | ||
"dependencies": { | ||
"bookshelf": "^0.7.9", | ||
"bookshelf": "^0.8.2", | ||
"joi": "^5.0.2", | ||
"lodash.difference": "^3.2.2", | ||
"xtend": "^4.0.0" | ||
@@ -24,0 +25,0 @@ }, |
@@ -22,3 +22,4 @@ /* global describe, before, beforeEach, it */ | ||
validate: { | ||
name: Joi.string().valid('hello', 'goodbye', 'yo') | ||
first_name: Joi.string().valid('hello', 'goodbye', 'yo').required(), | ||
last_name: Joi.string().allow(null) | ||
} | ||
@@ -28,3 +29,3 @@ }) | ||
specimen = new SpecimenClass({ | ||
name: 'hello' | ||
first_name: 'hello' | ||
}) | ||
@@ -54,3 +55,3 @@ | ||
validate: Joi.object().keys({ | ||
name: Joi.string().valid('hello', 'goodbye') | ||
first_name: Joi.string().valid('hello', 'goodbye') | ||
}) | ||
@@ -60,3 +61,3 @@ }) | ||
specimen = new SpecimenClass({ | ||
name: 'hello' | ||
first_name: 'hello' | ||
}) | ||
@@ -72,3 +73,3 @@ | ||
return expect(specimen.validateSave()).to.contain({ | ||
name: 'hello' | ||
first_name: 'hello' | ||
}) | ||
@@ -78,3 +79,3 @@ }) | ||
it('should error on invalid attributes', function () { | ||
specimen.set('name', 1) | ||
specimen.set('first_name', 1) | ||
expect(function () { | ||
@@ -84,2 +85,19 @@ specimen.validateSave() | ||
}) | ||
it('should work with updates method specified', function () { | ||
return SpecimenClass | ||
.where({ first_name: 'hello' }) | ||
.save({ last_name: 'world' }, { patch: true, method: 'update', require: false }) | ||
.then(function (model) { | ||
return expect(model.get('last_name')).to.equal('world') | ||
}) | ||
}) | ||
it('should work with model id specified', function () { | ||
return SpecimenClass.forge({ id: 1 }) | ||
.save({ last_name: 'world' }, { patch: true, require: false }) | ||
.then(function (model) { | ||
return expect(model.get('last_name')).to.equal('world') | ||
}) | ||
}) | ||
}) | ||
@@ -89,3 +107,3 @@ | ||
it('should itself be extensible', function () { | ||
return expect(ModelBase.extend({ tableName: 'test' })) | ||
return expect(ModelBase.extend({ tablefirst_name: 'test' })) | ||
.to.itself.respondTo('extend') | ||
@@ -116,3 +134,3 @@ }) | ||
return SpecimenClass.create({ | ||
name: 'hello' | ||
first_name: 'hello' | ||
}) | ||
@@ -127,5 +145,5 @@ .then(function (model) { | ||
it('should return a model', function () { | ||
expect(specimen.get('name')).to.not.eql('goodbye') | ||
expect(specimen.get('first_name')).to.not.eql('goodbye') | ||
return SpecimenClass.update({ | ||
name: 'goodbye' | ||
first_name: 'goodbye' | ||
}, { | ||
@@ -136,3 +154,3 @@ id: specimen.get('id') | ||
expect(model.get('id')).to.eql(specimen.get('id')) | ||
expect(model.get('name')).to.eql('goodbye') | ||
expect(model.get('first_name')).to.eql('goodbye') | ||
}) | ||
@@ -143,3 +161,3 @@ }) | ||
return SpecimenClass.update({ | ||
name: 'goodbye' | ||
first_name: 'goodbye' | ||
}, { | ||
@@ -157,5 +175,5 @@ id: -1, | ||
it('should destroy the model', function () { | ||
return SpecimenClass.forge({ name: 'hello' }) | ||
return SpecimenClass.forge({ first_name: 'hello' }) | ||
.save() | ||
.bind({}) | ||
.save() | ||
.then(function (model) { | ||
@@ -184,3 +202,3 @@ this.modelId = model.id | ||
return SpecimenClass.findOrCreate({ | ||
name: 'yo' | ||
first_name: 'yo' | ||
}) | ||
@@ -187,0 +205,0 @@ .then(function (model) { |
@@ -6,3 +6,4 @@ 'use strict' | ||
table.increments('id') | ||
table.string('name') | ||
table.string('first_name').notNullable() | ||
table.string('last_name') | ||
table.timestamps() | ||
@@ -9,0 +10,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
17772
320
4
+ Addedlodash.difference@^3.2.2
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedbookshelf@0.8.2(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedlodash@3.10.1(transitive)
+ Addedlodash._basedifference@3.0.3(transitive)
+ Addedlodash._baseflatten@3.1.4(transitive)
+ Addedlodash._baseindexof@3.1.0(transitive)
+ Addedlodash._cacheindexof@3.0.2(transitive)
+ Addedlodash._createcache@3.1.2(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash.difference@3.2.2(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.restparam@3.6.1(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
- Removedbackbone@1.1.0(transitive)
- Removedbookshelf@0.7.9(transitive)
- Removedlodash@2.4.2(transitive)
- Removedsimple-extend@0.1.0(transitive)
- Removedtrigger-then@0.3.0(transitive)
- Removedunderscore@1.13.7(transitive)
Updatedbookshelf@^0.8.2