Restrict app name

App name must match the regular expression: [\w\-]{1, 30}
This commit is contained in:
Evan Shaw
2016-07-29 14:00:35 +12:00
parent 2578530822
commit 6bd6dd342d

View File

@@ -1,6 +1,9 @@
package models package models
import "errors" import (
"errors"
"fmt"
)
type Apps []*App type Apps []*App
@@ -19,13 +22,27 @@ type App struct {
Routes Routes `json:"routes,omitempty"` Routes Routes `json:"routes,omitempty"`
} }
const (
maxAppName = 30
)
var ( var (
ErrAppsValidationName = errors.New("Missing app name") ErrAppsValidationMissingName = errors.New("Missing app name")
ErrAppsValidationTooLongName = fmt.Errorf("App name must be %v characters or less", maxAppName)
ErrAppsValidationInvalidName = errors.New("Invalid app name")
) )
func (a *App) Validate() error { func (a *App) Validate() error {
if a.Name == "" { if a.Name == "" {
return ErrAppsValidationName return ErrAppsValidationMissingName
}
if len(a.Name) > maxAppName {
return ErrAppsValidationTooLongName
}
for _, c := range a.Name {
if (c < '0' || '9' < c) && (c < 'A' || 'Z' > c) && (c < 'a' || 'z' < c) && c != '_' && c != '-' {
return ErrAppsValidationInvalidName
}
} }
return nil return nil
} }