Skip to content

Commit 4b7447e

Browse files
committed
Merge pull request #710 from sdf611097/typeChecking
Add __type checking
2 parents 818fe49 + 36aa593 commit 4b7447e

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

.DS_Store

8 KB
Binary file not shown.

spec/ParseObject.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,34 @@ describe('Parse.Object testing', () => {
336336
item.save({ "foo^bar": "baz" }).then(fail, done);
337337
});
338338

339+
it("invalid __type", function(done) {
340+
var item = new Parse.Object("Item");
341+
var types = ['Pointer', 'File', 'Date', 'GeoPoint', 'Bytes'];
342+
var Error = Parse.Error;
343+
var tests = types.map(type => {
344+
var test = new Parse.Object("Item");
345+
test.set('foo', {
346+
__type: type
347+
});
348+
return test;
349+
});
350+
var next = function(index) {
351+
if (index < tests.length) {
352+
tests[index].save().then(fail, error => {
353+
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
354+
next(index + 1);
355+
});
356+
} else {
357+
done();
358+
}
359+
}
360+
item.save({
361+
"foo": {
362+
__type: "IvalidName"
363+
}
364+
}).then(fail, err => next(0));
365+
});
366+
339367
it("simple field deletion", function(done) {
340368
var simple = new Parse.Object("SimpleObject");
341369
simple.save({

src/Schema.js

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ Schema.prototype.validateRequiredColumns = function(className, object, query) {
594594
if (!columns || columns.length == 0) {
595595
return Promise.resolve(this);
596596
}
597-
597+
598598
var missingColumns = columns.filter(function(column){
599599
if (query && query.objectId) {
600600
if (object[column] && typeof object[column] === "object") {
@@ -604,15 +604,15 @@ Schema.prototype.validateRequiredColumns = function(className, object, query) {
604604
// Not trying to do anything there
605605
return false;
606606
}
607-
return !object[column]
607+
return !object[column]
608608
});
609-
609+
610610
if (missingColumns.length > 0) {
611611
throw new Parse.Error(
612612
Parse.Error.INCORRECT_TYPE,
613613
missingColumns[0]+' is required.');
614614
}
615-
615+
616616
return Promise.resolve(this);
617617
}
618618

@@ -699,19 +699,44 @@ function getObjectType(obj) {
699699
if (obj instanceof Array) {
700700
return 'array';
701701
}
702-
if (obj.__type === 'Pointer' && obj.className) {
703-
return '*' + obj.className;
704-
}
705-
if (obj.__type === 'File' && obj.name) {
706-
return 'file';
707-
}
708-
if (obj.__type === 'Date' && obj.iso) {
709-
return 'date';
710-
}
711-
if (obj.__type == 'GeoPoint' &&
712-
obj.latitude != null &&
713-
obj.longitude != null) {
714-
return 'geopoint';
702+
if (obj.__type){
703+
switch(obj.__type) {
704+
case 'Pointer' :
705+
if(obj.className) {
706+
return '*' + obj.className;
707+
} else {
708+
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
709+
}
710+
break;
711+
case 'File' :
712+
if(obj.name) {
713+
return 'file';
714+
} else {
715+
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
716+
}
717+
break;
718+
case 'Date' :
719+
if(obj.iso) {
720+
return 'date';
721+
} else {
722+
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
723+
}
724+
break;
725+
case 'GeoPoint' :
726+
if(obj.latitude != null && obj.longitude != null) {
727+
return 'geopoint';
728+
} else {
729+
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
730+
}
731+
break;
732+
case 'Bytes' :
733+
if(!obj.base64) {
734+
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
735+
}
736+
break;
737+
default :
738+
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, 'invalid type: ' + obj.__type);
739+
}
715740
}
716741
if (obj['$ne']) {
717742
return getObjectType(obj['$ne']);

0 commit comments

Comments
 (0)