Merge "[CORD-2292] Removing all the fields not defined in xProto before saving a model"
diff --git a/src/app/core/services/helpers/config.helpers.spec.ts b/src/app/core/services/helpers/config.helpers.spec.ts
index 7120a6b..f36c20a 100644
--- a/src/app/core/services/helpers/config.helpers.spec.ts
+++ b/src/app/core/services/helpers/config.helpers.spec.ts
@@ -344,6 +344,48 @@
]);
});
});
+
+ describe('the removeExtraFields method', () => {
+ beforeEach(() => {
+ service = new ConfigHelpers(q, stateMock, toastr, modelStoreMock, XosFormHelpersMock);
+ });
+
+ it('should remove properties not defined in xProto', () => {
+ const model: IXosModeldef = {
+ name: 'Test',
+ app: 'test',
+ fields: [
+ {
+ type: 'number',
+ name: 'foo',
+ validators: []
+ },
+ {
+ type: 'string',
+ name: 'bar',
+ validators: [
+ {
+ bool_value: true,
+ name: 'required'
+ }
+ ]
+ }
+ ],
+ description: '',
+ verbose_name: ''
+ };
+
+ const item: any = {
+ foo: 1,
+ bar: 'existing',
+ baz: 'remove me'
+ };
+
+ const res = service['removeExtraFields'](item, model);
+
+ expect(res).not.toHaveProp('baz');
+ });
+ });
});
});
diff --git a/src/app/core/services/helpers/config.helpers.ts b/src/app/core/services/helpers/config.helpers.ts
index 4750ac1..80855e5 100644
--- a/src/app/core/services/helpers/config.helpers.ts
+++ b/src/app/core/services/helpers/config.helpers.ts
@@ -316,25 +316,17 @@
return;
}
+ // remove fields added by the GUI
+ item = this.removeExtraFields(item, model);
- const itemCopy = angular.copy(item);
-
- // TODO remove ManyToMany relations and save them separately (how??)
- delete item.networks;
-
- // remove field added by xosTable
_.forEach(Object.keys(item), prop => {
- // FIXME what _ptr fields comes from??
- if (prop.indexOf('-formatted') > -1 || prop.indexOf('_ptr') > -1) {
- delete item[prop];
- }
-
// convert dates back to UnixTime
if (this.XosFormHelpers._getFieldFormat(item[prop]) === 'date') {
item[prop] = new Date(item[prop]).getTime() / 1000;
}
});
+ const itemCopy = angular.copy(item);
const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name;
item.$save()
@@ -365,6 +357,16 @@
return formCfg;
}
+ private removeExtraFields(item: any, modelDef: IXosModeldef) {
+ _.forEach(Object.keys(item), prop => {
+ const hasField = _.findIndex(modelDef.fields, {name: prop}) > -1;
+ if (!hasField) {
+ delete item[prop];
+ }
+ });
+ return item;
+ }
+
private formatDefaultValues(val: any): any {
if (angular.isString(val)) {