diff --git a/Schema.js b/Schema.js index b0ea4d81..3f590ac7 100644 --- a/Schema.js +++ b/Schema.js @@ -100,9 +100,6 @@ function fieldNameIsValidForClass(fieldName, className) { } function invalidClassNameMessage(className) { - if (!className) { - className = ''; - } return 'Invalid classname: ' + className + ', classnames can only have alphanumeric characters and _, and must start with an alpha character '; } @@ -137,7 +134,7 @@ function schemaAPITypeToMongoFieldType(type) { return { error: "invalid JSON", code: Parse.Error.INVALID_JSON }; } switch (type.type) { - default: return { error: 'invalid field type: ' + type.type }; + default: return { error: 'invalid field type: ' + type.type, code: Parse.Error.INCORRECT_TYPE }; case 'Number': return { result: 'number' }; case 'String': return { result: 'string' }; case 'Boolean': return { result: 'boolean' }; @@ -211,10 +208,10 @@ Schema.prototype.reload = function() { // enabled) before calling this function. Schema.prototype.addClassIfNotExists = function(className, fields) { if (this.data[className]) { - return Promise.reject(new Parse.Error( - Parse.Error.DUPLICATE_VALUE, - 'class ' + className + ' already exists' - )); + return Promise.reject({ + code: Parse.Error.INVALID_CLASS_NAME, + error: 'class ' + className + ' already exists', + }); } if (!classNameIsValid(className)) { @@ -222,9 +219,6 @@ Schema.prototype.addClassIfNotExists = function(className, fields) { code: Parse.Error.INVALID_CLASS_NAME, error: invalidClassNameMessage(className), }); - return Promise.reject({ - code: Parse.Error.INVALID_CLASS_NAME, - }); } for (fieldName in fields) { if (!fieldNameIsValid(fieldName)) { diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index ce6f5968..a9cd6271 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -155,9 +155,11 @@ describe('Schema', () => { .then(schema => { schema.validateObject('NewClass', {foo: 7}) .then(() => { - schema.addClassIfNotExists('NewClass', { + schema.reload() + .then(schema => schema.addClassIfNotExists('NewClass', { foo: {type: 'String'} - }).catch(error => { + })) + .catch(error => { expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME) expect(error.error).toEqual('class NewClass already exists'); done(); @@ -223,7 +225,17 @@ describe('Schema', () => { }); }); - it('refuses to explicitly create the default fields', done => { + it('refuses to explicitly create the default fields for custom classes', done => { + config.database.loadSchema() + .then(schema => schema.addClassIfNotExists('NewClass', {objectId: {type: 'String'}})) + .catch(error => { + expect(error.code).toEqual(136); + expect(error.error).toEqual('field objectId cannot be added'); + done(); + }); + }); + + it('refuses to explicitly create the default fields for non-custom classes', done => { config.database.loadSchema() .then(schema => schema.addClassIfNotExists('_Installation', {localeIdentifier: {type: 'String'}})) .catch(error => { @@ -317,6 +329,18 @@ describe('Schema', () => { }); }); + it('refuses to add fields with unknown types', done => { + config.database.loadSchema() + .then(schema => schema.addClassIfNotExists('NewClass', { + foo: {type: 'Unknown'}, + })) + .catch(error => { + expect(error.code).toEqual(Parse.Error.INCORRECT_TYPE); + expect(error.error).toEqual('invalid field type: Unknown'); + done(); + }); + }); + it('will create classes', done => { config.database.loadSchema() .then(schema => schema.addClassIfNotExists('NewClass', { @@ -352,6 +376,32 @@ describe('Schema', () => { }); }); + it('creates the default fields for non-custom classes', done => { + config.database.loadSchema() + .then(schema => schema.addClassIfNotExists('_Installation', { + foo: {type: 'Number'}, + })) + .then(mongoObj => { + expect(mongoObj).toEqual({ + _id: '_Installation', + createdAt: 'string', + updatedAt: 'string', + objectId: 'string', + foo: 'number', + installationId: 'string', + deviceToken: 'string', + channels: 'array', + deviceType: 'string', + pushType: 'string', + GCMSenderId: 'string', + timeZone: 'string', + localeIdentifier: 'string', + badge: 'number', + }); + done(); + }); + }); + it('refuses to create two geopoints', done => { config.database.loadSchema() .then(schema => schema.addClassIfNotExists('NewClass', {