Merge pull request #710 from sdf611097/typeChecking
Add __type checking
This commit is contained in:
@@ -336,6 +336,34 @@ describe('Parse.Object testing', () => {
|
|||||||
item.save({ "foo^bar": "baz" }).then(fail, done);
|
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) {
|
it("simple field deletion", function(done) {
|
||||||
var simple = new Parse.Object("SimpleObject");
|
var simple = new Parse.Object("SimpleObject");
|
||||||
simple.save({
|
simple.save({
|
||||||
|
|||||||
@@ -699,19 +699,44 @@ function getObjectType(obj) {
|
|||||||
if (obj instanceof Array) {
|
if (obj instanceof Array) {
|
||||||
return 'array';
|
return 'array';
|
||||||
}
|
}
|
||||||
if (obj.__type === 'Pointer' && obj.className) {
|
if (obj.__type){
|
||||||
return '*' + obj.className;
|
switch(obj.__type) {
|
||||||
}
|
case 'Pointer' :
|
||||||
if (obj.__type === 'File' && obj.name) {
|
if(obj.className) {
|
||||||
return 'file';
|
return '*' + obj.className;
|
||||||
}
|
} else {
|
||||||
if (obj.__type === 'Date' && obj.iso) {
|
throw new Parse.Error(Parse.Error.INCORRECT_TYPE, "This is not a valid "+obj.__type);
|
||||||
return 'date';
|
}
|
||||||
}
|
break;
|
||||||
if (obj.__type == 'GeoPoint' &&
|
case 'File' :
|
||||||
obj.latitude != null &&
|
if(obj.name) {
|
||||||
obj.longitude != null) {
|
return 'file';
|
||||||
return 'geopoint';
|
} 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (obj['$ne']) {
|
if (obj['$ne']) {
|
||||||
return getObjectType(obj['$ne']);
|
return getObjectType(obj['$ne']);
|
||||||
|
|||||||
Reference in New Issue
Block a user