|
| 1 | +package validator |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + |
| 6 | + goValidator "github.com/go-playground/validator/v10" |
| 7 | +) |
| 8 | + |
| 9 | +// Platform struct to define the platform field |
| 10 | +type PackagePlatform struct { |
| 11 | + WP string `json:"wp" validate:"required"` |
| 12 | + PHP string `json:"php" validate:"required"` |
| 13 | +} |
| 14 | + |
| 15 | +// Package struct to define the wpm.json schema |
| 16 | +type Package struct { |
| 17 | + Name string `json:"name" validate:"required,min=3,max=164"` |
| 18 | + Description string `json:"description,omitempty"` |
| 19 | + Type string `json:"type" validate:"required,oneof=plugin theme mu-plugin drop-in"` |
| 20 | + Version string `json:"version" validate:"required,semver,max=64"` |
| 21 | + License string `json:"license" validate:"omitempty"` |
| 22 | + Homepage string `json:"homepage,omitempty" validate:"omitempty,url"` |
| 23 | + Tags []string `json:"tags,omitempty" validate:"dive,max=5"` |
| 24 | + Team []string `json:"team,omitempty"` |
| 25 | + Bin map[string]string `json:"bin,omitempty"` |
| 26 | + Platform PackagePlatform `json:"platform" validate:"required"` |
| 27 | + Dependencies map[string]string `json:"dependencies,omitempty"` |
| 28 | + DevDependencies map[string]string `json:"dev_dependencies,omitempty"` |
| 29 | + Scripts map[string]string `json:"scripts,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +// Description of package fields. |
| 33 | +var PackageFieldDescriptions = map[string]string{ |
| 34 | + "Name": "must contain only lowercase letters, numbers, and hyphens, and be between 3 and 164 characters. (required)", |
| 35 | + "Description": "should be a string. (optional)", |
| 36 | + "Type": "must be one of: 'plugin', 'theme', 'mu-plugin', or 'drop-in'. (required)", |
| 37 | + "Version": "must be a valid semantic version (semver) and less than 64 characters. (required)", |
| 38 | + "License": "must be a string. (optional)", |
| 39 | + "Homepage": "must be a valid url. (optional)", |
| 40 | + "Tags": "must be an array of strings with a maximum of 5 tags. (optional)", |
| 41 | + "Team": "must be an array of strings. (optional)", |
| 42 | + "Bin": "must be an object with string values. (optional)", |
| 43 | + "Platform": "must contain wp and php versions. (required)", |
| 44 | + "Dependencies": "must be an object with string values. (optional)", |
| 45 | + "DevDependencies": "must be an object with string values. (optional)", |
| 46 | + "Scripts": "must be an object with string values. (optional)", |
| 47 | +} |
| 48 | + |
| 49 | +// Dist struct to define the dist field |
| 50 | +type PackageDist struct { |
| 51 | + Size int `json:"size" validate:"gte=0"` |
| 52 | + FileCount int `json:"fileCount" validate:"gte=0"` |
| 53 | + Digest string `json:"digest" validate:"required,sha256"` |
| 54 | +} |
| 55 | + |
| 56 | +// NewValidator creates a new validator instance. |
| 57 | +func NewValidator() (*goValidator.Validate, error) { |
| 58 | + validator := goValidator.New() |
| 59 | + err := validator.RegisterValidation("package_name_regex", packageNameRegex) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + return validator, nil |
| 65 | +} |
| 66 | + |
| 67 | +// ValidatePackage validates the package struct. |
| 68 | +func ValidatePackage(pkg Package, v *goValidator.Validate) error { |
| 69 | + errs := v.Struct(pkg) |
| 70 | + if errs != nil { |
| 71 | + return HandleValidatorError(errs) |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// packageNameRegex validates the package name field with a regex. |
| 78 | +// Only lowercase letters, numbers, and hyphens are allowed. |
| 79 | +func packageNameRegex(fl goValidator.FieldLevel) bool { |
| 80 | + value := fl.Field().String() |
| 81 | + if value == "" { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + return regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) |
| 86 | +} |
0 commit comments