diff --git a/src/process/normalize/__tests__/normalize.test.ts b/src/process/normalize/__tests__/normalize.test.ts index 9608906d..414a0b46 100644 --- a/src/process/normalize/__tests__/normalize.test.ts +++ b/src/process/normalize/__tests__/normalize.test.ts @@ -103,6 +103,32 @@ it('Should normalize a radio component value with a number', () => { expect(context.data).to.deep.equal({radio: 0}); }); +it('Should normalize a radio component value with a string if storage type is set to string', () => { + const radioComp = { + type: 'radio', + key: 'radio', + input: true, + label: 'Radio', + dataType: 'string', + values: [ + { + label: '1', + value: 1, + }, + { + label: '0', + value: 0, + } + ] + }; + const data = { + radio: 0 + }; + const context = generateProcessorContext(radioComp, data); + normalizeProcessSync(context); + expect(context.data).to.deep.equal({radio: '0'}); +}); + it('Should normalize a number component value with a string value', () => { const numberComp = { type: 'number', diff --git a/src/process/normalize/index.ts b/src/process/normalize/index.ts index 45b513c3..ad2df332 100644 --- a/src/process/normalize/index.ts +++ b/src/process/normalize/index.ts @@ -109,7 +109,15 @@ const normalizeDayComponentValue = (component: DayComponent, form: any, value: a return dateParts.join('/'); }; -const normalizeRadioComponentValue = (value: any) => { +const normalizeRadioComponentValue = (value: any, dataType?: string) => { + switch(dataType) { + case 'number': + return +value; + case 'string': + return typeof value === 'object' ? JSON.stringify(value) : String(value); + case 'boolean': + return !(!value || value.toString() === 'false'); + } const isEquivalent = toString(value) === Number(value).toString(); if (!isNaN(parseFloat(value)) && isFinite(value) && isEquivalent) { return +value; @@ -318,7 +326,7 @@ export const normalizeProcessSync: ProcessorFnSync = (context) = scope.normalize[path].normalized = true; } } else if (isRadioComponent(component)) { - set(data, path, normalizeRadioComponentValue(value)); + set(data, path, normalizeRadioComponentValue(value, component.dataType)); scope.normalize[path].normalized = true; } else if (isSelectComponent(component)) { set(data, path, normalizeSelectComponentValue(component, value));