Skip to content

Commit f07836e

Browse files
committed
Add validation of deleteField function
1 parent 29d70a6 commit f07836e

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

spec/Schema.spec.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// These tests check that the Schema operates correctly.
21
var Config = require('../src/Config');
32
var Schema = require('../src/Schema');
43
var dd = require('deep-diff');
@@ -406,4 +405,60 @@ describe('Schema', () => {
406405
done();
407406
});
408407
});
408+
409+
it('can check if a class exists', done => {
410+
config.database.loadSchema()
411+
.then(schema => {
412+
return schema.addClassIfNotExists('NewClass', {})
413+
.then(() => {
414+
console.log(Object.getPrototypeOf(schema));
415+
schema.hasClass('NewClass')
416+
.then(hasClass => {
417+
expect(hasClass).toEqual(true);
418+
done();
419+
})
420+
.catch(fail);
421+
422+
schema.hasClass('NonexistantClass')
423+
.then(hasClass => {
424+
expect(hasClass).toEqual(false);
425+
done();
426+
})
427+
.catch(fail);
428+
})
429+
.catch(error => {
430+
fail('Couldn\'t create class');
431+
fail(error);
432+
});
433+
})
434+
.catch(error => fail('Couldn\'t load schema'));
435+
});
436+
437+
it('refuses to delete fields from invalid class names', done => {
438+
config.database.loadSchema()
439+
.then(schema => schema.deleteField('fieldName', 'invalid class name'))
440+
.catch(error => {
441+
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
442+
done();
443+
});
444+
});
445+
446+
it('refuses to delete invalid fields', done => {
447+
config.database.loadSchema()
448+
.then(schema => schema.deleteField('invalid field name', 'ValidClassName'))
449+
.catch(error => {
450+
expect(error.code).toEqual(Parse.Error.INVALID_KEY_NAME);
451+
done();
452+
});
453+
});
454+
455+
it('refuses to delete the default fields', done => {
456+
config.database.loadSchema()
457+
.then(schema => schema.deleteField('installationId', '_Installation'))
458+
.catch(error => {
459+
expect(error.code).toEqual(136);
460+
expect(error.error).toEqual('field installationId cannot be changed');
461+
done();
462+
});
463+
});
409464
});

src/Schema.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,38 @@ Schema.prototype.validateField = function(className, key, type, freeze) {
409409
});
410410
};
411411

412+
// Delete a field, and remove that data from all objects. This is intended
413+
// to remove unused fields, if other writers are writing objects that include
414+
// this field, the field may reappear. Returns a Promise that resolves with
415+
// no object on success, or rejects with { code, error } on failure.
416+
Schema.prototype.deleteField = function(fieldName, className) {
417+
if (!classNameIsValid(className)) {
418+
return Promise.reject({
419+
code: Parse.Error.INVALID_CLASS_NAME,
420+
error: invalidClassNameMessage(className),
421+
});
422+
}
423+
424+
if (!fieldNameIsValid(fieldName)) {
425+
return Promise.reject({
426+
code: Parse.Error.INVALID_KEY_NAME,
427+
error: 'invalid field name: ' + fieldName,
428+
});
429+
}
430+
431+
//Don't allow deleting the default fields.
432+
if (!fieldNameIsValidForClass(fieldName, className)) {
433+
return Promise.reject({
434+
code: 136,
435+
error: 'field ' + fieldName + ' cannot be changed',
436+
});
437+
}
438+
439+
return this.reload()
440+
.then(schema => {
441+
});
442+
}
443+
412444
// Given a schema promise, construct another schema promise that
413445
// validates this field once the schema loads.
414446
function thenValidateField(schemaPromise, className, key, type) {

0 commit comments

Comments
 (0)