first commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package validator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"browser.local/platform/domain"
|
||||
)
|
||||
|
||||
const maxLifecycleIdempotencyKeyLength = 160
|
||||
|
||||
func ValidateServerLifecycleCreate(create domain.ServerLifecycleCreate) error {
|
||||
var violations []string
|
||||
violations = appendRequired(violations, "id", create.ID)
|
||||
violations = appendRequired(violations, "pluginId", create.PluginID)
|
||||
violations = appendRequired(violations, "runEndpointId", create.RunEndpointID)
|
||||
violations = appendRequired(violations, "name", create.Name)
|
||||
violations = appendLifecycleIdempotencyViolations(violations, create.IdempotencyKey)
|
||||
return finish(violations)
|
||||
}
|
||||
|
||||
func ValidateServerLifecycleCommand(command domain.ServerLifecycleCommand) error {
|
||||
var violations []string
|
||||
violations = appendRequired(violations, "serverInstanceId", command.ServerInstanceID)
|
||||
if command.ExpectedConfigVersion <= 0 {
|
||||
violations = append(violations, "expectedConfigVersion must be positive")
|
||||
}
|
||||
violations = appendLifecycleIdempotencyViolations(violations, command.IdempotencyKey)
|
||||
return finish(violations)
|
||||
}
|
||||
|
||||
func ValidateServerLifecycleAction(action domain.ServerLifecycleAction) error {
|
||||
switch action {
|
||||
case domain.ServerLifecycleActionCreate, domain.ServerLifecycleActionStart, domain.ServerLifecycleActionStop:
|
||||
return nil
|
||||
default:
|
||||
return ValidationError{Violations: []string{fmt.Sprintf("action %q is invalid", action)}}
|
||||
}
|
||||
}
|
||||
|
||||
func appendLifecycleIdempotencyViolations(violations []string, key string) []string {
|
||||
violations = appendRequired(violations, "idempotencyKey", key)
|
||||
if len(key) > maxLifecycleIdempotencyKeyLength {
|
||||
violations = append(violations, "idempotencyKey is too long")
|
||||
}
|
||||
if strings.TrimSpace(key) != key {
|
||||
violations = append(violations, "idempotencyKey must not have surrounding whitespace")
|
||||
}
|
||||
return violations
|
||||
}
|
||||
Reference in New Issue
Block a user