2023-04-11 11:04:34 +02:00
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NamedPath string
|
|
|
|
|
|
|
|
const (
|
|
|
|
NamedPathAdmin NamedPath = "admin"
|
|
|
|
NamedPathIcon NamedPath = "icon"
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithNamedPathsValidator(names ...NamedPath) app.MetadataValidator {
|
|
|
|
set := map[NamedPath]struct{}{}
|
|
|
|
for _, n := range names {
|
|
|
|
set[n] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(metadata map[string]any) (bool, error) {
|
|
|
|
rawPaths, exists := metadata["paths"]
|
|
|
|
if !exists {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 15:08:07 +02:00
|
|
|
paths, ok := rawPaths.(app.MapStr)
|
2023-04-11 11:04:34 +02:00
|
|
|
if !ok {
|
|
|
|
return false, errors.Errorf("metadata['paths']: unexpected named path value type '%T'", rawPaths)
|
|
|
|
}
|
|
|
|
|
|
|
|
for n, p := range paths {
|
2023-04-11 15:08:07 +02:00
|
|
|
if _, exists := set[NamedPath(n)]; !exists {
|
|
|
|
return false, errors.Errorf("metadata['paths']: unexpected named path '%s'", n)
|
2023-04-11 11:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
path, ok := p.(string)
|
|
|
|
if !ok {
|
2023-04-11 15:08:07 +02:00
|
|
|
return false, errors.Errorf("metadata['paths']['%s']: unexpected named path value type '%T'", n, path)
|
2023-04-11 11:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
2023-04-11 15:08:07 +02:00
|
|
|
return false, errors.Errorf("metadata['paths']['%s']: named path value should start with a '/'", n)
|
2023-04-11 11:04:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|