{"record":{"id":"ecef2e39b7a67cce","repo":"semaphoreui/semaphore","slug":"key-can-not-be-empty","errorCode":null,"errorMessage":"key can not be empty","messagePattern":"key can not be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"db/Environment.go","lineNumber":94,"sourceCode":"\t}\n\n\treturn errors.New(\"invalid environment secret type\")\n}\n\nfunc validateJSON(s string, mustValuesBeScalar bool) error {\n\tif s == \"\" {\n\t\treturn nil\n\t}\n\n\tvar data map[string]any\n\terr := json.Unmarshal([]byte(s), &data)\n\tif err != nil {\n\t\treturn errors.New(\"must be valid JSON\")\n\t}\n\n\tfor k, v := range data {\n\t\tif k == \"\" {\n\t\t\treturn errors.New(\"key can not be empty\")\n\t\t}\n\n\t\tif mustValuesBeScalar {\n\t\t\tswitch v.(type) {\n\t\t\tcase []any, map[string]any:\n\t\t\t\treturn errors.New(\"values must be scalar\")\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (env *Environment) Validate() (err error) {\n\tif env.Name == \"\" {\n\t\terr = common_errors.NewValidationError(\"Environment name can not be empty\")\n\t\treturn\n\t}","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/semaphoreui/semaphore/blob/1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa/db/Environment.go#L76-L112","documentation":"validateJSON iterates the parsed environment JSON object and rejects it if any key is the empty string. Empty keys cannot be exported as environment variables, so an entry like {\"\": \"value\"} is caught during environment validation rather than failing silently at job runtime.","triggerScenarios":"Environment payload JSON containing an entry with an empty key (e.g. {\"\": \"...\"} or {\" \": ...} is not caught, but literal \"\" is), typically produced by templating where the variable name variable is empty.","commonSituations":"Ansible/Jinja templating emitting entries for undefined variable names; CSV-to-JSON converters producing empty keys for blank rows; manual editing mistakes in the environment JSON editor.","solutions":["Remove entries with empty keys from the environment JSON payload","Templating tools: skip or fail on empty variable names instead of emitting an empty key","Validate keys with a quick client-side check (all object keys non-empty) before calling the API"],"exampleFix":"// before\n{\"\": \"value\"}\n// after\n{\"MY_VAR\": \"value\"}","handlingStrategy":"validation","validationCode":"for k := range payload {\n\tif k == \"\" {\n\t\treturn errors.New(\"environment payload contains an empty key\")\n\t}\n}","typeGuard":"func hasNoEmptyKeys(m map[string]any) bool {\n\tfor k := range m {\n\t\tif k == \"\" { return false }\n\t}\n\treturn true\n}","tryCatchPattern":"if err := env.Validate(); err != nil {\n\tif strings.Contains(err.Error(), \"key can not be empty\") {\n\t\treturn fmt.Errorf(\"environment rejected: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Skip undefined variable names in templating instead of emitting empty keys","Check converters/generators for rows that produce blank keys","Sanitize keys (trim + reject empty) before building the payload"],"tags":["go","json","validation","environment"],"backgroundTag":"empty-required-field","analyzedSha":"1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa","analyzedAt":"2026-09-07T11:00:33.293Z","contentChangedAt":"2026-09-07T11:00:33.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}