Skip to content

Commit 60bbd42

Browse files
committed
remove className from beforeSave hook response
1 parent e4471bb commit 60bbd42

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

spec/ParseObject.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,17 @@ describe('Parse.Object testing', () => {
728728
});
729729
});
730730

731+
it('cannot save object with className field', async () => {
732+
const obj = new TestObject();
733+
obj.set('className', 'bar');
734+
try {
735+
await obj.save();
736+
expect(true).toBe(false);
737+
} catch (e) {
738+
expect(e.message).toBe('Invalid field name: className.');
739+
}
740+
});
741+
731742
it('old attribute unset then unset', function (done) {
732743
const TestObject = Parse.Object.extend('TestObject');
733744
const obj = new TestObject();

src/Controllers/DatabaseController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ class DatabaseController {
564564
}
565565
const rootFieldName = getRootFieldName(fieldName);
566566
if (
567-
!SchemaController.fieldNameIsValid(rootFieldName) &&
567+
!SchemaController.fieldNameIsValid(rootFieldName, className) &&
568568
!isSpecialUpdateKey(rootFieldName)
569569
) {
570570
throw new Parse.Error(
@@ -1213,7 +1213,7 @@ class DatabaseController {
12131213
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Cannot sort by ${fieldName}`);
12141214
}
12151215
const rootFieldName = getRootFieldName(fieldName);
1216-
if (!SchemaController.fieldNameIsValid(rootFieldName)) {
1216+
if (!SchemaController.fieldNameIsValid(rootFieldName, className)) {
12171217
throw new Parse.Error(
12181218
Parse.Error.INVALID_KEY_NAME,
12191219
`Invalid field name: ${fieldName}.`

src/Controllers/HooksController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ function wrapToHTTPRequest(hook, key) {
242242
if (typeof result === 'object') {
243243
delete result.createdAt;
244244
delete result.updatedAt;
245+
delete result.className;
245246
}
246247
return { object: result };
247248
} else {

src/Controllers/SchemaController.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,19 +424,24 @@ function classNameIsValid(className: string): boolean {
424424
// Be a join table OR
425425
joinClassRegex.test(className) ||
426426
// Include only alpha-numeric and underscores, and not start with an underscore or number
427-
fieldNameIsValid(className)
427+
fieldNameIsValid(className, className)
428428
);
429429
}
430430

431431
// Valid fields must be alpha-numeric, and not start with an underscore or number
432432
// must not be a reserved key
433-
function fieldNameIsValid(fieldName: string): boolean {
433+
function fieldNameIsValid(fieldName: string, className: string): boolean {
434+
if (className && className !== '_Hooks') {
435+
if (fieldName === 'className') {
436+
return false;
437+
}
438+
}
434439
return classAndFieldRegex.test(fieldName) && !invalidColumns.includes(fieldName);
435440
}
436441

437442
// Checks that it's not trying to clobber one of the default fields of the class.
438443
function fieldNameIsValidForClass(fieldName: string, className: string): boolean {
439-
if (!fieldNameIsValid(fieldName)) {
444+
if (!fieldNameIsValid(fieldName, className)) {
440445
return false;
441446
}
442447
if (defaultColumns._Default[fieldName]) {
@@ -979,7 +984,7 @@ export default class SchemaController {
979984
) {
980985
for (const fieldName in fields) {
981986
if (existingFieldNames.indexOf(fieldName) < 0) {
982-
if (!fieldNameIsValid(fieldName)) {
987+
if (!fieldNameIsValid(fieldName, className)) {
983988
return {
984989
code: Parse.Error.INVALID_KEY_NAME,
985990
error: 'invalid field name: ' + fieldName,
@@ -1063,7 +1068,7 @@ export default class SchemaController {
10631068
fieldName = fieldName.split('.')[0];
10641069
type = 'Object';
10651070
}
1066-
if (!fieldNameIsValid(fieldName)) {
1071+
if (!fieldNameIsValid(fieldName, className)) {
10671072
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
10681073
}
10691074

@@ -1157,7 +1162,7 @@ export default class SchemaController {
11571162
}
11581163

11591164
fieldNames.forEach(fieldName => {
1160-
if (!fieldNameIsValid(fieldName)) {
1165+
if (!fieldNameIsValid(fieldName, className)) {
11611166
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `invalid field name: ${fieldName}`);
11621167
}
11631168
//Don't allow deleting the default fields.

0 commit comments

Comments
 (0)