details.go
1 package source 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 dogeboxd "github.com/dogeorg/dogeboxd/pkg" 8 ) 9 10 func ParseAndValidateSourceDetails(content string) (dogeboxd.SourceDetails, error) { 11 var details dogeboxd.SourceDetails 12 err := json.Unmarshal([]byte(content), &details) 13 if err != nil { 14 return dogeboxd.SourceDetails{}, fmt.Errorf("failed to unmarshal content: %w", err) 15 } 16 17 // Check we have an ID and a name, description is optional. 18 // Empty/missing pups are fine, but check any defined ones have a location. 19 20 if details.ID == "" { 21 return dogeboxd.SourceDetails{}, fmt.Errorf("missing field: id") 22 } 23 24 if details.Name == "" { 25 return dogeboxd.SourceDetails{}, fmt.Errorf("missing field: name") 26 } 27 28 for _, pup := range details.Pups { 29 if pup.Location == "" { 30 return dogeboxd.SourceDetails{}, fmt.Errorf("missing field: location in pups") 31 } 32 } 33 34 return details, nil 35 }