Socket
Socket
Sign inDemoInstall

bookshelf-modelbase

Package Overview
Dependencies
Maintainers
2
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-modelbase - npm Package Compare versions

Comparing version 2.3.1 to 2.5.0

18

lib/index.js
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)

5

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc