Skip to content

Commit 791c551

Browse files
committed
Merge pull request #567 from IlyaDiallo/master
Accept subdocuments keys ("object.subobject"), to allow atomic updates of an object field.
2 parents 3b1165b + 193f368 commit 791c551

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

spec/RestCreate.spec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// These tests check the "create" functionality of the REST API.
1+
// These tests check the "create" / "update" functionality of the REST API.
22
var auth = require('../src/Auth');
33
var cache = require('../src/cache');
44
var Config = require('../src/Config');
@@ -41,6 +41,38 @@ describe('rest create', () => {
4141
});
4242
});
4343

44+
it('handles object and subdocument', (done) => {
45+
var obj = {
46+
subdoc: {foo: 'bar', wu: 'tan'},
47+
};
48+
rest.create(config, auth.nobody(config), 'MyClass', obj).then(() => {
49+
return database.mongoFind('MyClass', {}, {});
50+
}).then((results) => {
51+
expect(results.length).toEqual(1);
52+
var mob = results[0];
53+
expect(typeof mob.subdoc).toBe('object');
54+
expect(mob.subdoc.foo).toBe('bar');
55+
expect(mob.subdoc.wu).toBe('tan');
56+
expect(typeof mob._id).toEqual('string');
57+
58+
var obj = {
59+
'subdoc.wu': 'clan',
60+
};
61+
62+
rest.update(config, auth.nobody(config), 'MyClass', mob._id, obj).then(() => {
63+
return database.mongoFind('MyClass', {}, {});
64+
}).then((results) => {
65+
expect(results.length).toEqual(1);
66+
var mob = results[0];
67+
expect(typeof mob.subdoc).toBe('object');
68+
expect(mob.subdoc.foo).toBe('bar');
69+
expect(mob.subdoc.wu).toBe('clan');
70+
done();
71+
});
72+
73+
});
74+
});
75+
4476
it('handles user signup', (done) => {
4577
var user = {
4678
username: 'asdf',

spec/Schema.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ describe('Schema', () => {
2929
});
3030
});
3131

32+
it('can validate one object with dot notation', (done) => {
33+
config.database.loadSchema().then((schema) => {
34+
return schema.validateObject('TestObjectWithSubDoc', {x: false, y: 'YY', z: 1, 'aObject.k1': 'newValue'});
35+
}).then((schema) => {
36+
done();
37+
}, (error) => {
38+
fail(error);
39+
done();
40+
});
41+
});
42+
3243
it('can validate two objects in a row', (done) => {
3344
config.database.loadSchema().then((schema) => {
3445
return schema.validateObject('Foo', {x: true, y: 'yyy', z: 0});

src/Schema.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ Schema.prototype.validateField = function(className, key, type, freeze) {
426426
// Just to check that the key is valid
427427
transform.transformKey(this, className, key);
428428

429+
if( key.indexOf(".") > 0 ) {
430+
// subdocument key (x.y) => ok if x is of type 'object'
431+
key = key.split(".")[ 0 ];
432+
type = 'object';
433+
}
434+
429435
var expected = this.data[className][key];
430436
if (expected) {
431437
expected = (expected === 'map' ? 'object' : expected);

0 commit comments

Comments
 (0)