{"record":{"id":"e9abc6384808d5d1","repo":"validatorjs/validator.js","slug":"expected-a-string-but-received-a-input","errorCode":null,"errorMessage":"Expected a string but received a ${input}","messagePattern":"Expected a string but received a (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lib/util/assertString.js","lineNumber":2,"sourceCode":"export default function assertString(input) {\n  if (input === undefined || input === null) throw new TypeError(`Expected a string but received a ${input}`);\n  if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`);\n}\n","sourceCodeStart":1,"sourceCodeEnd":5,"githubUrl":"https://github.com/validatorjs/validator.js/blob/a79ff980ab14257e795332989e497bdff3218e87/src/lib/util/assertString.js#L1-L5","documentation":"assertString is validator.js's internal type assertion run at the top of nearly every public API (isAlpha, contains, equals, blacklist, isAbaRouting, etc.). It throws a TypeError when the input is undefined or null, interpolating the value itself into the message ('Expected a string but received a undefined' / 'a null'). The library requires all string arguments to be actual strings and refuses to coerce.","triggerScenarios":"Calling any validator with an argument that is undefined or null, e.g. validator.isAlpha(req.body.name) where the field is missing, validator.equals(undefined, 'x'), validator.contains(query, null), or passing the result of a lookup that returned undefined.","commonSituations":"Missing request body fields / query params in Express apps; JSON.parse yielding null; optional object properties left undefined; database rows with NULL columns; refactored call sites where an argument was dropped; default parameters not applied because null was passed explicitly.","solutions":["Coerce or default the value before calling: (input ?? '') or (input || '') when an empty string is an acceptable interpretation.","Add an explicit null/undefined check at the call site and branch to your own 'missing value' handling instead of calling the validator.","Fix the data source: use required-field validation on request input (e.g. express-validator, zod) so missing fields never reach the validator.","Inspect the stack trace to find which argument is undefined/null and correct the variable assignment or function signature."],"exampleFix":"// before\nvalidator.isAlpha(req.query.name); // throws when ?name is absent\n// after\nconst name = req.query.name ?? '';\nif (name === '') {\n  res.status(400).json({ error: 'name is required' });\n} else if (validator.isAlpha(name)) {\n  // ...\n}","handlingStrategy":"type-guard","validationCode":"function isNonEmptyStringArg(v) {\n  return typeof v === 'string';\n}\n// usage\nif (!isNonEmptyStringArg(input)) {\n  throw new TypeError('input must be a string');\n}\nreturn validator.isAlpha(input, locale);","typeGuard":"function isString(v) {\n  return typeof v === 'string';\n}\n// narrows: if (isString(input)) { validator.contains(haystack, input); }","tryCatchPattern":"try {\n  return validator.isAlpha(input);\n} catch (e) {\n  if (e instanceof TypeError && e.message.startsWith('Expected a string but received a')) {\n    return false; // or report a missing-field error upstream\n  }\n  throw e;\n}","preventionTips":["Apply nullish coalescing (?? '') at boundaries (HTTP input, DB rows, env vars) before validation.","Validate required request fields with a schema (zod/joi/express-validator) so null/undefined is rejected before reaching validators.","Never pass optional values directly; destructure with defaults: const { name = '' } = req.body.","Enable TypeScript strictNullChecks so null/undefined cannot reach the call site untyped."],"tags":["typeerror","null","undefined","type-check","validator"],"backgroundTag":"expected-string-received-null-undefined","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}