Skip to content

Commit

Permalink
fix(admin-ui): Use correct defaults for nullable custom fields (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
seminarian authored Aug 29, 2023
1 parent 10b1325 commit 88430e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,13 @@ describe('createUpdatedTranslatable()', () => {

it('coerces empty customFields to correct type', () => {
const customFieldConfig: CustomFieldConfig[] = [
{ name: 'a', type: 'boolean', list: false },
{ name: 'b', type: 'int', list: false },
{ name: 'c', type: 'float', list: false },
{ name: 'd', type: 'datetime', list: false },
{ name: 'e', type: 'string', list: false },
{ name: 'a', type: 'boolean', list: false, nullable: false },
{ name: 'b', type: 'int', list: false, nullable: false },
{ name: 'c', type: 'float', list: false, nullable: false },
{ name: 'd', type: 'datetime', list: false, nullable: false },
{ name: 'e', type: 'string', list: false, nullable: false },
{ name: 'f', type: 'int', list: false, nullable: true },
{ name: 'g', type: 'datetime', list: false, nullable: true },
];

const formValue = {
Expand All @@ -198,6 +200,8 @@ describe('createUpdatedTranslatable()', () => {
c: '',
d: '',
e: '',
f: '',
g: '',
},
};

Expand All @@ -213,5 +217,7 @@ describe('createUpdatedTranslatable()', () => {
expect(result.customFields.c).toBe(0);
expect(result.customFields.d instanceof Date).toBe(true);
expect(result.customFields.e).toBe('');
expect(result.customFields.f).toBe(null);
expect(result.customFields.g).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
newTranslatedCustomFields[field.name] = value;
} else {
newCustomFields[field.name] =
value === '' ? getDefaultValue(field.type as CustomFieldType) : value;
value === ''
? getDefaultValue(field.type as CustomFieldType, field.nullable ?? true)
: value;
}
}
newTranslation.customFields = newTranslatedCustomFields;
Expand All @@ -59,7 +61,7 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
return newTranslatable;
}

function getDefaultValue(type: CustomFieldType): any {
function getDefaultValue(type: CustomFieldType, isNullable?: boolean): any {
switch (type) {
case 'localeString':
case 'string':
Expand All @@ -70,9 +72,9 @@ function getDefaultValue(type: CustomFieldType): any {
return false;
case 'float':
case 'int':
return 0;
return isNullable ? null : 0;
case 'datetime':
return new Date();
return isNullable ? null : new Date();
case 'relation':
return null;
default:
Expand Down

0 comments on commit 88430e5

Please sign in to comment.