Skip to content

Commit

Permalink
edits to ValidateManifest
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Dec 18, 2024
1 parent 7829423 commit b49bee2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 55 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
# Go workspace file
go.work

test_icon.png
test_manifest.json
test-pkg
89 changes: 46 additions & 43 deletions experimental/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ func ValidateManifest(author string, data []byte) (bool, []string, error) {

err := json.Unmarshal(data, &manifest)
if err != nil {
errors = append(errors, err.Error())
}

pkg, _ := GetPackage(author, manifest.Name)
if pkg == nil {
return false, nil, NewErr("package not found under the specified author")
return false, nil, NewErr("error deserializing manifest: \n" + err.Error())
}

AddIfEmpty(&errors, &manifest.Name, "required property 'name' is empty or unspecified")
Expand All @@ -69,57 +64,45 @@ func ValidateManifest(author string, data []byte) (bool, []string, error) {

verEmpty := AddIfEmpty(&errors, &manifest.VersionNumber, "required property 'version_number' is empty or unspecified")
if !verEmpty {
sv, _ := util.CheckSemVer(manifest.VersionNumber)
AddIfFalse(&errors, &sv, "property 'version_number' does not follow semantic versioning (major.minor.patch)")

if !sv {
return false, errors, nil
}

verA, _ := version.NewSemver(manifest.VersionNumber)
verB, _ := version.NewSemver(pkg.Latest.VersionNumber)

if verA.LessThanOrEqual(verB) {
errors = append(errors, "property 'version_number' must be higher than the latest")
valid, _ := util.CheckSemVer(manifest.VersionNumber)
if valid {
pkg, _ := GetPackage(author, manifest.Name)
if pkg != nil {
verA, _ := version.NewSemver(manifest.VersionNumber)
verB, _ := version.NewSemver(pkg.Latest.VersionNumber)

if verA.LessThanOrEqual(verB) {
Add(&errors, "property 'version_number' must be higher than the latest")
}
}
} else {
Add(&errors, "property 'version_number' does not follow semantic versioning (major.minor.patch)")
}
}

if manifest.WebsiteURL == nil {
errors = append(errors, "required property 'website_url' is unspecified")
Add(&errors, "required property 'website_url' is unspecified")
} else {
url := strings.ToLower(*manifest.WebsiteURL)
if !(strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) {
errors = append(errors, "property 'website_url' must be a valid URL")
Add(&errors, "property 'website_url' must be a valid URL")
}
}

if manifest.Dependencies == nil {
errors = append(errors, "manifest property 'dependencies' is required")
}

return len(errors) < 1, errors, nil
}

func AddIfEmpty(arr *[]string, str *string, errStr string) bool {
empty := *str == "" || str == nil
if empty {
*arr = append(*arr, errStr)
}

return empty
}
Add(&errors, "manifest property 'dependencies' is required")
} else {
for _, dep := range manifest.Dependencies {
fullName := author + "-" + manifest.Name
if strings.Contains(strings.ToLower(dep), strings.ToLower(fullName)) {
Add(&errors, "manifest property 'dependencies' is invalid. cannot depend on self")
}

func AddIfFalse(arr *[]string, val *bool, errStr string) {
if !*val {
*arr = append(*arr, errStr)
// Check multiple versions of same package
}
}
}

func AddIfInvalid(arr *[]string, str *string, errStr string) {
matched, _ := regexp.MatchString(`^[a-zA-Z0-9_]+$`, *str)
if !matched {
*arr = append(*arr, errStr)
}
return len(errors) < 1, errors, nil
}

// Decodes image data and validates that the image is a PNG and the dimensions are 256x256.
Expand All @@ -146,3 +129,23 @@ func ValidateIcon(params IconValidatorParams) (bool, error) {

return false, errors.New("image dimensions did not match: 256x256")
}

func Add(arr *[]string, errStr string) {
*arr = append(*arr, errStr)
}

func AddIfEmpty(arr *[]string, str *string, errStr string) bool {
empty := *str == "" || str == nil
if empty {
Add(arr, errStr)
}

return empty
}

func AddIfInvalid(arr *[]string, str *string, errStr string) {
matched, _ := regexp.MatchString(`^[a-zA-Z0-9_]+$`, *str)
if !matched {
Add(arr, errStr)
}
}
12 changes: 3 additions & 9 deletions tests/exp/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ import (
func TestValidateIcon(t *testing.T) {
t.Skip()

var err error
icon, err := os.ReadFile("../test_icon.png")

icon, err := os.ReadFile("../test-pkg/icon.png.jpg")
if err != nil {
t.Fatal(err.Error())
}

valid, err := TSGO.ValidateIcon(TSGO.IconValidatorParams{
ImageData: icon,
})

valid, err := TSGO.ValidateIcon(TSGO.IconValidatorParams{ImageData: icon})
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -34,14 +29,13 @@ func TestValidateManifest(t *testing.T) {
t.Skip()

var errs []string
data, err := os.ReadFile("../test_manifest.json")

data, err := os.ReadFile("../test-pkg/manifest.json")
if err != nil {
t.Fatal(err.Error())
}

valid, errs, err := TSGO.ValidateManifest("Owen3H", data)

if err != nil {
t.Fatal(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func PackagesFromCommunities(communities []Community) (PackageList, error) {
i := i
g.Go(func() error {
comm := communities[i]
pkgs, err := comm.AllPackages()

pkgs, err := comm.AllPackages()
if err != nil {
return err
}
Expand Down

0 comments on commit b49bee2

Please sign in to comment.