{"record":{"id":"206f7549cb24bb91","repo":"siyuan-note/siyuan","slug":"field-s-is-required-block-remaining","errorCode":null,"errorMessage":"Field [%s] is required","messagePattern":"Field \\[(.+?)\\] is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/apicontract/block_remaining.go","lineNumber":85,"sourceCode":"\tfields, err := blockRequestFields(reader, \"/api/block/checkBlockRef\")\n\tif err != nil {\n\t\treturn request, err\n\t}\n\tif raw, present := fields[\"scope\"]; present {\n\t\tif bytes.Equal(raw, []byte(\"null\")) || json.Unmarshal(raw, &request.Scope) != nil {\n\t\t\treturn request, errors.New(\"Field [scope] should be of type [String]\")\n\t\t}\n\t}\n\tif strings.TrimSpace(request.Scope) == \"\" {\n\t\trequest.Scope = \"blocks\"\n\t}\n\treadArray := func(key string, required, nonempty bool) ([]string, error) {\n\t\traw, present := fields[key]\n\t\tif !present && !required {\n\t\t\treturn nil, nil\n\t\t}\n\t\tif !present || bytes.Equal(raw, []byte(\"null\")) {\n\t\t\treturn nil, fmt.Errorf(\"Field [%s] is required\", key)\n\t\t}\n\t\tvar entries []json.RawMessage\n\t\tif json.Unmarshal(raw, &entries) != nil {\n\t\t\treturn nil, fmt.Errorf(\"Field [%s] should be of type [Array]\", key)\n\t\t}\n\t\tif nonempty && len(entries) == 0 {\n\t\t\treturn nil, fmt.Errorf(\"Field [%s] must not be empty\", key)\n\t\t}\n\t\tvar values []string\n\t\tseen := map[string]bool{}\n\t\tfor _, entry := range entries {\n\t\t\tvar value string\n\t\t\tif json.Unmarshal(entry, &value) != nil || strings.TrimSpace(value) == \"\" {\n\t\t\t\treturn nil, fmt.Errorf(\"Field [%s] should contain non-empty strings\", key)\n\t\t\t}\n\t\t\tif !seen[value] {\n\t\t\t\tvalues = append(values, value)\n\t\t\t\tseen[value] = true","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/apicontract/block_remaining.go#L67-L103","documentation":"The checkBlockRef decoder's readArray helper treats a missing key or an explicit JSON null for a required field as an error: 'Field [key] is required'. Required id-array fields must be present and non-null; empty arrays may additionally be rejected when the nonempty flag is set.","triggerScenarios":"POSTing to /api/block/checkBlockRef without the ids field ({}) , with {\"ids\":null}, or omitting another required array field the decoder asks for via readArray(key, true, ...).","commonSituations":"Clients serializing undefined values as null (JSON.stringify drops undefined but libraries often emit null), payloads built conditionally where the field was skipped, or API version drift where a formerly optional field became required.","solutions":["Include the required field as a JSON array in the body, e.g. {\"ids\":[\"20240101120000-abcdefg\"]}.","Replace null with an empty array if an empty set is acceptable, or omit the call when there is nothing to check.","Fix client serialization so undefined/None fields are dropped or defaulted, not sent as null.","Check the endpoint's current contract — the field may have become required after an update."],"exampleFix":"// before\n{\"ids\": null}\n// after\n{\"ids\": [\"20240101120000-abcdefg\", \"20240101120000-hijklmn\"]}","handlingStrategy":"validation","validationCode":"const required = [\"ids\"];\nfor (const k of required) {\n  if (!Array.isArray(payload[k])) { throw new Error(`field ${k} must be present and an array`); }\n}","typeGuard":"const isPresentArray = (v) => Array.isArray(v);","tryCatchPattern":"try {\n  await post(\"/api/block/checkBlockRef\", payload);\n} catch (e) {\n  const m = String(e.message).match(/Field \\[(\\w+)\\] is required/);\n  if (m) { payload[m[1]] = payload[m[1]] ?? []; await post(\"/api/block/checkBlockRef\", payload); }\n  else { throw e; }\n}","preventionTips":["Configure serializers to drop undefined instead of emitting null","Always initialize required array fields to [] before building the payload","Schema-validate payloads against the endpoint contract in CI","Re-check field requirements after API version upgrades"],"tags":["validation","required-fields","json","api"],"backgroundTag":"missing-required-argument","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"}