fix(cli): reject NaN/Inf in --field number parsing per Codex review (round 1)

strconv.ParseFloat accepts "NaN", "+Inf", "-Inf" as valid float64 values,
but encoding/json cannot marshal those. The downstream json.Marshal(fields)
errors at cmd/pad/main.go createCmd / updateCmd are intentionally ignored
(`fieldsJSON, _ := json.Marshal(fields)`), so a single malformed --field
input would silently drop the entire fields payload instead of rejecting.

Reject non-finite floats in parseFieldFlag and fall back to the raw string;
the server validator then returns the useful "field X must be a number"
error.

Verified:
  pad item update BLOG-1393 --field reading_time=NaN → "must be a number" ✓
  pad item update BLOG-1393 --field reading_time=Inf → "must be a number" ✓
  pad item update BLOG-1393 --field reading_time=4   → stored as 4         ✓
This commit is contained in:
xarmian
2026-05-13 04:11:34 +00:00
parent 957582eea9
commit 3508a83307
+9 -1
View File
@@ -10,6 +10,7 @@ import (
"io"
"io/fs"
"log/slog"
"math"
goMime "mime"
"net/http"
"net/url"
@@ -2821,7 +2822,14 @@ func parseFieldFlag(schema models.CollectionSchema, key, raw string) any {
}
case "number":
if f, err := strconv.ParseFloat(raw, 64); err == nil {
return f
// Reject NaN / ±Inf — encoding/json can't marshal them, and
// the downstream json.Marshal(fields) error is ignored, so
// returning a non-finite float would silently drop the entire
// fields payload. Falling back to the raw string lets the
// server validator return a useful "must be a number" error.
if !math.IsNaN(f) && !math.IsInf(f, 0) {
return f
}
}
case "checkbox":
if b, err := strconv.ParseBool(raw); err == nil {