Skip to content

Commit e376f10

Browse files
authored
fix(Object): add reserved keys, remove id alias (#394)
1 parent cd1410f commit e376f10

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/file.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,6 @@ module.exports = function(AV) {
456456
get: function(attrName) {
457457
switch (attrName) {
458458
case 'objectId':
459-
case 'id':
460459
return this.id;
461460
case 'url':
462461
case 'name':

src/object.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ const AVError = require('./error');
33
const AVRequest = require('./request').request;
44
const utils = require('./utils');
55

6+
const RESERVED_KEYS = ['objectId', 'createdAt', 'updatedAt'];
7+
const checkReservedKey = key => {
8+
if (RESERVED_KEYS.indexOf(key) !== -1) {
9+
throw new Error(`key[${key}] is reserved`);
10+
}
11+
};
12+
613
// AV.Object is analogous to the Java AVObject.
714
// It also implements the same interface as a Backbone model.
815

@@ -289,7 +296,6 @@ module.exports = function(AV) {
289296
get: function(attr) {
290297
switch (attr) {
291298
case 'objectId':
292-
case 'id':
293299
return this.id;
294300
case 'createdAt':
295301
case 'updatedAt':
@@ -355,16 +361,16 @@ module.exports = function(AV) {
355361
_mergeMagicFields: function(attrs) {
356362
// Check for changes of magic fields.
357363
var model = this;
358-
var specialFields = ["id", "objectId", "createdAt", "updatedAt"];
364+
var specialFields = ["objectId", "createdAt", "updatedAt"];
359365
AV._arrayEach(specialFields, function(attr) {
360366
if (attrs[attr]) {
361367
if (attr === "objectId") {
362368
model.id = attrs[attr];
363369
} else if ((attr === "createdAt" || attr === "updatedAt") &&
364370
!_.isDate(attrs[attr])) {
365371
model[attr] = AV._parseDate(attrs[attr]);
366-
} else {
367-
model[attr] = attrs[attr];
372+
} else {
373+
model[attr] = attrs[attr];
368374
}
369375
delete attrs[attr];
370376
}
@@ -604,11 +610,13 @@ module.exports = function(AV) {
604610
if (_.isObject(key) || utils.isNullOrUndefined(key)) {
605611
attrs = key;
606612
AV._objectEach(attrs, function(v, k) {
613+
checkReservedKey(k);
607614
attrs[k] = AV._decode(k, v);
608615
});
609616
options = value;
610617
} else {
611618
attrs = {};
619+
checkReservedKey(key);
612620
attrs[key] = AV._decode(key, value);
613621
}
614622

test/object.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ describe('Objects', function(){
2525
});
2626
new Post().name;
2727
});
28+
29+
it('reserved keys', () => {
30+
(() => new Person({
31+
objectId: '0'
32+
})).should.throwError(/reserved/);
33+
(() => new Person({
34+
createdAt: '0'
35+
})).should.throwError(/reserved/);
36+
(() => new Person({
37+
updatedAt: '0'
38+
})).should.throwError(/reserved/);
39+
(() => new Person().set('objectId', '1')).should.throwError(/reserved/);
40+
})
2841

2942
describe('#extend', () => {
3043
it('extend for multiple times should not throw', () => {
@@ -69,6 +82,7 @@ describe('Objects', function(){
6982
gameScore.set("playerName", "dd");
7083
gameScore.set("cheatMode", false);
7184
gameScore.set("arr", ["arr1","arr2"]);
85+
gameScore.set('id', 'id');
7286
return gameScore.save().then(function(result) {
7387
expect(result.id).to.be.ok();
7488
objId=result.id;
@@ -107,6 +121,7 @@ describe('Objects', function(){
107121
return query.get(objId).then(function(result) {
108122
expect(gameScore.id).to.be.ok();
109123
expect(gameScore.get('objectId')).to.be(gameScore.id);
124+
expect(gameScore.get('id')).to.be('id');
110125
});
111126
});
112127
});

0 commit comments

Comments
 (0)