Skip to content

Add __type checking #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
28 changes: 28 additions & 0 deletions spec/ParseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,34 @@ describe('Parse.Object testing', () => {
item.save({ "foo^bar": "baz" }).then(fail, done);
});

it("invalid __type", function(done) {
var item = new Parse.Object("Item");
var types = ['Pointer', 'File', 'Date', 'GeoPoint', 'Bytes'];
var Error = Parse.Error;
var tests = types.map(type => {
var test = new Parse.Object("Item");
test.set('foo', {
__type: type
});
return test;
});
var next = function(index) {
if (index < tests.length) {
tests[index].save().then(fail, error => {
expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE);
next(index + 1);
});
} else {
done();
}
}
item.save({
"foo": {
__type: "IvalidName"
}
}).then(fail, err => next(0));
});

it("simple field deletion", function(done) {
var simple = new Parse.Object("SimpleObject");
simple.save({
Expand Down
59 changes: 42 additions & 17 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ Schema.prototype.validateRequiredColumns = function(className, object, query) {
if (!columns || columns.length == 0) {
return Promise.resolve(this);
}

var missingColumns = columns.filter(function(column){
if (query && query.objectId) {
if (object[column] && typeof object[column] === "object") {
Expand All @@ -636,15 +636,15 @@ Schema.prototype.validateRequiredColumns = function(className, object, query) {
// Not trying to do anything there
return false;
}
return !object[column]
return !object[column]
});

if (missingColumns.length > 0) {
throw new Parse.Error(
Parse.Error.INCORRECT_TYPE,
missingColumns[0]+' is required.');
}

return Promise.resolve(this);
}

Expand Down Expand Up @@ -731,19 +731,44 @@ function getObjectType(obj) {
if (obj instanceof Array) {
return 'array';
}
if (obj.__type === 'Pointer' && obj.className) {
return '*' + obj.className;
}
if (obj.__type === 'File' && obj.name) {
return 'file';
}
if (obj.__type === 'Date' && obj.iso) {
return 'date';
}
if (obj.__type == 'GeoPoint' &&
obj.latitude != null &&
obj.longitude != null) {
return 'geopoint';
if (obj.__type){
switch(obj.__type) {
case 'Pointer' :
if(obj.className) {
return '*' + obj.className;
} else {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
}
break;
case 'File' :
if(obj.name) {
return 'file';
} else {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
}
break;
case 'Date' :
if(obj.iso) {
return 'date';
} else {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
}
break;
case 'GeoPoint' :
if(obj.latitude != null && obj.longitude != null) {
return 'geopoint';
} else {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
}
break;
case 'Bytes' :
if(!obj.base64) {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
}
break;
default :
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, 'invalid type: ' + obj.__type);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as each case returns, you could just add after the switch throw new Parse.Error(Parse.Error.INCORRECT_TYPE, 'invalid type: ' + obj.__type);

}
}
if (obj['$ne']) {
return getObjectType(obj['$ne']);
Expand Down