{"record":{"id":"12bbb57c4d37ce98","repo":"hashicorp/nomad","slug":"affinity-weight-must-be-within-the-range-100-100","errorCode":null,"errorMessage":"Affinity weight must be within the range [-100,100]","messagePattern":"Affinity weight must be within the range \\[-100,100\\]","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nomad/structs/structs.go","lineNumber":10237,"sourceCode":"\t\tif a.RTarget == \"\" {\n\t\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Operator %q requires an RTarget\", a.Operand))\n\t\t}\n\tdefault:\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Unknown affinity operator %q\", a.Operand))\n\t}\n\n\t// Ensure we have an LTarget\n\tif a.LTarget == \"\" {\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"No LTarget provided but is required\"))\n\t}\n\n\t// Ensure that weight is between -100 and 100, and not zero\n\tif a.Weight == 0 {\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Affinity weight cannot be zero\"))\n\t}\n\n\tif a.Weight > 100 || a.Weight < -100 {\n\t\tmErr.Errors = append(mErr.Errors, fmt.Errorf(\"Affinity weight must be within the range [-100,100]\"))\n\t}\n\n\treturn mErr.ErrorOrNil()\n}\n\n// DiffID fulfills the DiffableWithID interface.\nfunc (a *Affinity) DiffID() string {\n\treturn a.String()\n}\n\n// Spread is used to specify desired distribution of allocations according to weight\ntype Spread struct {\n\t// Attribute is the node attribute used as the spread criteria\n\tAttribute string\n\n\t// Weight is the relative weight of this spread, useful when there are multiple\n\t// spread and affinities\n\tWeight int8","sourceCodeStart":10219,"sourceCodeEnd":10255,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/nomad/structs/structs.go#L10219-L10255","documentation":"Affinity.Validate() bounds weight to [-100, 100]. If a.Weight is greater than 100 or less than -100, the job is rejected with 'Affinity weight must be within the range [-100,100]'. Weight controls how strongly the scheduler favors (positive) or disfavors (negative) matching nodes.","triggerScenarios":"Submitting a job (HCL or /v1/jobs API) whose affinity weight is outside [-100, 100], e.g. weight = 200 or weight = -150.","commonSituations":"Misreading the docs and using percentage-like values (0-100 only), script-generated weights from unbounded computations, or porting configs from tools with different weighting scales.","solutions":["Clamp the weight to the allowed range, e.g. weight = 100 for maximum preference.","Cap generated weights at ±100 before rendering the job spec.","If stronger preference is needed, add multiple affinities rather than exceeding the bound."],"exampleFix":"// before\naffinity {\n  attribute = \"${node.class}\"\n  value     = \"high-memory\"\n  weight    = 200\n}\n// after\naffinity {\n  attribute = \"${node.class}\"\n  value     = \"high-memory\"\n  weight    = 100\n}","handlingStrategy":"validation","validationCode":"if a.Weight > 100 || a.Weight < -100 { return fmt.Errorf(\"weight %d out of range [-100,100]\", a.Weight) }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp weights to [-100, 100] in job generators","Do not assume percentage scales beyond 100 are valid","Add unit tests asserting weight bounds in spec builders"],"tags":["nomad","affinity","weight","job-spec","validation"],"backgroundTag":"invalid-parameter-range","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"}