{"record":{"id":"e3f2cab7af01efa6","repo":"siyuan-note/siyuan","slug":"createdoctree-requires-at-least-one-document","errorCode":null,"errorMessage":"createDocTree requires at least one document","messagePattern":"createDocTree requires at least one document","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":112,"sourceCode":"\tnodes         []*TemplateDocTreeNode\n\tenabled       bool\n\tallowCreation bool\n\ttotalOutput   int\n}\n\nvar (\n\ttemplateDocTreePlans     sync.Map\n\ttemplateDocTreePlansLock sync.Mutex\n)\n\nfunc parseTemplateDocTreeDefinition(def any) ([]*TemplateDocTreeNode, error) {\n\tstate := &templateDocTreeParseState{}\n\tnodes, err := state.parseNodes(def, 1)\n\tif nil != err {\n\t\treturn nil, err\n\t}\n\tif 0 == len(nodes) {\n\t\treturn nil, errors.New(\"createDocTree requires at least one document\")\n\t}\n\treturn nodes, nil\n}\n\ntype templateDocTreeParseState struct {\n\tcount int\n}\n\nfunc (state *templateDocTreeParseState) parseNodes(value any, depth int) ([]*TemplateDocTreeNode, error) {\n\tif maxTemplateDocTreeDepth < depth {\n\t\treturn nil, fmt.Errorf(\"createDocTree exceeds the maximum depth of %d\", maxTemplateDocTreeDepth)\n\t}\n\tvalues, ok := value.([]any)\n\tif !ok {\n\t\treturn nil, errors.New(\"createDocTree definition must be a list\")\n\t}\n\tif 0 == len(values) {\n\t\treturn nil, errors.New(\"createDocTree document list must not be empty\")","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L94-L130","documentation":"parseTemplateDocTreeDefinition parses the createDocTree definition (a nested list/dictionary structure declaring child documents). After recursively parsing nodes it requires at least one document node at the top level; a definition that parses to an empty node list is rejected. Empty lists are also rejected earlier inside parseNodes.","triggerScenarios":"Calling createDocTree with def = nil, def = [] (JSON empty array), or a value that yields zero nodes (e.g. an empty string form that was normalized to an empty list) so len(nodes)==0 after parseNodes returns.","commonSituations":"Frontend submitting the doc-tree dialog with no documents added; programmatically building the definition and forgetting to append the first node; an API consumer sending an empty array to 'validate' a request.","solutions":["Include at least one document object, e.g. [{\"title\": \"Child doc\"}].","If def is nil or empty in your client, skip the createDocTree call instead of sending an empty plan.","Check that your serialization of the tree definition is not dropping nodes (e.g. wrong JSON field name)."],"exampleFix":"// before\n{\"def\": []}\n// after\n{\"def\": [{\"title\": \"Chapter 1\"}]}","handlingStrategy":"validation","validationCode":"function hasAtLeastOneDoc(def) {\n  return Array.isArray(def) && def.length > 0;\n}\nif (!hasAtLeastOneDoc(def)) return; // skip the call instead of sending an empty plan","typeGuard":null,"tryCatchPattern":"try {\n  await createDocTree(payload);\n} catch (e) {\n  if (String(e).includes(\"at least one document\")) {\n    // treat as no-op: nothing to create\n  } else { throw e; }\n}","preventionTips":["Guard client code to skip createDocTree when the user added no documents","Assert the serialized definition contains >= 1 node before sending","Check that your node serialization isn't dropping everything (wrong field names)"],"tags":["empty-input","validation","templates"],"backgroundTag":"empty-required-field","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}