validate.go 610 B

123456789101112131415161718192021222324252627282930
  1. package cloudcost
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/opencost/opencost/core/pkg/opencost"
  6. )
  7. // ValidateField normalizes and validates a cloud cost autocomplete field name.
  8. func ValidateField(field string) (string, error) {
  9. if field == "" {
  10. return "", fmt.Errorf("field is required")
  11. }
  12. f := strings.ToLower(field)
  13. if f == "label" {
  14. return f, nil
  15. }
  16. if strings.HasPrefix(f, "label:") {
  17. _, labelKey, _ := strings.Cut(field, ":")
  18. return "label:" + labelKey, nil
  19. }
  20. property, err := opencost.ParseCloudCostProperty(field)
  21. if err != nil {
  22. return "", err
  23. }
  24. return string(property), nil
  25. }