Skip to content

add invalid __type check #674

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions spec/ParseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ describe('Parse.Object testing', () => {
'Item should not be updated with invalid key.');
item.save({ "foo^bar": "baz" }).then(fail, done);
});

it("invalid __type", function(done) {
var item = new Parse.Object("Item");
item.save({ "foo": {__type: "IvalidName"} }).then(fail, done);
});

it("simple field deletion", function(done) {
var simple = new Parse.Object("SimpleObject");
Expand Down
55 changes: 38 additions & 17 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,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 @@ -630,15 +630,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 @@ -725,19 +725,40 @@ 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.INVALID_POINTER, JSON.stringify(obj) + " is not a valid Pointer");
}
break;
case 'File' :
if(obj.url && obj.name) {
Copy link
Contributor

Choose a reason for hiding this comment

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

name is actually the only required field on a file. In most cases, the url can & should be constructed during read. We were actually discussing making url required earlier today, but decided against it for now.

return 'file';
} else {
let msg = obj.name? JSON.stringify(obj) + " is not a valid File" : "File has no name";
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, msg);
}
break;
case 'Date' :
if(obj.iso) {
return 'date';
} else {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, JSON.stringify(obj) + " is not a valid Date");
}
break;
case 'GeoPoint' :
if(obj.latitude != null && obj.longitude != null) {
return 'geopoint';
} else {
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, JSON.stringify(obj) + " is not a valid GeoPoint");
}
break;
default :
Copy link
Contributor

Choose a reason for hiding this comment

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

Bytes is also a valid type if it contains a base64 key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Excuse me, how can I test it by myself?
"npm test" always failed.

throw new Parse.Error(Parse.Error.INCORRECT_TYPE, 'invalid type: ' + obj.__type);
}
}
if (obj['$ne']) {
return getObjectType(obj['$ne']);
Expand Down