Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit e98ab50

Browse files
authored
fix: check for plain object before Object.keys call (#27)
* fix: check for plain object before Object.keys call * fix: tests * chore: version bump
1 parent 1fa8b6f commit e98ab50

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed

lib/model/model.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const debug = require('debug')('requelize:model')
22

33
const Joi = require('joi')
4+
const isPlainObject = require('lodash.isplainobject')
45
const Query = require('./query')
56
const RequelizeError = require('../error')
67
const validateSchema = require('./util/validateSchema')
@@ -51,9 +52,11 @@ function createModel (requelize) {
5152
hiddenProp(this, '_validate', true)
5253
hiddenProp(this, '_saved', false)
5354

54-
// Set initial values (must be after getters/setters)
55-
for (const key of Object.keys(initialData)) {
56-
this[key] = initialData[key]
55+
if (isPlainObject(initialData)) {
56+
// Set initial values (must be after getters/setters)
57+
for (const key of Object.keys(initialData)) {
58+
this[key] = initialData[key]
59+
}
5760
}
5861

5962
debug(`instanciating ${Model._name} with data ${JSON.stringify(this._data)}`)

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "requelize",
3-
"version": "0.9.3",
3+
"version": "0.9.4",
44
"description": "RethinkDB ORM",
55
"main": "index.js",
66
"repository": "https://github.com/buckless/requelize.git",
@@ -18,6 +18,7 @@
1818
},
1919
"dependencies": {
2020
"debug": "^2.6.3",
21+
"lodash.isplainobject": "^4.0.6",
2122
"rethinkdbdash": "^2.3.28",
2223
"rxjs": "^5.2.0"
2324
},

test/test.issue26.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { test, requelize } = require('./utils')
2+
3+
test('Issue #26', (t) => {
4+
t.plan(1)
5+
6+
const Foo = requelize.model('foo')
7+
8+
try {
9+
let foo = new Foo(null)
10+
11+
t.equal(true, foo.hasOwnProperty('id'), 'instance is created with null as initialData')
12+
} catch (err) {
13+
t.fail(err)
14+
}
15+
})

yarn.lock

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,6 @@ [email protected]:
821821
version "2.16.3"
822822
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"
823823

824-
825-
version "4.1.0"
826-
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.1.0.tgz#4a4557460f69842ed463aa00628cc26d2683afa7"
827-
828824
home-or-tmp@^2.0.0:
829825
version "2.0.0"
830826
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
@@ -992,10 +988,6 @@ isarray@^1.0.0, isarray@~1.0.0:
992988
version "1.0.0"
993989
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
994990

995-
996-
version "2.2.1"
997-
resolved "https://registry.yarnpkg.com/isemail/-/isemail-2.2.1.tgz#0353d3d9a62951080c262c2aa0a42b8ea8e9e2a6"
998-
999991
isexe@^1.1.1:
1000992
version "1.1.2"
1001993
resolved "https://registry.yarnpkg.com/isexe/-/isexe-1.1.2.tgz#36f3e22e60750920f5e7241a476a8c6a42275ad0"
@@ -1023,25 +1015,12 @@ istanbul@^0.4.5:
10231015
which "^1.1.1"
10241016
wordwrap "^1.0.0"
10251017

1026-
1027-
version "2.1.1"
1028-
resolved "https://registry.yarnpkg.com/items/-/items-2.1.1.tgz#8bd16d9c83b19529de5aea321acaada78364a198"
1029-
10301018
jodid25519@^1.0.0:
10311019
version "1.0.2"
10321020
resolved "https://registry.yarnpkg.com/jodid25519/-/jodid25519-1.0.2.tgz#06d4912255093419477d425633606e0e90782967"
10331021
dependencies:
10341022
jsbn "~0.1.0"
10351023

1036-
joi@^10.2.2:
1037-
version "10.2.2"
1038-
resolved "https://registry.yarnpkg.com/joi/-/joi-10.2.2.tgz#dc5a792b7b4c6fffa562242a95b55d9d3f077e24"
1039-
dependencies:
1040-
hoek "4.x.x"
1041-
isemail "2.x.x"
1042-
items "2.x.x"
1043-
topo "2.x.x"
1044-
10451024
js-tokens@^3.0.0:
10461025
version "3.0.1"
10471026
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
@@ -1124,6 +1103,10 @@ load-json-file@^1.0.0:
11241103
pinkie-promise "^2.0.0"
11251104
strip-bom "^2.0.0"
11261105

1106+
lodash.isplainobject@^4.0.6:
1107+
version "4.0.6"
1108+
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
1109+
11271110
lodash@^4.0.0, lodash@^4.3.0:
11281111
version "4.17.4"
11291112
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
@@ -1784,12 +1767,6 @@ through@^2.3.6, through@~2.3.4, through@~2.3.8:
17841767
version "2.3.8"
17851768
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
17861769

1787-
1788-
version "2.0.2"
1789-
resolved "https://registry.yarnpkg.com/topo/-/topo-2.0.2.tgz#cd5615752539057c0dc0491a621c3bc6fbe1d182"
1790-
dependencies:
1791-
hoek "4.x.x"
1792-
17931770
tough-cookie@~2.3.0:
17941771
version "2.3.2"
17951772
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"

0 commit comments

Comments
 (0)