IsAPIName checks whether the provided value is a suitable name for an API object.
(value string, allowSlashes bool)
| 561 | |
| 562 | // IsAPIName checks whether the provided value is a suitable name for an API object. |
| 563 | func IsAPIName(value string, allowSlashes bool) error { |
| 564 | // Limit length to 64 characters. |
| 565 | if len(value) > 64 { |
| 566 | return errors.New("Maximum name length is 64 characters") |
| 567 | } |
| 568 | |
| 569 | // Check for unicode characters. |
| 570 | for _, r := range value { |
| 571 | if unicode.IsSpace(r) { |
| 572 | return errors.New("Name cannot contain white space") |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | // Check for special URL characters. |
| 577 | reservedChars := []string{"$", "?", "&", "+", "\"", "'", "`", "*"} |
| 578 | if !allowSlashes { |
| 579 | reservedChars = append(reservedChars, "/") |
| 580 | } |
| 581 | |
| 582 | for _, char := range reservedChars { |
| 583 | if strings.Contains(value, char) { |
| 584 | return fmt.Errorf("Name contains invalid character %q", char) |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | // Check beginning and end. |
| 589 | match, err := regexp.MatchString(`^[a-zA-Z0-9]+(.*[a-zA-Z0-9]+)?$`, value) |
| 590 | if err != nil { |
| 591 | return err |
| 592 | } |
| 593 | |
| 594 | if !match { |
| 595 | return errors.New("Names must start and end with an alphanumeric character") |
| 596 | } |
| 597 | |
| 598 | return nil |
| 599 | } |
| 600 | |
| 601 | // IsUUID validates whether a value is a UUID. |
| 602 | func IsUUID(value string) error { |
no test coverage detected
searching dependent graphs…