Skip to content

Adds more expressive schema mismatch errors #2662

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 1 commit into from
Sep 9, 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
58 changes: 58 additions & 0 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,62 @@ describe('SchemaController', () => {
});
done();
});

it('yields a proper schema mismatch error (#2661)', done => {
let anObject = new Parse.Object('AnObject');
let anotherObject = new Parse.Object('AnotherObject');
let someObject = new Parse.Object('SomeObject');
Parse.Object.saveAll([anObject, anotherObject, someObject]).then(() => {
anObject.set('pointer', anotherObject);
return anObject.save();
}).then(() => {
anObject.set('pointer', someObject);
return anObject.save();
}).then(() => {
fail('shoud not save correctly');
done();
}, (err) => {
expect(err instanceof Parse.Error).toBeTruthy();
expect(err.message).toEqual('schema mismatch for AnObject.pointer; expected Pointer<AnotherObject> but got Pointer<SomeObject>')
done();
});
});

it('yields a proper schema mismatch error bis (#2661)', done => {
let anObject = new Parse.Object('AnObject');
let someObject = new Parse.Object('SomeObject');
Parse.Object.saveAll([anObject, someObject]).then(() => {
anObject.set('number', 1);
return anObject.save();
}).then(() => {
anObject.set('number', someObject);
return anObject.save();
}).then(() => {
fail('shoud not save correctly');
done();
}, (err) => {
expect(err instanceof Parse.Error).toBeTruthy();
expect(err.message).toEqual('schema mismatch for AnObject.number; expected Number but got Pointer<SomeObject>')
done();
});
});

it('yields a proper schema mismatch error ter (#2661)', done => {
let anObject = new Parse.Object('AnObject');
let someObject = new Parse.Object('SomeObject');
Parse.Object.saveAll([anObject, someObject]).then(() => {
anObject.set('pointer', someObject);
return anObject.save();
}).then(() => {
anObject.set('pointer', 1);
return anObject.save();
}).then(() => {
fail('shoud not save correctly');
done();
}, (err) => {
expect(err instanceof Parse.Error).toBeTruthy();
expect(err.message).toEqual('schema mismatch for AnObject.pointer; expected Pointer<SomeObject> but got Number')
done();
});
});
});
9 changes: 8 additions & 1 deletion src/Controllers/SchemaController.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ const dbTypeMatchesObjectType = (dbType, objectType) => {
return false;
}

const typeToString = (type) => {
if (type.targetClass) {
return `${type.type}<${type.targetClass}>`;
}
return `${type.type || type}`;
}

// Stores the entire schema of the app in a weird hybrid format somewhere between
// the mongo format and the Parse format. Soon, this will all be Parse format.
export default class SchemaController {
Expand Down Expand Up @@ -591,7 +598,7 @@ export default class SchemaController {
if (!dbTypeMatchesObjectType(expectedType, type)) {
throw new Parse.Error(
Parse.Error.INCORRECT_TYPE,
`schema mismatch for ${className}.${fieldName}; expected ${expectedType.type || expectedType} but got ${type.type}`
`schema mismatch for ${className}.${fieldName}; expected ${typeToString(expectedType)} but got ${typeToString(type)}`
);
}
return this;
Expand Down