{"id":"d30beba0110e299d","repo":"sindresorhus/is","slug":"expected-value-which-is-valid-date-received-val","errorCode":null,"errorMessage":"Expected value which is `valid Date`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `valid Date`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1975,"sourceCode":"\t}\n}\n\n// eslint-disable-next-line unicorn/prevent-abbreviations\nexport function assertUrlSearchParams(value: unknown, message?: string): asserts value is URLSearchParams {\n\tif (!isUrlSearchParams(value)) {\n\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> {","sourceCodeStart":1957,"sourceCodeEnd":1993,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1957-L1993","documentation":"Thrown by `assertValidDate()` in @sindresorhus/is when the value is not a Date instance whose time value is a real number — `isValidDate` requires `isDate(value) && !Number.isNaN(Number(value))`. It catches the classic 'Invalid Date' object, which IS a Date instance but represents no point in time.","triggerScenarios":"`assert.validDate(value)` with `new Date('not-a-date')` or any unparseable date string, a date string itself (strings always fail — only Date objects pass), a timestamp number, or null/undefined.","commonSituations":"Parsing user input or API date fields with `new Date(str)` where the string is empty, malformed, or in a locale format the engine rejects (e.g. 'DD/MM/YYYY'); undefined fields producing Invalid Date silently upstream.","solutions":["Validate/normalize input before constructing: parse known formats explicitly (ISO 8601 works everywhere: `new Date('2026-07-31')`) or use a date library for other formats.","If you have a timestamp or string, convert first: `new Date(timestamp)` — the assertion only accepts Date objects.","Trace where the Invalid Date was created — check the raw string that was fed to `new Date()`; it's usually an undefined or misformatted API field."],"exampleFix":"// before\nconst d = new Date(user.dob); // '31/07/1990' → Invalid Date\nassert.validDate(d);\n// after\nconst [day, month, year] = user.dob.split('/').map(Number);\nconst d = new Date(year, month - 1, day);\nassert.validDate(d);","handlingStrategy":"validation","validationCode":"const date = new Date(input);\nif (Number.isNaN(date.getTime())) {\n  throw new Error(`Unparseable date input: ${input}`);\n}","typeGuard":"if (is.validDate(value)) {\n  // value is a Date whose time is not NaN\n}","tryCatchPattern":"try {\n  assert.validDate(value);\n} catch (error) {\n  if (error instanceof TypeError) { /* Date exists but is Invalid Date */ }\n  throw error;\n}","preventionTips":["new Date('garbage') returns an Invalid Date object, not an error — always check getTime() is not NaN after parsing","Being a Date instance is not enough; is.date passes where is.validDate fails","Parse user/ISO date strings with a real parser or validate immediately at the input boundary"],"tags":["typescript","type-assertion","date","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}