{"id":"db73ec21b3de0ae8","repo":"sindresorhus/is","slug":"expected-value-which-is-valid-length-received-v","errorCode":null,"errorMessage":"Expected value which is `valid length`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `valid length`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1981,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('URLSearchParams', value));\n\t}\n}\n\nexport function assertUrlString(value: unknown, message?: string): asserts value is UrlString {\n\tif (!isUrlString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('string with a URL', value));\n\t}\n}\n\nexport function assertValidDate(value: unknown, message?: string): asserts value is Date {\n\tif (!isValidDate(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('valid Date', value));\n\t}\n}\n\nexport function assertValidLength(value: unknown, message?: string): asserts value is number {\n\tif (!isValidLength(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('valid length', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertWeakMap<Key extends object = object, Value = unknown>(value: unknown, message?: string): asserts value is WeakMap<Key, Value> {\n\tif (!isWeakMap(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('WeakMap', value));\n\t}\n}\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","sourceCodeStart":1963,"sourceCodeEnd":1999,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1963-L1999","documentation":"Thrown by `assertValidLength()` in @sindresorhus/is when the value fails `isValidLength`, i.e. it is not a safe integer >= 0 (`Number.isSafeInteger(value) && value >= 0`). It validates values meant to be used as array/string lengths or counts.","triggerScenarios":"`assert.validLength(value)` with a negative number, a float (e.g. 2.5), NaN, Infinity, a number beyond Number.MAX_SAFE_INTEGER, a numeric string like '5', or a BigInt.","commonSituations":"Lengths computed by division producing floats (`total / pageSize`); parsing lengths from headers or query strings without Number conversion; offset arithmetic underflowing to negative values; Content-Length beyond safe-integer range.","solutions":["Convert and round appropriately: `Math.floor(value)` or `Math.trunc(value)` for computed lengths, and clamp with `Math.max(0, value)` if underflow is expected.","Parse strings to numbers first: `Number.parseInt(str, 10)` and validate the result isn't NaN.","Find the arithmetic that produced the negative/fractional value — the assertion usually flags an upstream logic bug (off-by-one, division), not a formatting problem."],"exampleFix":"// before\nconst pages = total / pageSize;\nassert.validLength(pages);\n// after\nconst pages = Math.ceil(total / pageSize);\nassert.validLength(pages);","handlingStrategy":"validation","validationCode":"function isValidLength(value) {\n  return Number.isSafeInteger(value) && value >= 0;\n}\nif (!isValidLength(len)) {\n  throw new RangeError(`Invalid length: ${len}`);\n}","typeGuard":"if (is.validLength(value)) {\n  // value is a non-negative safe integer\n}","tryCatchPattern":"try {\n  assert.validLength(value);\n} catch (error) {\n  if (error instanceof TypeError) { /* negative, fractional, NaN, or unsafe integer */ }\n  throw error;\n}","preventionTips":["A valid length is a non-negative safe integer — guard against NaN, negatives, fractions, and Infinity from arithmetic","Validate lengths derived from user input or division/subtraction before using them to size arrays or slices","Use Number.isSafeInteger(n) && n >= 0 as the equivalent pre-check"],"tags":["typescript","type-assertion","number","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}