Move field name validation logic out of mongo (#1752)

* Remove transformKey(...)

* Move validation logic into Parse Server and out of Mongo Adapter

* Fix nits
This commit is contained in:
Drew
2016-05-13 15:28:14 -07:00
parent 4bfe2c5014
commit e4998c256a
4 changed files with 24 additions and 31 deletions

View File

@@ -618,9 +618,22 @@ DatabaseController.prototype.find = function(className, query, {
.then(schemaController => {
if (sort) {
mongoOptions.sort = {};
for (let key in sort) {
let mongoKey = this.transform.transformKey(schemaController, className, key);
mongoOptions.sort[mongoKey] = sort[key];
for (let fieldName in sort) {
// Parse.com treats queries on _created_at and _updated_at as if they were queries on createdAt and updatedAt,
// so duplicate that behaviour here.
if (fieldName === '_created_at') {
fieldName = 'createdAt';
sort['createdAt'] = sort['_created_at'];
} else if (fieldName === '_updated_at') {
fieldName = 'updatedAt';
sort['updatedAt'] = sort['_updated_at'];
}
if (!SchemaController.fieldNameIsValid(fieldName)) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
}
const mongoKey = this.transform.transformKeyValue(schemaController, className, fieldName, null).key;
mongoOptions.sort[mongoKey] = sort[fieldName];
}
}
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))

View File

@@ -253,7 +253,7 @@ class SchemaController {
this.data[schema.className] = schema.fields;
this.perms[schema.className] = schema.classLevelPermissions;
});
// Inject the in-memory classes
volatileClasses.forEach(className => {
this.data[className] = injectDefaultSchema({
@@ -466,15 +466,16 @@ class SchemaController {
// If 'freeze' is true, refuse to update the schema for this field.
validateField(className, fieldName, type, freeze) {
return this.reloadData().then(() => {
// Just to check that the fieldName is valid
this._collection.transform.transformKey(this, className, fieldName);
if( fieldName.indexOf(".") > 0 ) {
if (fieldName.indexOf(".") > 0) {
// subdocument key (x.y) => ok if x is of type 'object'
fieldName = fieldName.split(".")[ 0 ];
type = 'Object';
}
if (!fieldNameIsValid(fieldName)) {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
}
let expected = this.data[className][fieldName];
if (expected) {
expected = (expected === 'map' ? 'Object' : expected);
@@ -847,6 +848,7 @@ function getObjectType(obj) {
export {
load,
classNameIsValid,
fieldNameIsValid,
invalidClassNameMessage,
buildMergedSchemaObject,
systemClasses,