ideal-postcodes / postcode

UK Postcode parsing and helper methods
https://postcodejs.ideal-postcodes.dev/
MIT License
69 stars 7 forks source link

Throws an error if passed null #487

Closed davecarlson closed 1 year ago

davecarlson commented 1 year ago
{
  postcodeError: TypeError: Cannot read properties of null (reading 'match')
      at exports.isValid (/node_modules/postcode/dist/index.js:95:21)
      at exports.parse (/node_modules/postcode/dist/index.js:253:18)
      at eval (webpack-internal:///(api)/./pages/api/v2/crons/postcodes.js:29:71)
      at asyncForEach (webpack-internal:///(api)/./lib/asyncForEach.js:7:11)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async dailyReport (webpack-internal:///(api)/./pages/api/v2/crons/postcodes.js:27:3),
}

usage:

const postcode = parse(user.postcode) // might be null if not set yet

I would expect if the parse function is passed null, it would return isValid: false instead:

cblanc commented 1 year ago

Hi there

Here's the interface: https://github.com/ideal-postcodes/postcode/blob/master/lib/index.ts#L285

It only accepts a string. If you're not sure about what types are flowing through your application you can parse(user.postcode || "") - will have the effect of producing invalid postcode for null

davecarlson commented 1 year ago

Hi cblanc, yes I saw the interface and your suggestion is what i'm actually doing anyway, though I raised the issue as it's less obvious if someone was using it in JS rather than typescript.

I did notice that the interface for the parser itself underneath actually accepts string or null: https://github.com/ideal-postcodes/postcode/blob/b67442dfb369fca91e6c853823dcc76542bceac0/lib/index.ts#L46