{"id":"b41fd4735bd9f4a4","repo":"sindresorhus/is","slug":"expected-value-which-is-whitespace-string-recei","errorCode":null,"errorMessage":"Expected value which is `whitespace string`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `whitespace string`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":2008,"sourceCode":"}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakRef<T extends object = object>(value: unknown, message?: string): asserts value is WeakRef<T> {\n\tif (!isWeakRef(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('WeakRef', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakSet<T extends object = object>(value: unknown, message?: string): asserts value is WeakSet<T> {\n\tif (!isWeakSet(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('WeakSet', value));\n\t}\n}\n\nexport function assertWhitespaceString(value: unknown, message?: string): asserts value is string {\n\tif (!isWhitespaceString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('whitespace string', value));\n\t}\n}\n\nexport default is;\n\nexport type {\n\tArrayLike,\n\tClass,\n\tEvenInteger,\n\tFiniteNumber,\n\tInteger,\n\tNaN,\n\tNegativeInfinity,\n\tNegativeInteger,\n\tNegativeNumber,\n\tNodeStream,\n\tNonNegativeInteger,\n\tNonNegativeNumber,","sourceCodeStart":1990,"sourceCodeEnd":2026,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1990-L2026","documentation":"Thrown by `assertWhitespaceString()` when the value is not a non-empty string consisting entirely of whitespace. The underlying guard is `isString(value) && /^\\s+$/v.test(value)`, so it rejects non-strings, the empty string (the regex requires at least one character), and any string containing a non-whitespace character. It exists to distinguish 'blank but not empty' strings — a different condition from `isEmptyString` or the combined `isEmptyStringOrWhitespace`.","triggerScenarios":"Calling `assertWhitespaceString(value)` (or `is.assert.whitespaceString(value)`) with a non-string, with `''` (empty fails the `+` quantifier), or with a string containing any visible character, e.g. `'  a  '`. Only strings like `' '`, `'\\t\\n'` pass. Note the regex uses the `v` flag, which requires Node.js 20+/modern engines — on older runtimes the failure would be a SyntaxError at load, not this TypeError.","commonSituations":"Usually hit in input-sanitization or linting-style code that checks whether user input is blank: developers expect `''` to count as whitespace, but this assertion deliberately excludes the empty string. Also hit when trimmed input is passed (trimming removes the whitespace, yielding `''`).","solutions":["If empty strings should also pass, use `assertEmptyStringOrWhitespace` instead — that is the guard meaning 'blank'.","Ensure the value is a string before asserting; coerce or validate upstream if it may be a number, null, or undefined.","Don't trim before the assertion — `value.trim()` turns a whitespace string into an empty one, which fails.","If you actually want 'has meaningful content', invert with `assertNonEmptyStringAndNotWhitespace` rather than negating this assertion."],"exampleFix":"// before\nassertWhitespaceString(''); // throws — empty string is not whitespace\n\n// after\nassertEmptyStringOrWhitespace(''); // passes: covers both empty and whitespace-only","handlingStrategy":"validation","validationCode":"const isWhitespaceString = (value: unknown): boolean =>\n  typeof value === 'string' && value.length > 0 && /^\\s+$/.test(value);\n\nif (isWhitespaceString(value)) {\n  assert.whitespaceString(value); // safe\n}","typeGuard":"import is from '@sindresorhus/is';\n\nfunction ensureWhitespaceString(value: unknown): string | undefined {\n  return is.whitespaceString(value) ? value : undefined;\n}","tryCatchPattern":"try {\n  assert.whitespaceString(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // value was not a whitespace-only string\n    handleInvalidInput(error.message);\n  } else {\n    throw error;\n  }\n}","preventionTips":["Note the assertion requires a NON-EMPTY string consisting entirely of whitespace — the empty string '' fails it; check value.length > 0 first","Do not assume trimmed input passes: any visible character makes the string non-whitespace","If you actually want 'empty or whitespace' semantics, use is.emptyStringOrWhitespace instead of this stricter check","Guard with is.whitespaceString(value) before asserting when the string comes from user input or external data"],"tags":["typescript","type-assertion","string-validation","whitespace","runtime-validation","sindresorhus-is"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}