{"record":{"id":"2b72f52609c79456","repo":"validatorjs/validator.js","slug":"expected-a-string-but-received-a-input-construct","errorCode":null,"errorMessage":"Expected a string but received a ${input.constructor.name}","messagePattern":"Expected a string but received a (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lib/util/assertString.js","lineNumber":3,"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 throws this TypeError when the input is neither undefined nor null but its constructor name is not 'String' — i.e. a number, object, array, boolean, or a String object from another realm/way. It fires at the entry of most public validators (isAlpha, contains, equals, blacklist, isAbaRouting, etc.) to guarantee the argument is a primitive string, since the library does not coerce types.","triggerScenarios":"Passing a number (validator.isAlpha(12345)), a Date, a parsed JSON object/array, a Boolean, a String wrapper object (new String('abc')), or a cross-realm string (e.g. from vm/iframe) whose constructor.name differs, to any validator or sanitizer expecting a string.","commonSituations":"Numeric form/query inputs not converted to string before validation; Number() results fed to isInt-like checks that still require strings; MongoDB/ObjectId or Decimal objects used directly; template-free values from config files typed as numbers; code that worked with loosely-typed data later run through the validator unchanged.","solutions":["Convert explicitly with String(value) (or value.toString() for safe object types like ObjectId) before calling the validator.","For primitives other than strings, decide whether string conversion is semantically correct — String(123) is fine for isInt, but String({}) produces '[object Object]' and hides the bug.","Fix the typing at the source: enforce string types with TypeScript, schema validation (zod/joi), or form input coercion so non-strings never reach the validator.","Watch for String wrapper objects: use value.valueOf() or String(value) to unwrap new String('...') instances."],"exampleFix":"// before\nif (validator.isAlpha(zipCode)) { ... } // zipCode = 90210 (number) -> TypeError\n// after\nconst zip = typeof zipCode === 'string' ? zipCode : String(zipCode ?? '');\nif (validator.isAlpha(zip)) { ... }","handlingStrategy":"type-guard","validationCode":"function assertPrimitiveString(v, name = 'value') {\n  if (typeof v !== 'string') {\n    throw new TypeError(`${name} must be a primitive string, received: ${Object.prototype.toString.call(v)}`);\n  }\n  return v;\n}\n// usage: validator.isAbaRouting(assertPrimitiveString(routingNumber, 'routingNumber'))","typeGuard":"function isPrimitiveString(v) {\n  return typeof v === 'string';\n}\n// typeof excludes String wrapper objects too: typeof new String('a') === 'object'","tryCatchPattern":"try {\n  return validator.isAbaRouting(input);\n} catch (e) {\n  if (e instanceof TypeError && e.message.startsWith('Expected a string but received a')) {\n    console.error(`Non-string passed to validator: ${e.message}`);\n    return false;\n  }\n  throw e;\n}","preventionTips":["Convert numerics and objects explicitly (String(x)) at the boundary; avoid implicit coercion into validators.","Unwrap String wrapper objects with valueOf()/String() — prefer always storing primitive strings.","Use TypeScript or JSDoc types (@param {string}) so non-string arguments are caught at compile time.","Beware cross-realm strings (vm, iframes): route such values through String(value) before validation."],"tags":["typeerror","type-check","wrong-type","validator","input-error"],"backgroundTag":"expected-string-received-type","analyzedSha":"a79ff980ab14257e795332989e497bdff3218e87","analyzedAt":"2026-08-31T21:42:31.416Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}