Skip to content

Commit fc576cb

Browse files
authored
Adds more expressive schema mismatch errors (#2662)
1 parent 364604e commit fc576cb

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

spec/Schema.spec.js

+58
Original file line numberDiff line numberDiff line change
@@ -808,4 +808,62 @@ describe('SchemaController', () => {
808808
});
809809
done();
810810
});
811+
812+
it('yields a proper schema mismatch error (#2661)', done => {
813+
let anObject = new Parse.Object('AnObject');
814+
let anotherObject = new Parse.Object('AnotherObject');
815+
let someObject = new Parse.Object('SomeObject');
816+
Parse.Object.saveAll([anObject, anotherObject, someObject]).then(() => {
817+
anObject.set('pointer', anotherObject);
818+
return anObject.save();
819+
}).then(() => {
820+
anObject.set('pointer', someObject);
821+
return anObject.save();
822+
}).then(() => {
823+
fail('shoud not save correctly');
824+
done();
825+
}, (err) => {
826+
expect(err instanceof Parse.Error).toBeTruthy();
827+
expect(err.message).toEqual('schema mismatch for AnObject.pointer; expected Pointer<AnotherObject> but got Pointer<SomeObject>')
828+
done();
829+
});
830+
});
831+
832+
it('yields a proper schema mismatch error bis (#2661)', done => {
833+
let anObject = new Parse.Object('AnObject');
834+
let someObject = new Parse.Object('SomeObject');
835+
Parse.Object.saveAll([anObject, someObject]).then(() => {
836+
anObject.set('number', 1);
837+
return anObject.save();
838+
}).then(() => {
839+
anObject.set('number', someObject);
840+
return anObject.save();
841+
}).then(() => {
842+
fail('shoud not save correctly');
843+
done();
844+
}, (err) => {
845+
expect(err instanceof Parse.Error).toBeTruthy();
846+
expect(err.message).toEqual('schema mismatch for AnObject.number; expected Number but got Pointer<SomeObject>')
847+
done();
848+
});
849+
});
850+
851+
it('yields a proper schema mismatch error ter (#2661)', done => {
852+
let anObject = new Parse.Object('AnObject');
853+
let someObject = new Parse.Object('SomeObject');
854+
Parse.Object.saveAll([anObject, someObject]).then(() => {
855+
anObject.set('pointer', someObject);
856+
return anObject.save();
857+
}).then(() => {
858+
anObject.set('pointer', 1);
859+
return anObject.save();
860+
}).then(() => {
861+
fail('shoud not save correctly');
862+
done();
863+
}, (err) => {
864+
expect(err instanceof Parse.Error).toBeTruthy();
865+
expect(err.message).toEqual('schema mismatch for AnObject.pointer; expected Pointer<SomeObject> but got Number')
866+
done();
867+
});
868+
});
811869
});

src/Controllers/SchemaController.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ const dbTypeMatchesObjectType = (dbType, objectType) => {
299299
return false;
300300
}
301301

302+
const typeToString = (type) => {
303+
if (type.targetClass) {
304+
return `${type.type}<${type.targetClass}>`;
305+
}
306+
return `${type.type || type}`;
307+
}
308+
302309
// Stores the entire schema of the app in a weird hybrid format somewhere between
303310
// the mongo format and the Parse format. Soon, this will all be Parse format.
304311
export default class SchemaController {
@@ -592,7 +599,7 @@ export default class SchemaController {
592599
if (!dbTypeMatchesObjectType(expectedType, type)) {
593600
throw new Parse.Error(
594601
Parse.Error.INCORRECT_TYPE,
595-
`schema mismatch for ${className}.${fieldName}; expected ${expectedType.type || expectedType} but got ${type.type}`
602+
`schema mismatch for ${className}.${fieldName}; expected ${typeToString(expectedType)} but got ${typeToString(type)}`
596603
);
597604
}
598605
return this;

0 commit comments

Comments
 (0)