{"record":{"id":"4c1eeaa48b044215","repo":"mikefarah/yq","slug":"cannot-trim-v-can-only-operate-on-strings","errorCode":null,"errorMessage":"cannot trim %v, can only operate on strings. ","messagePattern":"cannot trim (.+?), can only operate on strings\\. ","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_strings.go","lineNumber":147,"sourceCode":"\t\tcandidate := el.Value.(*CandidateNode)\n\t\tvalue, err := interpolate(d, context.SingleChildContext(candidate), expressionNode.Operation.StringValue)\n\t\tif err != nil {\n\t\t\treturn Context{}, err\n\t\t}\n\t\tnode := createScalarNode(value, value)\n\t\tresults.PushBack(node)\n\t}\n\n\treturn context.ChildContext(results), nil\n}\n\nfunc trimSpaceOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {\n\tresults := list.New()\n\tfor el := context.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tnode := el.Value.(*CandidateNode)\n\n\t\tif node.guessTagFromCustomType() != \"!!str\" {\n\t\t\treturn Context{}, fmt.Errorf(\"cannot trim %v, can only operate on strings. \", node.Tag)\n\t\t}\n\n\t\tnewStringNode := node.CreateReplacement(ScalarNode, node.Tag, strings.TrimSpace(node.Value))\n\t\tnewStringNode.Style = node.Style\n\t\tresults.PushBack(newStringNode)\n\n\t}\n\n\treturn context.ChildContext(results), nil\n}\n\nfunc toStringOperator(_ *dataTreeNavigator, context Context, _ *ExpressionNode) (Context, error) {\n\tresults := list.New()\n\tfor el := context.MatchingNodes.Front(); el != nil; el = el.Next() {\n\t\tnode := el.Value.(*CandidateNode)\n\t\tvar newStringNode *CandidateNode\n\t\tif node.Tag == \"!!str\" {\n\t\t\tnewStringNode = node.CreateReplacement(ScalarNode, \"!!str\", node.Value)","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_strings.go#L129-L165","documentation":"trim_space (and the ltrimstr/rtrimstr family) requires string input: the guard checks each matched node's resolved tag via guessTagFromCustomType and rejects anything that is not !!str (custom string-like tags are unwrapped first). The node's actual tag replaces the trailing %v in the message.","triggerScenarios":"Thrown at pkg/yqlib/operator_strings.go:147 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Convert first: . | tostring | trim","Select the string field rather than a number/map","Check the value is not null or boolean before trimming"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}