Просмотр исходного кода

fix(admin-ui): Coerce customFields to correct data types

Michael Bromley 7 лет назад
Родитель
Сommit
a13c0a0af3

+ 32 - 4
admin-ui/src/app/common/utilities/create-updated-translatable.spec.ts

@@ -100,10 +100,10 @@ describe('createUpdatedTranslatable()', () => {
             return;
         }
 
-        expect((result as any).customFields).toEqual({
+        expect(result.customFields).toEqual({
             available: false,
         });
-        expect((result.translations[0] as any).customFields).toEqual({
+        expect(result.translations[0].customFields).toEqual({
             shortName: 'bar',
         });
     });
@@ -128,11 +128,39 @@ describe('createUpdatedTranslatable()', () => {
             return;
         }
 
-        expect((result as any).customFields).toEqual({
+        expect(result.customFields).toEqual({
             available: false,
         });
-        expect((result.translations[0] as any).customFields).toEqual({
+        expect(result.translations[0].customFields).toEqual({
             shortName: 'bar',
         });
     });
+
+    it('coerces empty customFields to correct type', () => {
+        const customFieldConfig: CustomFieldConfig[] = [
+            { name: 'a', type: 'boolean' },
+            { name: 'b', type: 'int' },
+            { name: 'c', type: 'float' },
+            { name: 'd', type: 'datetime' },
+            { name: 'e', type: 'string' },
+        ];
+
+        const formValue = {
+            customFields: {
+                a: '',
+                b: '',
+                c: '',
+                d: '',
+                e: '',
+            },
+        };
+
+        const result = createUpdatedTranslatable(product, formValue, customFieldConfig, LanguageCode.en);
+
+        expect(result.customFields.a).toBe(false);
+        expect(result.customFields.b).toBe(0);
+        expect(result.customFields.c).toBe(0);
+        expect(result.customFields.d instanceof Date).toBe(true);
+        expect(result.customFields.e).toBe('');
+    });
 });

+ 25 - 2
admin-ui/src/app/common/utilities/create-updated-translatable.ts

@@ -1,6 +1,12 @@
 import { LanguageCode } from 'shared/generated-types';
 
-import { CustomFieldConfig, CustomFieldsObject, MayHaveCustomFields } from 'shared/shared-types';
+import {
+    CustomFieldConfig,
+    CustomFieldsObject,
+    CustomFieldType,
+    MayHaveCustomFields,
+} from 'shared/shared-types';
+import { assertNever } from 'shared/shared-utils';
 
 /**
  * When updating an entity which has translations, the value from the form will pertain to the current
@@ -27,7 +33,7 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
             if (field.type === 'localeString') {
                 newTranslatedCustomFields[field.name] = value;
             } else {
-                newCustomFields[field.name] = value;
+                newCustomFields[field.name] = value === '' ? getDefaultValue(field.type) : value;
             }
         }
         newTranslation.customFields = newTranslatedCustomFields;
@@ -45,6 +51,23 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
     return newTranslatable;
 }
 
+function getDefaultValue(type: CustomFieldType): any {
+    switch (type) {
+        case 'localeString':
+        case 'string':
+            return '';
+        case 'boolean':
+            return false;
+        case 'float':
+        case 'int':
+            return 0;
+        case 'datetime':
+            return new Date();
+        default:
+            assertNever(type);
+    }
+}
+
 /**
  * Returns a shallow clone of `obj` with any properties contained in `patch` overwriting
  * those of `obj`.

+ 1 - 1
server/dev-config.ts

@@ -6,7 +6,7 @@ import { VendureConfig } from './src/config/vendure-config';
  * Config settings used during development
  */
 export const devConfig: VendureConfig = {
-    disableAuth: true,
+    disableAuth: false,
     port: API_PORT,
     apiPath: API_PATH,
     cors: true,