{"record":{"id":"e853896d646dda96","repo":"hashicorp/nomad","slug":"service-d-q-validation-failed-s","errorCode":null,"errorMessage":"service[%d] %+q validation failed: %s","messagePattern":"service\\[(.+?)\\] %\\+q validation failed: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/structs.go","lineNumber":8481,"sourceCode":"\n// validateServices takes a task and validates the services within it are valid\n// and reference ports that exist.\nfunc validateServices(t *Task, tgNetworks Networks) error {\n\tvar mErr multierror.Error\n\n\t// Ensure that services don't ask for nonexistent ports and their names are\n\t// unique.\n\tservicePorts := make(map[string]map[string]struct{})\n\taddServicePort := func(label, service string) {\n\t\tif _, ok := servicePorts[label]; !ok {\n\t\t\tservicePorts[label] = map[string]struct{}{}\n\t\t}\n\t\tservicePorts[label][service] = struct{}{}\n\t}\n\tknownServices := make(map[string]struct{})\n\tfor i, service := range t.Services {\n\t\tif err := service.Validate(); err != nil {\n\t\t\touter := fmt.Errorf(\"service[%d] %+q validation failed: %s\", i, service.Name, err)\n\t\t\tmErr.Errors = append(mErr.Errors, outer)\n\t\t}\n\n\t\tif service.AddressMode == AddressModeAlloc {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"service %q cannot use address_mode=\\\"alloc\\\", only services defined in a \\\"group\\\" block can use this mode\", service.Name))\n\t\t}\n\n\t\tif service.AddressMode == AddressModeAllocIPv6 {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"service %q cannot use address_mode=\\\"alloc_ipv6\\\", only services defined in a \\\"group\\\" block can use this mode\", service.Name))\n\t\t}\n\n\t\t// Ensure that services with the same name are not being registered for\n\t\t// the same port\n\t\tif _, ok := knownServices[service.Name+service.PortLabel]; ok {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"service %q is duplicate\", service.Name))\n\t\t}\n\t\tknownServices[service.Name+service.PortLabel] = struct{}{}\n","sourceCodeStart":8463,"sourceCodeEnd":8499,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/structs.go#L8463-L8499","documentation":"In Task.Validate, each entry of t.Services is validated by Service.Validate; failures are collected as 'service[%d] %+q validation failed: %s' with the service index, name, and wrapped validation errors. It aggregates any problem with a service definition: bad name characters, missing checks fields, invalid address_mode, invalid provider, etc.","triggerScenarios":"A task service block failing Service.Validate — e.g. name with invalid characters for Consul/Nomad DNS, check with both address_mode issues, port label not defined, unsupported provider value, or invalid upstream config in connect blocks.","commonSituations":"Service names containing slashes or characters Consul rejects; port labels referencing ports not declared in the task's resources; typos in provider (\"consul\" vs \"nomad\"); service blocks copied between group and task level where address_mode=\"alloc\" is invalid (that case also yields the follow-up alloc-mode error).","solutions":["Read the wrapped %s error after 'validation failed:' for the concrete field problem","Fix the service name to use valid characters and declare the referenced port in resources","Correct provider/check/address_mode fields per docs and run `nomad job validate` before submitting"],"exampleFix":"// before\nservice {\n  name = \"my/app\"\n  port = \"http\"\n}\n// after\nservice {\n  name = \"my-app\"\n  port = \"http\"\n}","handlingStrategy":"validation","validationCode":"for i, svc := range t.Services {\n    if err := svc.Validate(); err != nil {\n        return fmt.Errorf(\"service[%d] %q bad: %w\", i, svc.Name, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := job.Validate(); err != nil {\n    // match 'service[N] \"name\" validation failed:' and report the inner cause\n}","preventionTips":["Use DNS-safe service names (no slashes/spaces)","Ensure every service port label exists in the task's resources ports","Validate provider and check configuration against your Nomad/Consul version","Gate submissions on `nomad job validate` in CI"],"tags":["nomad","service","validation","consul"],"backgroundTag":"service-validation-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}