{"record":{"id":"70ad562a17e74c54","repo":"gohugoio/hugo","slug":"can-t-evaluate-an-invalid-value","errorCode":null,"errorMessage":"can't evaluate an invalid value","messagePattern":"can't evaluate an invalid value","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/where.go","lineNumber":314,"sourceCode":"\t\tif svp != nil && smvp != nil {\n\t\t\tre, err := hstrings.GetOrCompileRegexp(*smvp)\n\t\t\tif err != nil {\n\t\t\t\treturn false, err\n\t\t\t}\n\t\t\tif re.MatchString(*svp) {\n\t\t\t\treturn true, nil\n\t\t\t}\n\t\t\treturn false, nil\n\t\t}\n\tdefault:\n\t\treturn false, errors.New(\"no such operator\")\n\t}\n\treturn false, nil\n}\n\nfunc evaluateSubElem(ctx, obj reflect.Value, elemName string) (reflect.Value, error) {\n\tif !obj.IsValid() {\n\t\treturn zero, errors.New(\"can't evaluate an invalid value\")\n\t}\n\n\ttyp := obj.Type()\n\tobj, isNil := hreflect.Indirect(obj)\n\n\t// first, check whether obj has a method. In this case, obj is\n\t// a struct or its pointer. If obj is a struct,\n\t// to check all T and *T method, use obj pointer type Value\n\tobjPtr := obj\n\tif !hreflect.IsInterfaceOrPointer(objPtr.Kind()) && objPtr.CanAddr() {\n\t\tobjPtr = objPtr.Addr()\n\t}\n\n\tmt := hreflect.GetMethodByNameForType(objPtr.Type(), elemName)\n\tif mt.Func.IsValid() {\n\t\t// Receiver is the first argument.\n\t\targs := []reflect.Value{objPtr}\n\t\tnum := mt.Type.NumIn()","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/where.go#L296-L332","documentation":"The `where` and `sort` functions walk a dotted key path (e.g. `.Author.Name`) by calling `evaluateSubElem` for each segment. If an intermediate reflect.Value is not valid (`!obj.IsValid()`), the field/method/map navigation cannot proceed and this error fires. An invalid Value typically results from a missing map key or an absent struct field on a prior path segment.","triggerScenarios":"Calling `{{ where .Pages \"Author.Name\" \"eq\" \"Alice\" }}` where some pages have no `Author` map (so `.Author` yields an invalid zero Value), or using a dotted path where a middle segment does not exist on the element type.","commonSituations":"Heterogeneous collections where some items have a nested field and others do not. The developer expects missing keys to be skipped but the evaluation short-circuits on the invalid intermediate value.","solutions":["Ensure all elements share the same structure for the given key path.","Use a single-level key or flatten the data so the path always resolves.","Filter the collection first to items that have the nested field, then apply `where`."],"exampleFix":"// before\n{{ where .Pages \"Author.Name\" \"eq\" \"Alice\" }}\n// after\n{{ $withAuthor := where .Pages \"Author\" \"ne\" nil }}\n{{ where $withAuthor \"Author.Name\" \"eq\" \"Alice\" }}","handlingStrategy":"validation","validationCode":"// Pre-filter elements that have the nested key before applying a dotted path\n{{ $hasKey := where .Pages \"Author\" \"ne\" nil }}\n{{ where $hasKey \"Author.Name\" \"eq\" \"Alice\" }}","typeGuard":"func pathResolvesOnAll(elemType reflect.Type, path []string) bool {\n    t := elemType\n    if t.Kind() == reflect.Pointer { t = t.Elem() }\n    for _, seg := range path {\n        switch t.Kind() {\n        case reflect.Struct:\n            f, ok := t.FieldByName(seg)\n            if !ok { return false }\n            t = f.Type\n        case reflect.Map:\n            t = t.Elem()\n        default:\n            return false\n        }\n        if t.Kind() == reflect.Pointer { t = t.Elem() }\n    }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Ensure all elements share the structure needed by the dotted key path.","Pre-filter to elements that have the top-level key before drilling deeper.","Avoid dotted paths over heterogeneous collections."],"tags":["hugo","template","collections","where","key-path"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}