{"record":{"id":"dcdcbb5884c489e2","repo":"goharbor/harbor","slug":"badrequestcode","errorCode":"BadRequestCode","errorMessage":"cannot be empty","messagePattern":"cannot be empty","errorType":"validation","errorClass":"lib/errors.Error","httpStatus":400,"severity":"error","filePath":"src/pkg/label/model/model.go","lineNumber":48,"sourceCode":"\n// Label holds information used for a label\ntype Label struct {\n\tID           int64     `orm:\"pk;auto;column(id)\" json:\"id\"`\n\tName         string    `orm:\"column(name)\" json:\"name\"`\n\tDescription  string    `orm:\"column(description)\" json:\"description\"`\n\tColor        string    `orm:\"column(color)\" json:\"color\"`\n\tLevel        string    `orm:\"column(level)\" json:\"-\"`\n\tScope        string    `orm:\"column(scope)\" json:\"scope\"`\n\tProjectID    int64     `orm:\"column(project_id)\" json:\"project_id\"`\n\tCreationTime time.Time `orm:\"column(creation_time);auto_now_add\" json:\"creation_time\"`\n\tUpdateTime   time.Time `orm:\"column(update_time);auto_now\" json:\"update_time\"`\n\tDeleted      bool      `orm:\"column(deleted)\" json:\"deleted\"`\n}\n\n// Valid ...\nfunc (l *Label) Valid() error {\n\tif len(l.Name) == 0 {\n\t\treturn errors.New(\"cannot be empty\").WithCode(errors.BadRequestCode)\n\t}\n\tif len(l.Name) > 128 {\n\t\treturn errors.New(\"max length is 128\").WithCode(errors.BadRequestCode)\n\t}\n\n\tif l.Scope != common.LabelScopeGlobal && l.Scope != common.LabelScopeProject {\n\t\treturn errors.New(nil).WithMessagef(\"invalid: %s\", l.Scope).WithCode(errors.BadRequestCode)\n\t} else if l.Scope == common.LabelScopeProject && l.ProjectID <= 0 {\n\t\treturn errors.New(nil).WithMessagef(\"invalid: %d\", l.ProjectID).WithCode(errors.BadRequestCode)\n\t}\n\treturn nil\n}\n\n// TableName ...\nfunc (l *Label) TableName() string {\n\treturn \"harbor_label\"\n}\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/goharbor/harbor/blob/7b2fd08cc568955cca339afeefab27372840d936/src/pkg/label/model/model.go#L30-L66","documentation":"Label.Valid() (called from the POST/PUT /api/v2.0/labels and /api/v2.0/projects/{project_id}/labels handlers) rejects a label whose Name is empty. The check is len(l.Name) == 0, so a missing or whitespace-only-as-empty name fails; it maps to BadRequestCode (HTTP 400).","triggerScenarios":"POST or PUT to the label APIs with the \"name\" field omitted, set to \"\", or an empty string after JSON decoding; also direct model.Valid() calls in Go code that build a Label without a Name.","commonSituations":"Automation templates with a placeholder label name never filled in; YAML/JSON config that drops the name key after templating; API clients with optional-name structs that serialize as empty.","solutions":["Provide a non-empty name in the request body, e.g. {\"name\": \"critical\", \"scope\": \"g\"}.","Validate/trim the name client-side before calling the API.","If it comes from a template, assert the templating produced a non-empty value."],"exampleFix":"# before\ncurl -X POST https://harbor/api/v2.0/labels -d '{\"scope\": \"g\"}'\n# after\ncurl -X POST https://harbor/api/v2.0/labels -d '{\"name\": \"critical\", \"scope\": \"g\", \"color\": \"#FFFFFF\"}'","handlingStrategy":"validation","validationCode":"function validateLabelPayload(l) {\n  if (!l || typeof l.name !== 'string' || l.name.trim().length === 0)\n    throw new Error('label name must be a non-empty string');\n  return true;\n}","typeGuard":null,"tryCatchPattern":"On HTTP 400 from the labels API, log the request body minus secrets, fill the missing name field, and re-send; do not blind-retry unchanged.","preventionTips":["Make label name a required field in client schemas/forms.","Assert non-empty values after templating config files that carry label names.","Unit-test client payload builders with empty-input cases."],"tags":["labels","validation","api","harbor"],"backgroundTag":null,"analyzedSha":"7b2fd08cc568955cca339afeefab27372840d936","analyzedAt":"2026-08-16T00:00:10.961Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}