Skip to content

Commit

Permalink
Make sure empty number of votes is null instead of 0 in the request t…
Browse files Browse the repository at this point in the history
…o the api (#871)
  • Loading branch information
Lionqueen94 authored Jan 21, 2025
1 parent 9ccd88a commit a8819a1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions frontend/lib/util/form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Form", () => {

// Number required
[{ type: "number", required: true }, "", "FORM_VALIDATION_RESULT_REQUIRED", undefined],
// FIXME in issue #824 [{ type: "number", required: false }, "", undefined, undefined],
[{ type: "number", required: false }, "", undefined, undefined],

// Number min/max
[{ type: "number", min: 3 }, "2", "FORM_VALIDATION_RESULT_MIN", undefined],
Expand All @@ -54,7 +54,7 @@ describe("Form", () => {

// Number isFormatted required
[{ type: "number", isFormatted: true, required: true }, "", "FORM_VALIDATION_RESULT_REQUIRED", undefined],
// FIXME in issue #824 [{ type: "number", isFormatted: true , required: false }, "", undefined, undefined],
[{ type: "number", isFormatted: true, required: false }, "", undefined, undefined],
])("processForm for field type %j with input %j should return %j, %j", (field, inputValue, error, value) => {
type RequestObject = { field: FieldValue<typeof field> };
const formFields: FormFields<RequestObject> = { field };
Expand Down
6 changes: 6 additions & 0 deletions frontend/lib/util/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export function processForm<RequestObject>(

switch (field.type) {
case "number": {
if (!field.required && value === "") {
value = undefined;
break;
}
const parsedValue = field.isFormatted ? deformatNumber(value) : parseIntUserInput(value);
//parseInt is used in deformatNumber, the result is a number or NaN.
if (parsedValue === undefined || Number.isNaN(parsedValue)) {
Expand Down Expand Up @@ -121,6 +125,8 @@ export function validateFormValue(field: AnyFormField, value: string | number |
if (field.max && value > field.max) {
return "FORM_VALIDATION_RESULT_MAX";
}
} else if (!field.required && value === undefined) {
return null;
} else {
return "FORM_VALIDATION_RESULT_INVALID_TYPE";
}
Expand Down

0 comments on commit a8819a1

Please sign in to comment.