validatorjs/validator.js · error · TypeError

Expected a string but received a ${input}

Error message

Expected a string but received a ${input}

What it means

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.

Source

Thrown at src/lib/util/assertString.js:2

export default function assertString(input) {
  if (input === undefined || input === null) throw new TypeError(`Expected a string but received a ${input}`);
  if (input.constructor.name !== 'String') throw new TypeError(`Expected a string but received a ${input.constructor.name}`);
}

View on GitHub (pinned to a79ff980ab)

Solutions

  1. Coerce or default the value before calling: (input ?? '') or (input || '') when an empty string is an acceptable interpretation.
  2. Add an explicit null/undefined check at the call site and branch to your own 'missing value' handling instead of calling the validator.
  3. Fix the data source: use required-field validation on request input (e.g. express-validator, zod) so missing fields never reach the validator.
  4. Inspect the stack trace to find which argument is undefined/null and correct the variable assignment or function signature.

Example fix

// before
validator.isAlpha(req.query.name); // throws when ?name is absent
// after
const name = req.query.name ?? '';
if (name === '') {
  res.status(400).json({ error: 'name is required' });
} else if (validator.isAlpha(name)) {
  // ...
}
Defensive patterns

Strategy: type-guard

Validate before calling

function isNonEmptyStringArg(v) {
  return typeof v === 'string';
}
// usage
if (!isNonEmptyStringArg(input)) {
  throw new TypeError('input must be a string');
}
return validator.isAlpha(input, locale);

Type guard

function isString(v) {
  return typeof v === 'string';
}
// narrows: if (isString(input)) { validator.contains(haystack, input); }

Try / catch

try {
  return validator.isAlpha(input);
} catch (e) {
  if (e instanceof TypeError && e.message.startsWith('Expected a string but received a')) {
    return false; // or report a missing-field error upstream
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of validatorjs/validator.js@a79ff980ab (2026-08-31). Data as JSON: /api/errors/e9abc6384808d5d1. Report an issue: GitHub.