Skip to content

Commit 3379866

Browse files
committed
Move validation logic into Parse Server and out of Mongo Adapter
1 parent af06061 commit 3379866

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

spec/MongoTransform.spec.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,6 @@ describe('untransformObject', () => {
191191
});
192192
});
193193

194-
describe('transformKeyValue', () => {
195-
it('throws out _password', done => {
196-
expect(() => transform.transformKeyValue(dummySchema, '_User', '_password', null, {validate: true})).toThrow();
197-
done();
198-
});
199-
});
200-
201194
describe('transform schema key changes', () => {
202195

203196
it('changes new pointer key', (done) => {

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ function transformKeyValue(schema, className, restKey, restValue, {
7171
if (authDataMatch) {
7272
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'can only query on ' + key);
7373
}
74-
if (validate && !key.match(/^[a-zA-Z][a-zA-Z0-9_\.]*$/)) {
75-
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, 'invalid key name: ' + key);
76-
}
7774
}
7875

7976
// Handle special schema key changes

src/Controllers/DatabaseController.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,23 @@ DatabaseController.prototype.find = function(className, query, {
618618
.then(schemaController => {
619619
if (sort) {
620620
mongoOptions.sort = {};
621-
for (let key in sort) {
622-
let mongoKey = this.transform.transformKeyValue(schemaController, className, key, null, {validate: true}).key;
623-
mongoOptions.sort[mongoKey] = sort[key];
621+
for (let fieldName in sort) {
622+
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
623+
// so duplicate that behaviour here.
624+
if (fieldName === '_created_at') {
625+
fieldName = 'createdAt';
626+
sort['createdAt'] = sort['_created_at'];
627+
}
628+
if (fieldName === '_updated_at') {
629+
fieldName = 'updatedAt';
630+
sort['updatedAt'] = sort['_updated_at'];
631+
}
632+
633+
if (!SchemaController.fieldNameIsValid(fieldName)) {
634+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
635+
}
636+
let mongoKey = this.transform.transformKeyValue(schemaController, className, fieldName, null).key;
637+
mongoOptions.sort[mongoKey] = sort[fieldName];
624638
}
625639
}
626640
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))

src/Controllers/SchemaController.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,15 +466,16 @@ class SchemaController {
466466
// If 'freeze' is true, refuse to update the schema for this field.
467467
validateField(className, fieldName, type, freeze) {
468468
return this.reloadData().then(() => {
469-
// Just to check that the fieldName is valid
470-
this._collection.transform.transformKeyValue(this, className, fieldName, null, {validate: true});
471-
472-
if( fieldName.indexOf(".") > 0 ) {
469+
if (fieldName.indexOf(".") > 0) {
473470
// subdocument key (x.y) => ok if x is of type 'object'
474471
fieldName = fieldName.split(".")[ 0 ];
475472
type = 'Object';
476473
}
477474

475+
if (!fieldNameIsValid(fieldName)) {
476+
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
477+
}
478+
478479
let expected = this.data[className][fieldName];
479480
if (expected) {
480481
expected = (expected === 'map' ? 'Object' : expected);
@@ -847,6 +848,7 @@ function getObjectType(obj) {
847848
export {
848849
load,
849850
classNameIsValid,
851+
fieldNameIsValid,
850852
invalidClassNameMessage,
851853
buildMergedSchemaObject,
852854
systemClasses,

0 commit comments

Comments
 (0)