suggestValue finds the closest match from valid options using Levenshtein distance. Returns the closest match, or empty string if no reasonable match exists.
(input string, valid []string)
| 25 | // suggestValue finds the closest match from valid options using Levenshtein distance. |
| 26 | // Returns the closest match, or empty string if no reasonable match exists. |
| 27 | func suggestValue(input string, valid []string) string { |
| 28 | input = strings.ToLower(input) |
| 29 | best := "" |
| 30 | bestDist := len(input)/2 + 1 // threshold: must be within half the input length |
| 31 | |
| 32 | for _, v := range valid { |
| 33 | d := levenshtein(input, strings.ToLower(v)) |
| 34 | if d < bestDist { |
| 35 | bestDist = d |
| 36 | best = v |
| 37 | } |
| 38 | } |
| 39 | return best |
| 40 | } |
| 41 | |
| 42 | // invalidValueError creates an error message with an optional suggestion for the closest valid value. |
| 43 | func invalidValueError(field, value string, valid []string) error { |