mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Restrict app name
App name must match the regular expression: [\w\-]{1, 30}
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package models
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Apps []*App
|
||||
|
||||
@@ -19,13 +22,27 @@ type App struct {
|
||||
Routes Routes `json:"routes,omitempty"`
|
||||
}
|
||||
|
||||
const (
|
||||
maxAppName = 30
|
||||
)
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user