{"record":{"id":"81f250402e372864","repo":"dgraph-io/dgraph","slug":"expected-an-int-but-got-v","errorCode":null,"errorMessage":"Expected an int but got %v","messagePattern":"Expected an int but got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"dql/parser.go","lineNumber":319,"sourceCode":"}\n\n// Request stores the query text and the variable mapping.\ntype Request struct {\n\tStr       string\n\tVariables map[string]string\n}\n\nfunc parseValue(v varInfo) (types.Val, error) {\n\ttyp := v.Type\n\tif v.Value == \"\" {\n\t\treturn types.Val{}, errors.Errorf(\"No value found\")\n\t}\n\n\tswitch typ {\n\tcase \"int\":\n\t\t{\n\t\t\tif i, err := strconv.ParseInt(v.Value, 0, 64); err != nil {\n\t\t\t\treturn types.Val{}, errors.Wrapf(err, \"Expected an int but got %v\", v.Value)\n\t\t\t} else {\n\t\t\t\treturn types.Val{\n\t\t\t\t\tTid:   types.IntID,\n\t\t\t\t\tValue: i,\n\t\t\t\t}, nil\n\t\t\t}\n\t\t}\n\tcase \"float\":\n\t\t{\n\t\t\tif i, err := strconv.ParseFloat(v.Value, 64); err != nil {\n\t\t\t\treturn types.Val{}, errors.Wrapf(err, \"Expected a float but got %v\", v.Value)\n\t\t\t} else {\n\t\t\t\treturn types.Val{\n\t\t\t\t\tTid:   types.FloatID,\n\t\t\t\t\tValue: i,\n\t\t\t\t}, nil\n\t\t\t}\n\t\t}","sourceCodeStart":301,"sourceCodeEnd":337,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/dql/parser.go#L301-L337","documentation":"parseValue parses a variable declared (or inferred) as type \"int\" using strconv.ParseInt(v.Value, 0, 64). If the string is not a valid integer literal, the parse error is wrapped with 'Expected an int but got %v' showing the offending value.","triggerScenarios":"Passing a variable whose Type is \"int\" but whose Value string isn't a valid integer — e.g. \"$age\": \"abc\", \"12.5\", or \"1e3\" in the variables map of a parameterized query.","commonSituations":"Sending JSON numbers as raw strings from clients with locale formatting (\"1,000\"); passing float values where an int is declared; user input not sanitized before being placed in the variables map.","solutions":["Ensure the variable's string value is a valid Go-parseable integer (digits, optional sign, optional 0x/0b prefixes since base 0 is used)","Fix the declared type: if the value is a decimal like 12.5, declare it as \"float\" instead of \"int\"","Validate/convert on the client with strconv.ParseInt (or equivalent) before submitting","Read the wrapped inner error and the %v value in the message to see which variable value failed"],"exampleFix":"// before\nvars := map[string]string{\"$age\": \"12.5\"} // declared int\n// after\nvars := map[string]string{\"$age\": \"12\"} // or declare $age as float","handlingStrategy":"validation","validationCode":"if _, err := strconv.ParseInt(vars[\"$age\"], 0, 64); err != nil {\n  return fmt.Errorf(\"$age must be an integer: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"_, err := dql.ParseWithNeedVars(query, vars)\nif err != nil && strings.Contains(err.Error(), \"Expected an int\") {\n  return fmt.Errorf(\"bad int variable: %w\", err)\n}","preventionTips":["Pre-parse and normalize variable strings with strconv on the client","Declare float-typed values as \"float\", not \"int\"","Strip locale formatting (thousands separators) before submitting"],"tags":["dgraph","dql","variables","type-parsing","int"],"backgroundTag":"query-variable-type-mismatch","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}