57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
|
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
|
||
|
}
|
||
|
|
||
|
paths, ok := rawPaths.(map[any]any)
|
||
|
if !ok {
|
||
|
return false, errors.Errorf("metadata['paths']: unexpected named path value type '%T'", rawPaths)
|
||
|
}
|
||
|
|
||
|
for n, p := range paths {
|
||
|
name, ok := n.(string)
|
||
|
if !ok {
|
||
|
return false, errors.Errorf("metadata['paths']: unexpected named path type '%T'", n)
|
||
|
}
|
||
|
|
||
|
if _, exists := set[NamedPath(name)]; !exists {
|
||
|
return false, errors.Errorf("metadata['paths']: unexpected named path '%s'", name)
|
||
|
}
|
||
|
|
||
|
path, ok := p.(string)
|
||
|
if !ok {
|
||
|
return false, errors.Errorf("metadata['paths']['%s']: unexpected named path value type '%T'", name, path)
|
||
|
}
|
||
|
|
||
|
if !strings.HasPrefix(path, "/") {
|
||
|
return false, errors.Errorf("metadata['paths']['%s']: named path value should start with a '/'", name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true, nil
|
||
|
}
|
||
|
}
|