Fixed saving of a cleared number value (#412)

* Fixed saving of a cleared number value

* fix: clearing saved number for a localized field

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Oran Epelbaum
2022-01-06 00:24:14 +02:00
committed by GitHub
parent 65b0ad7f08
commit 5549926d2c
3 changed files with 5 additions and 4 deletions

View File

@@ -84,7 +84,6 @@ const LocalizedPosts: CollectionConfig = {
name: 'priority',
label: 'Priority',
type: 'number',
required: true,
localized: true,
},
{

View File

@@ -137,7 +137,7 @@ describe('Collections - REST', () => {
description: updatedDesc,
richText: updatedRichText,
nonLocalizedArray: updatedNonLocalizedArray,
priority: 1,
priority: '',
}),
headers,
method: 'put',
@@ -147,6 +147,7 @@ describe('Collections - REST', () => {
expect(response.status).toBe(200);
expect(data.doc.description).toBe(updatedDesc);
expect(data.doc.priority).not.toStrictEqual(1);
expect(data.doc.nonLocalizedArray).toHaveLength(2);
expect(data.doc.richText[0].children[0].text).toBe('english update');

View File

@@ -109,7 +109,8 @@ const traverseFields = (args: Arguments): void => {
}
if (field.type === 'number' && typeof data[field.name] === 'string') {
dataCopy[field.name] = parseFloat(data[field.name]);
const trimmed = data[field.name].trim();
dataCopy[field.name] = (trimmed.length === 0) ? null : parseFloat(trimmed);
}
if (fieldAffectsData(field) && field.name === 'id') {
@@ -173,7 +174,7 @@ const traverseFields = (args: Arguments): void => {
let valueToSet;
if (localeID === locale) {
if (data[field.name]) {
if (typeof data[field.name] !== 'undefined') {
valueToSet = data[field.name];
} else if (docWithLocales?.[field.name]?.[localeID]) {
valueToSet = docWithLocales?.[field.name]?.[localeID];