ValidateChatLabels checks that the provided labels map conforms to the labeling constraints for chats. It returns a list of validation errors, one per violated constraint.
(labels map[string]string)
| 27 | // labeling constraints for chats. It returns a list of validation |
| 28 | // errors, one per violated constraint. |
| 29 | func ValidateChatLabels(labels map[string]string) []codersdk.ValidationError { |
| 30 | var errs []codersdk.ValidationError |
| 31 | |
| 32 | if len(labels) > maxLabelsPerChat { |
| 33 | errs = append(errs, codersdk.ValidationError{ |
| 34 | Field: "labels", |
| 35 | Detail: fmt.Sprintf("too many labels (%d); maximum is %d", len(labels), maxLabelsPerChat), |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | for k, v := range labels { |
| 40 | if k == "" { |
| 41 | errs = append(errs, codersdk.ValidationError{ |
| 42 | Field: "labels", |
| 43 | Detail: "label key must not be empty", |
| 44 | }) |
| 45 | continue |
| 46 | } |
| 47 | |
| 48 | if len(k) > maxLabelKeyLength { |
| 49 | errs = append(errs, codersdk.ValidationError{ |
| 50 | Field: "labels", |
| 51 | Detail: fmt.Sprintf("label key %q exceeds maximum length of %d bytes", k, maxLabelKeyLength), |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | if !labelKeyRegex.MatchString(k) { |
| 56 | errs = append(errs, codersdk.ValidationError{ |
| 57 | Field: "labels", |
| 58 | Detail: fmt.Sprintf("label key %q contains invalid characters; must match %s", k, labelKeyRegex.String()), |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | if v == "" { |
| 63 | errs = append(errs, codersdk.ValidationError{ |
| 64 | Field: "labels", |
| 65 | Detail: fmt.Sprintf("label value for key %q must not be empty", k), |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | if len(v) > maxLabelValueLength { |
| 70 | errs = append(errs, codersdk.ValidationError{ |
| 71 | Field: "labels", |
| 72 | Detail: fmt.Sprintf("label value for key %q exceeds maximum length of %d bytes", k, maxLabelValueLength), |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return errs |
| 78 | } |