{"record":{"id":"b842b62dee785373","repo":"langgenius/dify","slug":"name-must-be-a-non-empty-string","errorCode":null,"errorMessage":"${name} must be a non-empty string","messagePattern":"(.+?) must be a non-empty string","errorType":"validation","errorClass":"ValidationError","httpStatus":null,"severity":"error","filePath":"sdks/nodejs-client/src/client/validation.ts","lineNumber":10,"sourceCode":"import { ValidationError } from '../errors/dify-error'\nimport { isRecord } from '../internal/type-guards'\n\nconst MAX_STRING_LENGTH = 10000\nconst MAX_LIST_LENGTH = 1000\nconst MAX_DICT_LENGTH = 100\n\nexport function ensureNonEmptyString(value: unknown, name: string): asserts value is string {\n  if (typeof value !== 'string' || value.trim().length === 0) {\n    throw new ValidationError(`${name} must be a non-empty string`)\n  }\n  if (value.length > MAX_STRING_LENGTH) {\n    throw new ValidationError(`${name} exceeds maximum length of ${MAX_STRING_LENGTH} characters`)\n  }\n}\n\n/**\n * Validates optional string fields that must be non-empty when provided.\n * Use this for fields like `name` that are optional but should not be empty strings.\n *\n * For filter parameters that accept empty strings (e.g., `keyword: \"\"`),\n * use `validateParams` which allows empty strings for optional params.\n */\nexport function ensureOptionalString(value: unknown, name: string): void {\n  if (value === undefined || value === null) {\n    return\n  }\n  if (typeof value !== 'string' || value.trim().length === 0) {","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/sdks/nodejs-client/src/client/validation.ts#L1-L28","documentation":"Thrown by ensureNonEmptyString() in validation.ts:10 as a ValidationError. This is the SDK's workhorse guard for required string parameters: datasetId, documentId, fileId, messageId, user, name, etc. The value must be a string of length ≥ 1 after trim(); anything else (number, undefined, null, '', '   ') fails.","triggerScenarios":"Calling any SDK method with a required identifier that is undefined, empty, whitespace-only, or a non-string type. Examples: client.filePreview('', user); kb.getDataset(undefined); client.messageFeedback(req) where req.user is blank; createDataset({ name: '   ' }).","commonSituations":"Reading an id from an env var or route param that was not set; copying a payload from logs and dropping a field; passing a numeric id where a string is expected; user identifier loaded from a session that is null.","solutions":["Ensure the value is a non-empty trimmed string at the call site: if (!datasetId?.trim()) throw new Error('datasetId required').","Pull ids from a typed source (route params typed as string, DB rows cast with String(id)) rather than untyped objects.","Default missing user identifiers to a stable non-empty value or fail fast in your handler before reaching the SDK.","For server response fields, read from the documented path (e.g. response.id) rather than guessing."],"exampleFix":"// before\nawait kb.getDataset(row.datasetId) // row.datasetId is undefined\n\n// after\nif (!row.datasetId) throw new Error('datasetId missing on row')\nawait kb.getDataset(row.datasetId)","handlingStrategy":"validation","validationCode":"function requireNonEmptyString(value: unknown, name: string): string {\n  if (typeof value !== 'string' || value.trim().length === 0) {\n    throw new Error(`${name} is required`)\n  }\n  return value\n}","typeGuard":"function isNonEmptyString(value: unknown): value is string {\n  return typeof value === 'string' && value.trim().length > 0\n}","tryCatchPattern":"try {\n  await kb.getDataset(datasetId)\n} catch (err) {\n  if (err instanceof Error && err.name === 'ValidationError' && /must be a non-empty string/.test(err.message)) {\n    // return 400 to caller with field name extracted\n  } else throw err\n}","preventionTips":["Validate route params at the handler boundary before calling the SDK.","Type all id parameters as string (not string | undefined).","Read ids from server responses using the documented path."],"tags":["validation","required-field","string","identifiers","guard"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}