MCPcopy
hub / github.com/lxc/incus / IsAPIName

Function IsAPIName

shared/validate/validate.go:563–599  ·  view source on GitHub ↗

IsAPIName checks whether the provided value is a suitable name for an API object.

(value string, allowSlashes bool)

Source from the content-addressed store, hash-verified

561
562// IsAPIName checks whether the provided value is a suitable name for an API object.
563func 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.
602func IsUUID(value string) error {

Callers 15

ValidNameFunction · 0.92
validateNameMethod · 0.92
ValidNameFunction · 0.92
validDevicesFunction · 0.92
storagePoolBucketsPostFunction · 0.92
storagePoolVolumesPostFunction · 0.92
storagePoolVolumePostFunction · 0.92
networkIntegrationsPostFunction · 0.92
networkIntegrationPostFunction · 0.92
clusterNodesPostFunction · 0.92

Calls 2

ErrorfMethod · 0.80
ContainsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…